Skip to content

Conversation

ViveliDuCh
Copy link
Member

@ViveliDuCh ViveliDuCh commented Sep 4, 2025

Fixes #6313

Implement support for stateful conversations, enabling the use of conversationId in the Microsoft.Extensions.AI chat web template. Add backend logic to the template for dynamically selecting stateful or stateless chat clients at runtime, enabling use of experimental stateful APIs like GetOpenAIResponseClient for supporting providers (AzureOpenAI, OpenAI), and laying the groundwork for future provider support.
Providers like Ollama and GitHubModels currently use the legacy, stateless GetChatClient due to lack of stateful API support.

Note: Use GetOpenAIResponseClient only with providers that support it—namely AzureOpenAI and OpenAI. Avoid using it with Ollama and GitHubModels, which throw runtime errors due to lack of support for the ResponseStreamingChatClient interface.

Microsoft Reviewers: Open in CodeFlow

@ViveliDuCh ViveliDuCh self-assigned this Sep 4, 2025
@Copilot Copilot AI review requested due to automatic review settings September 4, 2025 07:47
@ViveliDuCh ViveliDuCh added the area-ai Microsoft.Extensions.AI libraries label Sep 4, 2025
@ViveliDuCh ViveliDuCh requested a review from a team as a code owner September 4, 2025 07:47
Copilot

This comment was marked as outdated.

@ViveliDuCh ViveliDuCh requested a review from Copilot September 4, 2025 15:18
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for stateful conversations by implementing conversationID functionality in Microsoft.Extensions.AI.WebChatTemplate. The changes enable conversation state management by switching from GetChatClient to GetOpenAIResponseClient for OpenAI and AzureOpenAI providers while maintaining compatibility with providers that don't support the Responses interface.

  • Switch from GetChatClient to GetOpenAIResponseClient for OpenAI/AzureOpenAI providers to enable conversationID support
  • Implement conversation state management by tracking conversationID in chat options and updating it from streaming responses

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Chat.razor Updates streaming response handling to pass only user message when the conversation is stateful (conversationId not null), skipping the rest of the message history based on the conversationID.
Program.cs Replaces GetChatClient with GetOpenAIResponseClient/AddResponsesChatClient calls for the providers that support it.

Note: Adds inline NoWarn directive to suppress OPENAI001 warnings.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@ViveliDuCh ViveliDuCh closed this Sep 5, 2025
@ViveliDuCh ViveliDuCh reopened this Sep 5, 2025
@ViveliDuCh
Copy link
Member Author

@dotnet-policy-service agree company="Microsoft"

Copy link
Member

@jozkee jozkee left a comment

Choose a reason for hiding this comment

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

Otherwise, LGTM!

Copy link
Member

@MackinnonBuck MackinnonBuck left a comment

Choose a reason for hiding this comment

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

This is looking great! Just a few suggestions.

Copy link
Member

@jeffhandley jeffhandley left a comment

Choose a reason for hiding this comment

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

Talking with @stephentoub about this, we should consider another approach to these changes. The template should produce a project that handles both stateful and stateless conversations.

@ViveliDuCh / @jozkee - We can chat more about the implications of this different approach.

@MackinnonBuck
Copy link
Member

The template should produce a project that handles both stateful and stateless conversations.

I believe something like what I suggested here should handle both cases. We only submit the a subset of the converstaion history if the latest response update specified a ConversationId. Otherwise, the full conversation is sent every time.

…ient at runtime, remove Aspire-specific workarounds, simplify template logic, and default to stateful API where supported.
@ViveliDuCh ViveliDuCh merged commit 7f5c3c2 into dotnet:main Sep 11, 2025
6 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update MEAI Template to integrate ConversationId for stateful chat systems

4 participants