Repository navigation
.NET: Honor hosted agent lifetimes and add request-time agent selection for OpenAI Chat Completions hosting - #9525
Open
Marco Minerva (marcominerva) wants to merge 2 commits into
Conversation
…on for OpenAI Chat Completions hosting Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 09:04 — with
GitHub Actions
Active
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 09:05 — with
GitHub Actions
Active
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 09:05 — with
GitHub Actions
Active
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The not-found response needs explicit serialization metadata to work when reflection-based JSON serialization is disabled.
1 open issue
What changed in this PR
Updates OpenAI Chat Completions hosting to honor agent lifetimes and support request-time selection, addressing the Chat Completions portion of #7733.
Changes:
- Resolves scoped and transient hosted agents per request while preserving singleton behavior.
- Adds a selector overload with name validation and a not-found response.
- Adds shared validation helpers and agent-resolution tests.
| File | Description |
|---|---|
dotnet/tests/Microsoft.Agents.AI.Hosting.OpenAI.UnitTests/OpenAIChatCompletionsAgentResolutionTests.cs |
Tests lifetimes, selection, and validation. |
dotnet/src/Shared/HostedAgents/HostedAgentResolution.cs |
Adds registration and selected-name validation. |
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.ChatCompletions.cs |
Implements request-scoped resolution and selection. |
dotnet/eng/MSBuild/Shared.props |
Adds opt-in shared-source inclusion. |
Review effort: Balanced
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 09:14 — with
GitHub Actions
Active
Merged
4 of 5 tasks
…metadata AddOpenAIChatCompletions only registers the Chat Completions serialization metadata, which does not include ErrorResponse, so with reflection-based serialization disabled the agent_not_found response of the selector endpoint and the existing unsupported_parameter response failed to serialize. Both are now written with OpenAIHostingJsonContext metadata, preserving their status codes. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Marco Minerva (marcominerva)
deployed
to
github-app-auth
October 19, 2026 13:06 — with
GitHub Actions
Active
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Motivation & Context
MapOpenAIChatCompletions(IHostedAgentBuilder)resolves the agent from the root service provider when the endpoint is mapped, so Scoped and TransientAddAIAgentregistrations are captured as singletons. The endpoint also cannot choose the agent at request time.This PR is the OpenAI Chat Completions 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
MapOpenAIChatCompletions(IHostedAgentBuilder, ...)resolves Scoped/Transient agents fromHttpContext.RequestServicesfor each request; Singleton registrations are still resolved once at mapping time. Missing registrations are still reported at mapping time without resolving the agent.MapOpenAIChatCompletions(path, Func<HttpContext, AIAgent?> agentSelector, mapOptions)overload. Anullselection returns404with an OpenAI-styleagent_not_founderror; the selected agent must have a non-emptyName.src/Shared/HostedAgents/HostedAgentResolution.cs(identical in each PR of this split, so they merge cleanly in any order).Related Issue
Part of #7733 (split from #9382).
Contribution Checklist