Python: fix: skip orphan Anthropic thinking signatures#5784
Conversation
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
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||
fbd5e3f to
6abe3b3
Compare
|
Rebased this branch onto current Validation: The first local run without |
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.