* .NET: Persist input messages on streaming errors in PerServiceCallChatHistoryPersistingChatClient
When the underlying chat service emits an in-stream error (for example a
`response.error` SSE event from the OpenAI Responses API on rate limit),
the OpenAI client surfaces it as an `ErrorContent` update and ends the
stream without throwing. Previously, `PerServiceCallChatHistoryPersistingChatClient`
only persisted history when the streaming loop completed successfully and
`NotifyProvidersOfNewMessagesAsync` was called at the end. On the
in-stream-error path, the input messages handed to that iteration -
typically `FunctionResultContent` produced by `FunctionInvokingChatClient`
in the previous iteration - were never persisted. The next run would
replay session history with a dangling `FunctionCallContent` and the
service would reject the request with `No tool output found for function
call <id>`.
This change:
- Adds a `PersistInputOnErrorAsync` helper that persists the input
messages (with no response messages) so function-call/function-result
pairings are not split across failures.
- Calls the helper from every error path: pre-loop enumerator creation,
the first `MoveNextAsync`, the in-loop `MoveNextAsync`, and a new
`finally` that handles abnormal iterator disposal.
- After the streaming loop, scans the assembled response for any
`ErrorContent` and, if present, persists the input, notifies
providers of failure, and throws `InvalidOperationException` so the
error is surfaced to the caller instead of silently corrupting history.
- Hardens `InMemoryChatHistoryProvider.StoreChatHistoryAsync` to treat
a null `RequestMessages` as empty, since the new error path can
invoke it with no response messages.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix dropped FunctionResultContent on streaming pipeline early-disposal
When a consumer of ChatClientAgent.RunStreamingAsync stops iterating early
(e.g. ToolApprovalAgent yields the approval request and then `yield break`),
the framework cascades DisposeAsync down the stream. C# async iterators do
not auto-dispose IAsyncDisposable locals, so the inner enumerator returned
by IChatClient.GetStreamingResponseAsync(...).GetAsyncEnumerator(ct) was
left suspended. That suspended FunctionInvokingChatClient downstream, which
suspended PerServiceCallChatHistoryPersistingChatClient at its `yield
return`, so its finally block never ran and the in-flight
FunctionResultContent for the just-completed tool call was not persisted
to chat history. The next turn then loaded a session that contained a
FunctionCallContent with no matching FunctionResultContent and the model
returned HTTP 400 `No tool output found for function call`.
Fixes:
* ChatClientAgent.RunStreamingAsync: wrap the iteration in
try/finally that disposes the inner enumerator. Disposal now cascades
through the pipeline and PerService's finally runs on early exit.
* PerServiceCallChatHistoryPersistingChatClient: in the streaming path,
snapshot input messages with `messages.ToList()` (the caller, FICC,
reuses a single mutable buffer across iterations and may mutate it
before our finally / error path persists), wrap GetAsyncEnumerator,
the first MoveNextAsync, and in-loop MoveNextAsync in try/catch each
calling PersistInputOnErrorAsync + NotifyProvidersOfFailureAsync, and
add a finally that calls PersistInputOnErrorAsync when the loop did
not exit normally so per-iteration FRCs are persisted on early
disposal as well as on errors.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* .NET: Add tests for PerService streaming error/dispose persistence paths
Adds five regression tests covering the new error-path persistence in
PerServiceCallChatHistoryPersistingChatClient.GetStreamingResponseInnerAsync:
- Persists input messages when GetStreamingResponseAsync throws synchronously.
- Persists input messages when the first MoveNextAsync throws.
- Persists input messages when a mid-stream MoveNextAsync throws.
- Persists input messages when the consumer abandons enumeration early
(the ToolApprovalAgent yield-break / disposal-cascade case).
- Throws and persists input when the stream emits an in-band ErrorContent.
All 66 tests in the class pass on net10.0 and net472.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* .NET: Address PR feedback on PerService streaming error persistence
Two follow-ups from PR #5744 review:
1. Prevent duplicate persistence on the in-loop MoveNextAsync catch path.
The inner catch persists input messages, then rethrows, which propagates
through the surrounding try/finally where loopExitedNormally is still false,
causing the finally to persist again. Introduced an inputPersisted flag
that the inner catch sets after persisting; the finally now skips when
inputPersisted is true.
2. Use the caller's CancellationToken in the abnormal-exit finally instead
of CancellationToken.None, so cleanup remains responsive to cancellation.
Fall back to CancellationToken.None only when the caller's token is
already canceled (otherwise the persist call would observe the
cancellation, throw, and mask the original early-exit reason).
Tightened all five new streaming-error tests from Times.AtLeastOnce to
Times.Once on the input-persistence matcher to regression-guard against
duplicate persistence. All 66 tests in the class still pass (net10.0 + net472).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* .NET: Scope PerService streaming changes to cooperative early-exit only
Per discussion on PR #5744, scope this PR back to fix only the original
ToolApprovalAgent dropped-FunctionResultContent bug and address the
enumerator-disposal review comment. Specifically:
- Remove input-message persistence from the GetAsyncEnumerator and
MoveNextAsync error paths. Routing failed service calls through the
success notification channel was breaking the provider contract; we
will instead rely on inner-agent retries for transient errors. Failure
paths still call NotifyProvidersOfFailureAsync as before.
- Remove the in-stream ErrorContent detection block (same rationale).
- Keep the try/finally that calls the (now narrower) early-exit input
notification on cooperative disposal (e.g. ToolApprovalAgent yield
break). A new serviceErrorOccurred flag ensures we do NOT renotify
on exception paths.
- Always DisposeAsync the underlying enumerator on every exit path,
addressing the copilot-reviewer comment about leaked HTTP/streams.
- Rename PersistInputOnErrorAsync -> NotifyProvidersOfEarlyExitInputAsync
to better reflect what it does and when it runs (rogerbarreto nit).
- Apply rogerbarreto nit on InMemoryChatHistoryProvider null-coalescing.
- Drop the four tests that covered the removed error-path behavior;
keep RunStreamingAsync_PersistsInputMessages_WhenConsumerAbandons
EnumerationAsync (regression guard for the cooperative-pause path).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: alliscode <bentho@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Adding the ability to inject messages during the function call loop
* Split message injection functionality
* Remove interface, since it is not required not that we split the chat client.
* Address conversation id propogation
* Fix formatting issue
* Changes
* Fix ChatClientAgent streaming responses missing MessageId
Generate fallback MessageId in ChatClientAgent.RunCoreStreamingAsync when
the underlying LLM provider does not set ChatResponseUpdate.MessageId.
Without a MessageId the AGUI converter's null==null check silently drops
all text content, causing CopilotKit Zod validation errors.
Changes:
- ChatClientAgent: generate msg_{Guid} fallback via ??= in streaming loop
- AgentResponseExtensions: sync wrapper MessageId back to RawRepresentation
in AsChatResponseUpdate() so downstream consumers see the value
- Add unit tests for both fixes and AGUI streaming MessageId scenarios
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Address PR #4615 review comments
- Fix MessageId seeding: use first-seen provider MessageId (or generate
fallback) and apply consistently to all chunks in the stream, preventing
message splitting when providers set MessageId only on the first chunk
- Add test for mixed MessageId scenario (first chunk only)
- Fix skipped TextStreaming test: assert Empty (not NotEmpty) to match
actual null==null behavior
- Fix skipped ToolCalls test: assert empty ParentMessageId to match
actual empty-string passthrough behavior
* Handle empty MessageId in AsChatResponseUpdate sync
Treat empty/whitespace MessageId the same as null when syncing from
the AgentResponseUpdate wrapper back to RawRepresentation. Providers
that return empty string MessageId (e.g. tool call responses) now get
the wrapper value recovered correctly.
Add test for empty string MessageId recovery scenario.
* Move MessageId fallback generation to AGUI layer
Move fallback MessageId generation from ChatClientAgent to
AsAGUIEventStreamAsync, addressing the architectural concern that
MessageId is nullable in the AIAgent abstraction and the requirement
for non-null values is specific to the AGUI protocol.
The AGUI layer now generates a fallback MessageId for null or
empty/whitespace values, covering all agent types (not just
ChatClientAgent) including external implementations.
Changes:
- Revert MessageId generation from ChatClientAgent.RunCoreStreamingAsync
- Add fallback MessageId generation in AsAGUIEventStreamAsync for
null/empty MessageId values (handles both null and whitespace)
- Unskip and update AGUI tests to verify fallback generation
- Update ChatClientAgent tests to reflect passthrough behavior
* Revert AsChatResponseUpdate MessageId sync-back
Remove the MessageId sync-back logic from AsChatResponseUpdate() as it
is no longer needed. With fallback generation moved to the AGUI layer,
the abstraction layer should not mutate the RawRepresentation object.
Revert to the original passthrough behavior for AsChatResponseUpdate()
and update tests accordingly.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Persist messages during the Function Call Loop
* Revert version reset
* Fix bugs and improve sample
* Fix formatting issues
* Also updating conversation id during run
* Update based on ADR feedback
* .NET: [BREAKING] Add session statebag to use for state storage instead 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>
* Add a public StateKey property to providers (#3810)
* .NET: [BREAKING] Update providers in such a way that they can participate 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
* .NET: [BREAKING] Add consistent message filtering to all providers. (#3851)
* Add consistent message filtering to all providers.
* Remove old chat history filtering classes
* Fix merge issues
* Fix unit test
* Enforce non-nullable property
* Fix merging bug and make troubleshooting source info easier by adding tostring implementation
* .NET: [BREAKING] Add support for multiple AIContextProviders on a ChatClientAgent (#3863)
* Add support for multiple AIContextProviders on a ChatClientAgent
* Address PR comments and fix tests
* Address PR comments.
* .NET: [BREAKING]Delay AIContext Materialization until the end of the pipeline is reached. (#3883)
* Delay AIContext Materialization until the end of the pipeline is reached.
* Address PR comments.
* Address PR comments
* Modify InvokedContext to be immutable (#3888)
* .NET: Address Feedback on StateBag feature branch PR (#3910)
* 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>
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
RunCoreStreamingAsync was passing inputMessagesForProviders (which lacks
chat history) to GetStreamingResponseAsync instead of
inputMessagesForChatClient (which includes chat history). This caused
streaming runs to lose conversation context on subsequent calls.
The non-streaming path (RunCoreAsync) already correctly used
inputMessagesForChatClient. This aligns the streaming path to match.
Also adds a unit test that validates chat history is included in
messages sent to the chat client during streaming on subsequent calls.
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: lokitoth <6936551+lokitoth@users.noreply.github.com>
* Add ability to mark the source of Agent request messages and use that for filtering
* Add support for source, in addition to source type, and add unit tests for automatic stamping
* Address PR comments.
* Add merge fixes
* Address PR comments
* Add a StateBag to AgentSession and pass Agent and AgentSession to AIContextProvider and ChatHistoryProviders
* Remove statebag code from this branch, to get the refactoring out of the way first
* Apply suggestion from @rogerbarreto
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
* Apply suggestion from @westey-m
* Apply suggestion from @westey-m
---------
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
* save input messages and stream updates to the continuation token to be able to use them in the last successful stream resumption call.
* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgent_BackgroundResponsesTests.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgentContinuationToken.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* fix typo
* init continuation token from chat response
* remove unnecessary types for source generation
* remove check for continuation token passed at initial run
* remove check for continuation token pass at initial run
* centralize continuation token parsing
* update xml comments
* use readonly collection instead of enumerable
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Refactor ChatMessageStore methods to be similar to AIContextProvider
* Fix file encoding
* Ensure that AIContextProvider messages area also persisted.
* Update formatting and seal context classes
* Improve formatting
* Remove optional messages from constructor and add unit test
* Add ChatMessageStore filtering via a decorator
* Update sample and cosmos message store to store AIContextProvider messages in right order. Fix unit tests.
* Update Workflowmessage store to use aicontext provider messages.
* Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Apply suggestions from code review
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
* Improve xml docs messaging
* Address code review comments.
* Also notify message store on failure
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: SergeyMenshykh <68852919+SergeyMenshykh@users.noreply.github.com>
* prevent stremed updates loss when resuming streaming with non-agent managed store or/and context provider
* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* throw not supported exception instead invalid operation
* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
* Update dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
* use conversation id to check if chat history is managed by agent service or not
* extract background responses tests into a separate file
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: westey <164392973+westey-m@users.noreply.github.com>
* Fix issue where AIContextProvider messages were not added to MessageStores
* Fix typos
* Update XML docs to reduce ambiguity.
* Update AIContext XML docs
* Fix merge issue
* unit test for using create agent option by constructor
* remove this for prevent duplicate when ChatClientAgentOption and ChatOption has same Instruction
* update unit test for ChatClientAgentOptions
* improve structured output for chat client agent
* add comment to the result property
* remove code duplication and add tests
* refactor the CreateAIAgent extension methods to return specific types, so consumers can avoid unnecessary downcasting.
* fix type and remove unused using.
* add ChatClientAgentRunResponse and move AgentRunResponse to the abstractions package to reuse later.
* seal ChatClientAgentRunResponse
* update xml comment
* remove funcitons from sample
* rename agent for streaming
* Fix bug where ChatClientAgent throws when providing a ChatMessageStore with a service that requries service storage
* Update dotnet/tests/Microsoft.Agents.AI.UnitTests/ChatClient/ChatClientAgentTests.cs
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Make serialize methods sync and rename one to match others.
* Remove unnecessary async postfixes.
* Remove nullability of ChatMessageStore.Serialize return type, since the default JsonElement already represents an undefined json element.
* Fix unit test
* Rename AI Agent packages to use Microsoft.Agents.AI
* Fix for build
* Fix formatting
* Fix formatting
* Ignore in VSTHRD200 in migration samples
* Ignore in VSTHRD200 in migration samples
* Add some missing projects and run format
* Fix build errors
* Address code review feedback
* Fix merge issues
---------
Co-authored-by: Mark Wallace <markwallace@microsoft.com>