Skip to content

Python: fix: skip orphan Anthropic thinking signatures#5784

Open
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/anthropic-signature-only-thinking
Open

Python: fix: skip orphan Anthropic thinking signatures#5784
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/anthropic-signature-only-thinking

Conversation

@he-yufeng
Copy link
Copy Markdown
Contributor

Fixes #5783. The Anthropic message serializer no longer emits thinking blocks with thinking set to null when a text_reasoning item only carries protected_data. If the signature immediately follows a thinking block, it is attached to that block; otherwise the orphan signature-only item is skipped before replay. Validation: uv run pytest packages/anthropic/tests/test_anthropic_client.py -k 'text_reasoning or signature_delta' -q; uv run ruff check packages/anthropic/agent_framework_anthropic/_chat_client.py packages/anthropic/tests/test_anthropic_client.py; uv run ruff format --check packages/anthropic/agent_framework_anthropic/_chat_client.py packages/anthropic/tests/test_anthropic_client.py; uv run pyright packages/anthropic/agent_framework_anthropic/_chat_client.py packages/anthropic/tests/test_anthropic_client.py; uv run mypy --config-file pyproject.toml packages/anthropic/agent_framework_anthropic; python -m py_compile; git diff --check. I also tried mypy on the full test file, but it reports existing unrelated type errors in packages/anthropic/tests/test_anthropic_client.py.

Copilot AI review requested due to automatic review settings May 12, 2026 17:47
@github-actions github-actions Bot changed the title fix: skip orphan Anthropic thinking signatures Python: fix: skip orphan Anthropic thinking signatures May 12, 2026
Copy link
Copy Markdown
Contributor

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 fixes an Anthropic provider serialization bug where a signature-only text_reasoning content item (i.e., text=None with protected_data) could be emitted as an invalid Anthropic thinking block with thinking: null. The change ensures signature deltas are either attached to the preceding thinking block (when applicable) or skipped so replayed message history remains valid (per #5783).

Changes:

  • Update _prepare_message_for_anthropic() to skip text_reasoning items with text is None, optionally attaching protected_data as a signature on the immediately preceding thinking block when it has no signature.
  • Add unit tests covering (1) signature attachment to a prior thinking block and (2) skipping an orphan signature-only reasoning item before a tool call.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/anthropic/agent_framework_anthropic/_chat_client.py Prevents emitting invalid thinking: null blocks by attaching or skipping signature-only reasoning items during message preparation.
python/packages/anthropic/tests/test_anthropic_client.py Adds regression tests for signature-only reasoning attachment and orphan skipping behavior.

@he-yufeng he-yufeng force-pushed the fix/anthropic-signature-only-thinking branch from 38b51d1 to cf1f8e4 Compare May 13, 2026 10:44
@moonbox3
Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/anthropic/agent_framework_anthropic
   _chat_client.py4463592%461, 464, 545, 638, 640, 783, 819–820, 898, 900, 930–931, 976, 992–993, 1000–1002, 1006–1008, 1012–1015, 1129, 1139, 1191, 1339–1340, 1357, 1370, 1383, 1408–1409
TOTAL33944390988% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
6704 30 💤 0 ❌ 0 🔥 1m 50s ⏱️

@he-yufeng he-yufeng force-pushed the fix/anthropic-signature-only-thinking branch from fbd5e3f to 6abe3b3 Compare May 21, 2026 05:12
@he-yufeng
Copy link
Copy Markdown
Contributor Author

Rebased this branch onto current upstream/main.

Validation:

$env:PYTHONPATH = "$(Resolve-Path python\packages\core);$(Resolve-Path python\packages\anthropic)"
python -m pytest python\packages\anthropic\tests\test_anthropic_client.py -q --basetemp .tmp\pytest-5784

python -m py_compile python\packages\anthropic\agent_framework_anthropic\_chat_client.py python\packages\anthropic\tests\test_anthropic_client.py
git diff --check

The first local run without PYTHONPATH failed during collection because the monorepo core package was not on sys.path; the command above is the passing run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python Anthropic: signature-only thinking blocks replay as invalid null thinking

4 participants