Skip to content

.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
microsoft:mainfrom
marcominerva:dotnet-hosting-request-scoped-agents-openai-chat-completions
Open

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

Conversation

@marcominerva

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

Copy link
Copy Markdown
Contributor

Motivation & Context

MapOpenAIChatCompletions(IHostedAgentBuilder) resolves the agent from the root service provider when the endpoint is mapped, so Scoped and Transient AddAIAgent registrations 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

  • What are the major changes?
    • MapOpenAIChatCompletions(IHostedAgentBuilder, ...) resolves Scoped/Transient agents from HttpContext.RequestServices for each request; Singleton registrations are still resolved once at mapping time. Missing registrations are still reported at mapping time without resolving the agent.
    • New MapOpenAIChatCompletions(path, Func<HttpContext, AIAgent?> agentSelector, mapOptions) overload. A null selection returns 404 with an OpenAI-style agent_not_found error; the selected agent must have a non-empty Name.
    • 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? Scoped/Transient agents get a fresh instance per request; additive public API only.

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 Chat Completions 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

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

…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>

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.

🟢 Approval recommended

Static review identified no blocking issues, and regression tests cover the principal changes, though tests were not executed.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

This branch was successfully deployed

1 active deployment
github-app-auth — feb7db01 Deployed Oct 19, 2026 by marcominerva via add_label #25736
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