Skip to content

.Net: Keep completed tool turns out of streamed agent history - #14801

Open
Jiayi Yang (ridlog) wants to merge 1 commit into
microsoft:mainfrom
ridlog:fix/streamed-agent-message-history
Open

Jiayi Yang (ridlog) wants to merge 1 commit into
microsoft:mainfrom
ridlog:fix/streamed-agent-message-history

Conversation

@ridlog

Copy link
Copy Markdown

Motivation and Context

Fixes #12485.

After a tool call, ChatCompletionAgent.InvokeStreamingAsync copies the completed assistant and tool messages into the thread. It also retains the completed turn's text in its final-message buffer. A turn that streams Planning 0., calls a tool, then streams Final answer. therefore stores Planning 0.Final answer. as the final assistant message.

Description

Clear the buffer when the chat service supplies completed tool-turn messages. Append the current update after that step so the final turn keeps its first token. The streamed updates and OnIntermediateMessage callbacks remain intact.

The regression uses the real agent and thread with a mocked external chat service. It covers zero, one, and two tool turns, plus tool termination. On unchanged base ff22ec32, two cases fail and two pass. With this fix, the full Agent unit-test project passes: 508 passed, 1 existing test skipped. The changed files also pass dotnet format --verify-no-changes.

Commands:

dotnet test dotnet/src/Agents/UnitTests/Agents.UnitTests.csproj --no-restore
dotnet format dotnet/src/Agents/UnitTests/Agents.UnitTests.csproj --no-restore --verify-no-changes --include dotnet/src/Agents/Core/ChatCompletionAgent.cs dotnet/src/Agents/UnitTests/Core/ChatCompletionAgentTests.cs

Checked with .NET SDK 10.0.303 on Windows. Credentialed live-provider integration tests and the repository platform matrix were not run locally.

This contribution was generated and reviewed with OpenAI Codex at the account owner's request. It comes from a search for reproducible Agent issues. No separate human review is claimed.

Contribution Checklist

  • The Agent unit-test project builds without reported errors or warnings.
  • The changed files pass the formatting check.
  • Added regression tests and ran the full Agent unit-test project.
  • Checked that streamed content, intermediate callbacks, and tool termination retain their existing behavior.

The streaming agent copies completed assistant and tool messages from the
chat service into its thread, but it also kept their text in the buffer
used for the final message. Reset that buffer at each completed turn
before appending the next update, so the next turn keeps its first token.

Constraint: Preserve streamed updates and intermediate message callbacks
Confidence: high
Scope-risk: narrow
Tested: Baseline regression 2 failed/2 passed; fixed Agent project 508 passed/1 skipped; dotnet format verification
Not-tested: Credentialed live provider integration tests and the full repository platform matrix
Related: microsoft#12485
@ridlog
Jiayi Yang (ridlog) requested a review from a team as a code owner November 16, 2026 15:17
Copilot AI balanced review requested due to automatic review settings November 16, 2026 15:17

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@semantic-kernel-automation semantic-kernel-automation Bot added the .NET Issue or Pull requests regarding .NET code label Nov 16, 2026
@github-actions github-actions Bot changed the title .NET: Keep completed tool turns out of streamed agent history .Net: Keep completed tool turns out of streamed agent history Nov 16, 2026
@ridlog

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

This branch was successfully deployed

1 active deployment
github-app-auth — af957cf2 Deployed Nov 16, 2026 by ridlog via add_label #29702
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

.NET Issue or Pull requests regarding .NET code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.Net: Bug: Unexpected Accumulation of Previous Assistant Messages in Final Response Using InvokeStreamingAsync

2 participants