Python: fix: skip orphan Anthropic thinking signatures#5784
Open
he-yufeng wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
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 skiptext_reasoningitems withtext is None, optionally attachingprotected_dataas asignatureon the immediately precedingthinkingblock 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. |
38b51d1 to
cf1f8e4
Compare
eavanvalkenburg
approved these changes
May 18, 2026
Contributor
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
fbd5e3f to
6abe3b3
Compare
Contributor
Author
|
Rebased this branch onto current Validation: The first local run without |
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.
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.