Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add IHostedConversationClient abstraction for hosted conversation lifecycle management - #7393

#7393
Open
rogerbarreto wants to merge 7 commits into
dotnet:maindotnet/extensions:mainfrom
rogerbarreto:proposal-hostedconversationclientrogerbarreto/extensions:proposal-hostedconversationclientCopy head branch name to clipboard
Open

Add IHostedConversationClient abstraction for hosted conversation lifecycle management#7393
rogerbarreto wants to merge 7 commits into
dotnet:maindotnet/extensions:mainfrom
rogerbarreto:proposal-hostedconversationclientrogerbarreto/extensions:proposal-hostedconversationclientCopy head branch name to clipboard

Conversation

@rogerbarreto

@rogerbarreto rogerbarreto commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Add IHostedConversationClient abstraction for hosted conversation lifecycle management

Summary

Adds a new IHostedConversationClient interface to Microsoft.Extensions.AI for managing server-side hosted conversations (create, get, delete, add messages, list messages). This is complementary to IChatClient — the ConversationId returned by this client feeds into ChatOptions.ConversationId for inference.

Includes the full middleware stack (logging, OpenTelemetry, ConfigureOptions, builder, DI registration), an OpenAI implementation wrapping ConversationClient, and a UseHostedConversations() ChatClient builder extension that enables chatClient.GetService<IHostedConversationClient>().

Components

Microsoft.Extensions.AI.Abstractions (6 files):

  • IHostedConversationClient — 5 CRUD operations (CreateAsync, GetAsync, DeleteAsync, AddMessagesAsync, GetMessagesAsync) + GetService
  • HostedConversation — Response type with ConversationId, CreatedAt, Metadata, RawRepresentation, AdditionalProperties
  • HostedConversationCreationOptions — Options with Metadata, Messages, RawRepresentationFactory, Clone()
  • HostedConversationClientMetadataProviderName, ProviderUri
  • DelegatingHostedConversationClient — Base class for middleware chains
  • HostedConversationClientExtensionsGetService<T> convenience

Microsoft.Extensions.AI (10 files):

  • HostedConversationClientBuilderUse()/Build() pipeline builder
  • LoggingHostedConversationClient — Debug/Trace level logging
  • OpenTelemetryHostedConversationClient — Activity spans + duration histogram
  • ConfigureOptionsHostedConversationClient — Options callback for CreateAsync
  • HostedConversationChatClientDelegatingChatClient bridge enabling chatClient.GetService<IHostedConversationClient>() via UseHostedConversations() builder extension
  • Builder extension methods + DI ServiceCollection registration

Microsoft.Extensions.AI.OpenAI (1 new file + 1 modified):

  • OpenAIHostedConversationClient — Wraps OpenAI.Conversations.ConversationClient with protocol-level JSON APIs
  • AsIHostedConversationClient() extension on ConversationClient

Tests (8 files, 63 tests):

  • Abstraction tests: property roundtrips, clone independence, delegation
  • Middleware tests: builder chain, logging, ConfigureOptions, ChatClient bridge
  • OpenAI tests: GetService, metadata, null guards

Documentation:

  • docs/HostedConversation-ProviderMapping.md — Provider mapping for OpenAI, Azure Foundry, AWS Bedrock, Google Gemini, Anthropic, Ollama with gap analysis

Usage Example

OpenAIClient openAIClient = new(apiKey);

// Build a chat client with conversation support wired in
IChatClient chatClient = openAIClient.GetChatClient("gpt-4o")
    .AsIChatClient()
    .AsBuilder()
    .UseHostedConversations(
        openAIClient.GetConversationClient().AsIHostedConversationClient())
    .UseLogging()
    .Build();

// Discover the conversation client via GetService
var conversationClient = chatClient.GetService<IHostedConversationClient>()!;
var conversation = await conversationClient.CreateAsync();

// Use the conversation for inference
var response = await chatClient.GetResponseAsync(messages, new ChatOptions
{
    ConversationId = conversation.ConversationId
});

Design Decisions

  • Reuses ChatMessage for conversation items — no new message type needed
  • [Experimental(MEAI001)] on all types — same diagnostic ID as other AI experiments
  • RawRepresentation / RawRepresentationFactory escape hatches for 99.9% provider coverage
  • UseHostedConversations() builder extension bridges IChatClientIHostedConversationClient discovery
  • Anthropic/Ollama: implementable via local adapter pattern (no native server-side conversation CRUD)
Microsoft Reviewers: Open in CodeFlow

…ecycle management

Adds a new IHostedConversationClient interface for managing server-side
conversations (create, get, delete, add messages, list messages), with
full middleware stack (logging, OpenTelemetry, ConfigureOptions, builder,
DI registration) and OpenAI implementation wrapping ConversationClient.

Key components:
- IHostedConversationClient interface with 5 CRUD operations + GetService
- HostedConversation, HostedConversationCreationOptions, metadata types
- DelegatingHostedConversationClient for middleware chains
- HostedConversationClientBuilder with Use()/Build() pipeline
- LoggingHostedConversationClient, OpenTelemetryHostedConversationClient,
  ConfigureOptionsHostedConversationClient middleware
- HostedConversationChatClient bridge enabling
  chatClient.GetService<IHostedConversationClient>() via
  UseHostedConversations() builder extension
- OpenAIHostedConversationClient wrapping OpenAI Conversations API
- RawRepresentation/RawRepresentationFactory escape hatches
- Provider mapping report documenting OpenAI, Azure, Bedrock, Gemini,
  Anthropic, and Ollama support with gap analysis
- 63 unit tests across abstractions, middleware, and OpenAI layers
@github-actions github-actions Bot added the area-ai Microsoft.Extensions.AI libraries label Mar 13, 2026
Comment thread docs/HostedConversation-ProviderMapping.md Outdated
- Refactor HostedConversationCreationOptions to HostedConversationClientOptions
  (single shared options type across all operations, matching IHostedFileClient
  pattern with Limit, RawRepresentationFactory, AdditionalProperties)
- Remove Metadata property from HostedConversation and options (use
  AdditionalProperties only, same as IHostedFileClient)
- Remove Messages from creation options (OpenAI-only feature; use
  RawRepresentationFactory or AddMessagesAsync instead)
- Rewrite OpenTelemetryHostedConversationClient to follow files.* pattern
  (conversations.* namespace with disclaimer, no gen_ai.* since no OTel
  semantic convention exists for conversation operations)
- Remove HostedConversationChatClient bridge and UseHostedConversations()
  builder extension per Stephen's feedback
- Update Azure Foundry section in provider mapping doc (v2 uses OpenAI
  directly, not deprecated Threads API per qubitron's feedback)
@rogerbarreto
rogerbarreto marked this pull request as ready for review March 30, 2026 15:21
@rogerbarreto
rogerbarreto requested review from a team as code owners March 30, 2026 15:21
Copilot AI review requested due to automatic review settings March 30, 2026 15:21

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.

Pull request overview

Adds a new hosted conversation abstraction to the AI extensions stack, enabling provider-agnostic management of server-side conversation lifecycle and message history, with middleware (logging/OpenTelemetry/options) and an OpenAI-backed implementation.

Changes:

  • Introduces IHostedConversationClient + core models/options + delegating base + GetService extensions.
  • Adds HostedConversationClientBuilder pipeline with logging, OpenTelemetry, and options-configuration middleware plus DI registration.
  • Adds an OpenAI ConversationClient adapter and expands tests + provider-mapping documentation.

Reviewed changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/Libraries/Microsoft.Extensions.AI.Tests/HostedConversation/LoggingHostedConversationClientTests.cs Tests logging middleware behavior and builder integration.
test/Libraries/Microsoft.Extensions.AI.Tests/HostedConversation/HostedConversationClientBuilderTest.cs Tests hosted conversation pipeline builder ordering and error cases.
test/Libraries/Microsoft.Extensions.AI.Tests/HostedConversation/ConfigureOptionsHostedConversationClientTests.cs Tests ConfigureOptions middleware cloning and callback behavior.
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIHostedConversationClientTests.cs Tests OpenAI adapter GetService behavior and null guards.
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/HostedConversation/HostedConversationTests.cs Tests HostedConversation property defaults and roundtrips.
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/HostedConversation/HostedConversationClientOptionsTests.cs Tests HostedConversationClientOptions cloning and extensibility.
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/HostedConversation/DelegatingHostedConversationClientTests.cs Tests pass-through behavior of delegating base client + GetService semantics.
src/Shared/DiagnosticIds/DiagnosticIds.cs Adds diagnostic ID constant for hosted conversation experimental APIs.
src/Libraries/Microsoft.Extensions.AI/HostedConversation/OpenTelemetryHostedConversationClientBuilderExtensions.cs Builder extension to add OpenTelemetry hosted conversation middleware.
src/Libraries/Microsoft.Extensions.AI/HostedConversation/OpenTelemetryHostedConversationClient.cs Implements tracing/metrics middleware for hosted conversation operations.
src/Libraries/Microsoft.Extensions.AI/HostedConversation/LoggingHostedConversationClientBuilderExtensions.cs Builder extension to add logging middleware (with NullLoggerFactory short-circuit).
src/Libraries/Microsoft.Extensions.AI/HostedConversation/LoggingHostedConversationClient.cs Implements logging middleware for hosted conversation operations.
src/Libraries/Microsoft.Extensions.AI/HostedConversation/HostedConversationClientBuilderServiceCollectionExtensions.cs DI registration helpers for hosted conversation clients (including keyed).
src/Libraries/Microsoft.Extensions.AI/HostedConversation/HostedConversationClientBuilder.cs Pipeline builder for IHostedConversationClient middleware chains.
src/Libraries/Microsoft.Extensions.AI/HostedConversation/ConfigureOptionsHostedConversationClientBuilderExtensions.cs Builder extension to configure/clone options via middleware.
src/Libraries/Microsoft.Extensions.AI/HostedConversation/ConfigureOptionsHostedConversationClient.cs Middleware that clones/modifies options before forwarding calls.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIHostedConversationClient.cs OpenAI ConversationClient adapter implementing IHostedConversationClient.
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIClientExtensions.cs Adds AsIHostedConversationClient() extension for ConversationClient.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Conversations/IHostedConversationClient.cs Defines the hosted conversation lifecycle/message API + GetService.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Conversations/HostedConversationClientOptions.cs Adds request options, raw-representation factory, and cloning support.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Conversations/HostedConversationClientMetadata.cs Adds provider metadata model for telemetry/discovery.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Conversations/HostedConversationClientExtensions.cs Adds GetService<T> and GetRequiredService helpers for hosted conversation clients.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Conversations/HostedConversation.cs Adds hosted conversation response model with raw representation + additional properties.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Conversations/DelegatingHostedConversationClient.cs Adds delegating base implementation for middleware composition.
docs/HostedConversation-ProviderMapping.md Provider mapping report / gap analysis and usage guidance.

Comment thread docs/HostedConversation-ProviderMapping.md
Comment thread src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIHostedConversationClient.cs Outdated
Comment thread docs/HostedConversation-ProviderMapping.md
- Fix stale type references in provider mapping doc
  (HostedConversationCreationOptions -> HostedConversationClientOptions)
- Add explicit ChatRole.Tool mapping in WriteMessagesArray
- Support nested image_file object in input_image parser for roundtrip
  compatibility with HostedFileContent
@verdie-g

verdie-g commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

Hi, I looked into implementing IHostedConversationClient for Mistral (doc). Here are my findings:

IHostedConversationClient Mistral endpoint Notes
CreateAsync POST /v1/conversations Mistral always runs inference. OpenAI has a pure "create empty" endpoint with no model needed.
GetAsync GET /v1/conversations/{id} All good
DeleteAsync DELETE /v1/conversations/{id} All good
AddMessagesAsync POST /v1/conversations/{id} Mistral always runs completion on append, there is no "store only" endpoint.
GetMessagesAsync GET /v1/conversations/{id}/messages Mistral returns all messages in one call with no pagination. options.Limit is silently ignored. Probably something to fix on Mistral side.
GET /v1/conversations Would listing conversations be part of another interface?

Interested about your thoughts on CreateAsync and AddMessagesAsync. In which case do we want to add a message to a conversation without an inference? Shouldn't we use use IChatClient for that? Should the IHostedConversationClient be split for read/write?

Add listing support following IHostedFileClient.ListFilesAsync pattern.
Supported by Mistral, AWS Bedrock, and Google Gemini (3/4 hosted providers).
OpenAI implementation throws NotSupportedException as the OpenAI API does
not currently expose a list conversations endpoint.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.