Skip to content

Python: preserve empty signed Anthropic thinking blocks on streaming replay - #8171

Merged
Evan Mattson (moonbox3) merged 4 commits into
microsoft:mainfrom
leilei3167:fix/8168-empty-signed-thinking-replay
Sep 9, 2026
Merged

Evan Mattson (moonbox3) merged 4 commits into
microsoft:mainfrom
leilei3167:fix/8168-empty-signed-thinking-replay

Conversation

@leilei3167

@leilei3167 leilei3167 commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

Anthropic can emit a thinking block with thinking="" followed by a signature_delta. Stream aggregation was collapsing empty string to None, so the Anthropic serializer treated a real empty signed thinking block as an orphan signature and dropped it before tool-result replay. Nonempty thinking text was already preserved.

Fixes #8168

Description & Review Guide

  • What are the major changes? Keep "" distinct from None in Content._add_text_reasoning_content when merging reasoning text, so empty signed Anthropic thinking survives aggregation and replay.
  • What is the impact of these changes? Replay emits {"type": "thinking", "thinking": "", "signature": ...} before the tool call when that is what the stream produced. Both-None still stays None.
  • What do you want reviewers to focus on? The merge semantics in _types.py ("" vs None) and the Anthropic streaming replay regression.

Related Issue

Fixes #8168

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Empty thinking="" plus a signature_delta was coalesced to text=None, so
the Anthropic serializer treated it as an orphan signature and dropped
it before tool_use replay. Keep "" distinct from None through merge.

Fixes microsoft#8168

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.

🔵 Needs a closer look

It changes shared reasoning aggregation in a high-risk provider replay path requiring broader validation.

Pull request overview

Preserves empty signed Anthropic thinking blocks during streaming aggregation and replay.

Changes:

  • Distinguishes empty reasoning text from absent text during merging.
  • Adds core and Anthropic replay regression coverage.
File summaries
File Description
python/packages/core/agent_framework/_types.py Preserves empty reasoning text during aggregation.
python/packages/core/tests/core/test_types.py Tests empty and absent reasoning-text merges.
python/packages/anthropic/tests/test_anthropic_client.py Tests serialization and end-to-end streaming replay.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@leilei3167

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@leilei3167

Copy link
Copy Markdown
Contributor Author

CLA signed — license/cla is green now. Thanks for reviewing when you have a chance.

@moonbox3

Copy link
Copy Markdown
Contributor

leilei3167:

  1. Please update the PR body to use our approved template: https://github.com/microsoft/agent-framework/blob/main/.github/pull_request_template.md
  2. Please also fix the failing code quality checks

Use a ternary for combined_text so Package Checks lint passes.
@leilei3167

Copy link
Copy Markdown
Contributor Author

Updated the PR body to the approved template, and fixed the Package Checks lint (SIM108 ternary for combined_text). Ready for another look when you have a chance.

@moonbox3

Copy link
Copy Markdown
Contributor

please fix the failing CI/CD checks thanks

@leilei3167

Copy link
Copy Markdown
Contributor Author

CI is green on the latest tip now — Package Checks, Typing (zuban), Pre-commit, Samples, and the Python matrix all pass. Ready for another look when you have a chance.

@moonbox3
Evan Mattson (moonbox3) added this pull request to the merge queue Sep 9, 2026
Merged via the queue into microsoft:main with commit 2cb63d3 Sep 9, 2026
37 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — bc15a334 Deployed Sep 9, 2026 by leilei3167 via add_label #22203
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Empty signed thinking blocks are dropped during Anthropic streaming replay

3 participants