Skip to content

.NET: Add request-time agent selection for OpenAI Responses hosting - #9526

Open
Marco Minerva (marcominerva) wants to merge 3 commits into
microsoft:mainfrom
marcominerva:dotnet-hosting-request-scoped-agents-openai-responses
Open

Marco Minerva (marcominerva) wants to merge 3 commits into
microsoft:mainfrom
marcominerva:dotnet-hosting-request-scoped-agents-openai-responses

Conversation

@marcominerva

@marcominerva Marco Minerva (marcominerva) commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

The OpenAI Responses endpoints are bound to a single agent at mapping time, so applications that need to choose the agent per request (for example from a route value, header or claim) have to map one set of endpoints per agent.

This PR is the OpenAI Responses part of #9382, which was split per hosting protocol to make review easier. It is independent from the other PRs of the split.

Description & Review Guide

  • What are the major changes?
    • New MapOpenAIResponses(responsesPath, Func<HttpContext, AIAgent?> agentSelector, mapOptions) overload. The selector applies to every operation (create, get, cancel, delete, list input items); a null selection returns 404 before any response, session or conversation state is loaded.
    • Stored responses are bound to the selected agent's Name (InMemoryResponsesService.ForAgent), so a response can only be retrieved, canceled or deleted through requests that select the same agent. The agent name also keys the session store and partitions persisted sessions across new agent instances.
    • The internal IResponsesService/ResponsesHttpHandler accept the executor selected for the request; after execution the stored response releases the executor so the agent graph is not kept alive by stored responses.
    • Shared source helper src/Shared/HostedAgents/HostedAgentResolution.cs (identical in each PR of this split, so they merge cleanly in any order).
  • What is the impact of these changes? Additive public API only; the changed service types are internal. Existing single-agent mappings behave as before.

Related Issue

Part of #7733 (split from #9382).

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change.

…on for OpenAI Responses hosting

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Selected agents and session stores use different scopes, and disconnected streams can continue using disposed request-scoped dependencies.

2 open issues
What changed in this PR

Adds request-time agent selection to .NET OpenAI Responses hosting as part of #7733.

Changes:

  • Adds a selector overload covering all response operations.
  • Binds stored responses and session identity to the selected agent’s name.
  • Releases completed executors and adds selection and persistence regression tests.
File Description
dotnet/​tests/​Microsoft.Agents.AI.Hosting.OpenAI.UnitTests/​ResponseSessionRegressionTests.cs Tests selection, isolation, and session continuity.
dotnet/​tests/​Microsoft.Agents.AI.Hosting.OpenAI.UnitTests/​InMemoryResponsesServiceTests.cs Tests executor release after completion.
dotnet/​src/​Shared/​HostedAgents/​HostedAgentResolution.cs Adds shared agent identity validation.
dotnet/​src/​Microsoft.Agents.AI.Hosting.OpenAI/​Responses/​ResponsesHttpHandler.cs Selects agents for every operation.
dotnet/​src/​Microsoft.Agents.AI.Hosting.OpenAI/​Responses/​IResponsesService.cs Adds request-selected executor overloads.
dotnet/​src/​Microsoft.Agents.AI.Hosting.OpenAI/​Responses/​InMemoryResponsesService.cs Partitions responses and releases executors.
dotnet/​src/​Microsoft.Agents.AI.Hosting.OpenAI/​Responses/​AIAgentResponseExecutor.cs Supports explicit session storage identity.
dotnet/​src/​Microsoft.Agents.AI.Hosting.OpenAI/​Microsoft.Agents.AI.Hosting.OpenAI.csproj Includes shared hosting helpers.
dotnet/​src/​Microsoft.Agents.AI.Hosting.OpenAI/​EndpointRouteBuilderExtensions.Responses.cs Adds selector mapping and endpoint wiring.
dotnet/​eng/​MSBuild/​Shared.props Enables shared hosting source inclusion.

Review effort: Balanced

…quest

Agents selected for OpenAI Responses can be resolved from HttpContext.RequestServices, but their keyed session store was resolved from separate per-operation scopes, so a store depending on request-initialized state received a different, uninitialized dependency. Validation and responses that are not run in the background now resolve the store from the request services of the selected agent, while background responses keep using a scope they own because they outlive the request.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… run completes

Responses for selected agents that are not run in the background use services owned by the HTTP request, but streaming execution kept running after the client disconnected while the request ended, and a canceled non-streaming request ended while its execution was still unwinding. Executors now report whether an execution is bound to the request, and the responses service waits for such executions before the request ends: streaming runs are not canceled and complete normally, while canceled non-streaming runs are awaited until they stop.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

This branch was successfully deployed

1 active deployment
github-app-auth — 1907d1b8 Deployed Oct 19, 2026 by marcominerva via add_label #25741
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Usage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants