Conversation
…d of inside providers (#3737) * Add a StateBag to AgentSession and pass Agent and AgentSession to AIContextProvider and ChatHistoryProviders * Convert all AIContextProviders to use the statebag * Update InMemoryChatHistoryProvider to use StateBag * Update Comsos and Workflow ChatHistoryProviders * Update 3rd party chat history storage sample. * Remove serialize method from providers * Replacing provider factories with properties * Remove Providers from Session and flatten state bag serialization * Update samples to use getservice on agent * Updated additional session types to serialize statebag * Fix regression * Address PR comments * Address PR comments. * Fix formatting * Fix unit tests * Remove InMemoryAgentSession since it is not required anymore. * Address PR comments * Convert sessions for A2AAgent, ChatClientAgent, CopilotStudioAgent and GithubCopilotAgent to use regular json serialization. * Fix durable agent session jso usgae * Add jso to InMemory and Workflow ChatHistoryProviders * Update InMemoryChatHistoryProvider to use an options class for it's many optional settings. * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address PR feedback * Fix verification bug. * Improve state bag thread safety * Address PR comments and fix unit tests * Address PR comments * Fix unit test --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…pate in a pipeline (#3846) * Make providers pipeline capable * Fix unit tests * Move source stamping to providers from base class * Also update samples. * Address PR comments * Rename AsAgentRequestMessageSourcedMessage to WithAgentRequestMessageSource
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a breaking redesign of .NET session state and provider configuration by adding a per-session StateBag for provider state storage and enabling multiple AIContextProviders on an agent. It updates core abstractions, providers, tests, and samples to align with the new model.
Changes:
- Add
AgentSession.StateBagand supporting serialization/converter infrastructure for session-scoped provider state. - Update
ChatHistoryProvider/AIContextProviderandChatClientAgentOptionsto support agent-level providers (and multiple context providers) instead of per-session/provider factories/serialization. - Refactor built-in providers, workflows, tests, and samples to store state in
StateBagand to use updated invocation context shapes (AIContextpipeline + revised invoked contexts).
Reviewed changes
Copilot reviewed 103 out of 103 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/tests/OpenAIResponse.IntegrationTests/OpenAIResponseFixture.cs | Read chat history via agent service provider lookup |
| dotnet/tests/OpenAIChatCompletion.IntegrationTests/OpenAIChatCompletionFixture.cs | Read chat history via agent service provider lookup |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/WorkflowHostSmokeTests.cs | Update custom sessions to new AgentSession + StateBag model |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/TestRequestAgent.cs | Migrate session serialization to StateBag |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/TestReplayAgent.cs | Replace InMemoryAgentSession with AgentSession |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/TestEchoAgent.cs | Switch chat history storage to provider + StateBag |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/Sample/06_GroupChat_Workflow.cs | Replace InMemoryAgentSession with AgentSession |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/RoleCheckAgent.cs | Replace InMemoryAgentSession with AgentSession |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/InProcessExecutionTests.cs | Replace InMemoryAgentSession with AgentSession |
| dotnet/tests/Microsoft.Agents.AI.Workflows.UnitTests/AgentWorkflowBuilderTests.cs | Replace InMemoryAgentSession with AgentSession |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/TestJsonSerializerContext.cs | Add ChatClientAgentSession to STJ source gen context |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_DeserializeSessionTests.cs | Remove obsolete tests for old session/provider factory design |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_CreateSessionTests.cs | Remove obsolete tests for old session/provider factory design |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_ChatHistoryManagementTests.cs | Update tests to agent-level provider + new invoked contexts |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_BackgroundResponsesTests.cs | Update setup for multiple AI context providers + state keys |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentSessionTests.cs | Update serialization/deserialization tests to StateBag model |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentOptionsTests.cs | Update options tests to ChatHistoryProvider + AIContextProviders |
| dotnet/tests/Microsoft.Agents.AI.Mem0.IntegrationTests/Mem0ProviderTests.cs | Update Mem0 tests to session-scoped state via StateBag |
| dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.UnitTests/AGUIEndpointRouteBuilderExtensionsTests.cs | Migrate test sessions to STJ + StateBag |
| dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/SharedStateTests.cs | Migrate test sessions to STJ + StateBag |
| dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/ForwardedPropertiesTests.cs | Migrate test sessions to STJ + StateBag |
| dotnet/tests/Microsoft.Agents.AI.Hosting.AGUI.AspNetCore.IntegrationTests/BasicStreamingTests.cs | Migrate test sessions to STJ + StateBag |
| dotnet/tests/Microsoft.Agents.AI.DurableTask.UnitTests/DurableAgentSessionTests.cs | Update expected durable session JSON + add StateBag roundtrip tests |
| dotnet/tests/Microsoft.Agents.AI.AzureAI.UnitTests/AzureAIProjectChatClientExtensionsTests.cs | Update options preservation tests to new provider configuration |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/TestJsonSerializerContext.cs | Remove source-gen types tied to removed session models |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ServiceIdAgentSessionTests.cs | Remove tests for deleted ServiceIdAgentSession |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/InMemoryAgentSessionTests.cs | Remove tests for deleted InMemoryAgentSession |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatMessageExtensionsTests.cs | Update renamed message-stamping API usage |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderTests.cs | Update tests for revised invoked context API |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderMessageFilterTests.cs | Remove tests for deleted provider filter decorator |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/ChatHistoryProviderExtensionsTests.cs | Remove tests for deleted provider extensions |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentSessionTests.cs | Add basic StateBag roundtrip test |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRequestMessageSourceTypeTests.cs | Add ToString() tests |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRequestMessageSourceAttributionTests.cs | Add ToString() tests |
| dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIContextTests.cs | Update tests for IEnumerable-based Messages/Tools |
| dotnet/tests/Microsoft.Agents.AI.A2A.UnitTests/A2AAgentSessionTests.cs | Update A2A session deserialize + add StateBag preservation test |
| dotnet/tests/AzureAI.IntegrationTests/AIProjectClientFixture.cs | Read chat history via agent service provider lookup |
| dotnet/tests/AnthropicChatCompletion.IntegrationTests/AnthropicChatCompletionFixture.cs | Read chat history via agent service provider lookup |
| dotnet/src/Microsoft.Agents.AI/TextSearchProviderOptions.cs | Add StateKey + message filters for statebag-backed provider state |
| dotnet/src/Microsoft.Agents.AI/TextSearchProvider.cs | Move provider state to StateBag, update message/tool accumulation |
| dotnet/src/Microsoft.Agents.AI/Memory/ChatHistoryMemoryProviderOptions.cs | Add StateKey + message filters for statebag-backed provider state |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentSession.cs | Convert session persistence to STJ + StateBag |
| dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentOptions.cs | Replace factories with ChatHistoryProvider + AIContextProviders |
| dotnet/src/Microsoft.Agents.AI/AgentJsonUtilities.cs | Update STJ context types for new session/provider state model |
| dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowSession.cs | Store workflow session state in StateBag + update provider integration |
| dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowHostAgent.cs | Update workflow message storage path to pass session into provider |
| dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowChatHistoryProvider.cs | Migrate provider storage to StateBag |
| dotnet/src/Microsoft.Agents.AI.OpenAI/Extensions/OpenAIAssistantClientExtensions.cs | Update options mapping to new provider configuration |
| dotnet/src/Microsoft.Agents.AI.Mem0/Mem0ProviderOptions.cs | Add StateKey + message filters for statebag-backed provider state |
| dotnet/src/Microsoft.Agents.AI.Mem0/Mem0JsonUtilities.cs | Update STJ context type names |
| dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotJsonUtilities.cs | Switch STJ context to session type |
| dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgentSession.cs | Convert session persistence to STJ + StateBag |
| dotnet/src/Microsoft.Agents.AI.GitHub.Copilot/GitHubCopilotAgent.cs | Use new Deserialize session helper |
| dotnet/src/Microsoft.Agents.AI.DurableTask/DurableAgentSession.cs | Add StateBag persistence to durable sessions |
| dotnet/src/Microsoft.Agents.AI.CosmosNoSql/CosmosDBChatExtensions.cs | Replace factory with shared provider + session state initializer |
| dotnet/src/Microsoft.Agents.AI.CopilotStudio/CopilotStudioJsonUtilities.cs | Add STJ utilities for Copilot Studio session |
| dotnet/src/Microsoft.Agents.AI.CopilotStudio/CopilotStudioAgentSession.cs | Replace ServiceIdAgentSession with AgentSession + StateBag |
| dotnet/src/Microsoft.Agents.AI.CopilotStudio/CopilotStudioAgent.cs | Use new Deserialize session helper |
| dotnet/src/Microsoft.Agents.AI.AzureAI/AzureAIProjectChatClientExtensions.cs | Update options mapping to new provider configuration |
| dotnet/src/Microsoft.Agents.AI.AzureAI.Persistent/PersistentAgentsClientExtensions.cs | Update options mapping to new provider configuration |
| dotnet/src/Microsoft.Agents.AI.Abstractions/ServiceIdAgentSession.cs | Remove obsolete session base type |
| dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryChatHistoryProviderOptions.cs | New options object for statebag-backed in-memory chat history |
| dotnet/src/Microsoft.Agents.AI.Abstractions/InMemoryAgentSession.cs | Remove obsolete in-memory session type |
| dotnet/src/Microsoft.Agents.AI.Abstractions/ChatMessageExtensions.cs | Rename stamping helper to WithAgentRequestMessageSource |
| dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderMessageFilter.cs | Remove obsolete decorator (relied on mutable invoked context) |
| dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProviderExtensions.cs | Remove obsolete extensions |
| dotnet/src/Microsoft.Agents.AI.Abstractions/ChatHistoryProvider.cs | Shift state storage guidance to StateBag, revise invoked context API |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagValueJsonConverter.cs | Add converter for state bag value storage |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagValue.cs | Add cached JSON/deserialized representation for state bag values |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagJsonConverter.cs | Add converter to serialize state bag as a JSON object |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBag.cs | Add thread-safe session-scoped state store with JSON persistence |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSession.cs | Add StateBag to base session type + debugger display |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSourceType.cs | Add ToString() implementation |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRequestMessageSourceAttribution.cs | Add ToString() implementation |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AgentAbstractionsJsonUtilities.cs | Add source-gen support for state bag types |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AIContextProvider.cs | Remove message stamping + switch to AIContext pipeline |
| dotnet/src/Microsoft.Agents.AI.Abstractions/AIContext.cs | Switch Messages/Tools to IEnumerable to support deferred pipelines |
| dotnet/src/Microsoft.Agents.AI.A2A/A2AJsonUtilities.cs | Switch STJ context to session type |
| dotnet/src/Microsoft.Agents.AI.A2A/A2AAgentSession.cs | Convert session persistence to STJ + StateBag |
| dotnet/src/Microsoft.Agents.AI.A2A/A2AAgent.cs | Use new Deserialize session helper |
| dotnet/samples/GettingStarted/Agents/Agent_Step20_AdditionalAIContext/Program.cs | Update sample to multiple providers + StateBag storage |
| dotnet/samples/GettingStarted/Agents/Agent_Step16_ChatReduction/Program.cs | Update sample to new in-memory provider options + retrieval |
| dotnet/samples/GettingStarted/Agents/Agent_Step07_3rdPartyChatHistoryStorage/Program.cs | Update sample provider to store state in StateBag |
| dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step03_CustomRAGDataSource/Program.cs | Update to AIContextProviders list |
| dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step02_CustomVectorStoreRAG/Program.cs | Update to AIContextProviders list |
| dotnet/samples/GettingStarted/AgentWithRAG/AgentWithRAG_Step01_BasicTextRAG/Program.cs | Update to AIContextProviders + chat history storage filter |
| dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step03_CustomMemory/Program.cs | Update memory sample to StateBag + agent-level provider access |
| dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step02_MemoryUsingMem0/Program.cs | Update Mem0 sample to StateBag state initializer |
| dotnet/samples/GettingStarted/AgentWithMemory/AgentWithMemory_Step01_ChatHistoryMemory/Program.cs | Update ChatHistoryMemoryProvider initialization to state initializer |
| dotnet/samples/GettingStarted/AgentProviders/Agent_With_CustomImplementation/Program.cs | Update custom provider usage + STJ session serialization |
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagValue.cs
Outdated
Show resolved
Hide resolved
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentSessionStateBagValue.cs
Outdated
Show resolved
Hide resolved
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentSessionStateBagTests.cs
Outdated
Show resolved
Hide resolved
dotnet/samples/GettingStarted/AgentProviders/Agent_With_CustomImplementation/Program.cs
Outdated
Show resolved
Hide resolved
dotnet/samples/GettingStarted/AgentProviders/Agent_With_CustomImplementation/Program.cs
Outdated
Show resolved
Hide resolved
4 tasks
* Address Feedback on statebag feature branch PR * Update dotnet/src/Microsoft.Agents.AI.DurableTask/CHANGELOG.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Address PR comments --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
SergeyMenshykh
approved these changes
Feb 13, 2026
markwallace-microsoft
approved these changes
Feb 13, 2026
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 and Context
Description
Contribution Checklist