fix(anthropic): echo thinking-bearing assistant turns verbatim in tool loops#296
Merged
Merged
Conversation
…l loops With adaptive thinking, the Messages API rejects tool-loop follow-ups whose latest assistant message differs from the original response (thinking signatures bind block positions; text/tool_use siblings were rebuilt and thinking blocks hoisted). The signature payload now carries the full native content array when thinking blocks are present — both parse paths — and _init_request re-emits it unchanged. Thinking-only signature payloads (from older versions) keep the rebuild path.
7cedaef to
50e1c87
Compare
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
1 similar comment
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
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.
What
With adaptive thinking enabled, any tool-use follow-up request fails:
The serializer rebuilt the assistant turn from internal fields — thinking blocks hoisted to the front, text re-flattened (only the first text block survived, citations dropped),
tool_usereconstructed. Thinking signatures bind block positions (interleaved thinking), so the API requires the whole turn echoed exactly as received.Fix
For Anthropic, the
signatureround-trip payload now carries the full native content array when the response contains thinking blocks:_parse_reasoningstoresresponse_data["content"]verbatim (non-streaming).providers/anthropic/messages/streaming.py) now also reconstructsthinking/redacted_thinking/tool_useblocks by index;AnthropicTextStream._aggregate_signaturereturns that ordered array when it carries thinking. The modality stream's separate thinking accumulator became redundant and is removed._init_requestemits the array unchanged for such turns and rebuilds nothing. Thinking-only signature payloads (messages persisted by older versions) keep the existing rebuild path, and non-thinking turns are untouched.No changes to
Message/base client/base streaming — everything rides the existing provider-opaquesignaturechannel, same as the google and openresponses round-trips.Verification
make lintclean;make test682 passed; pre-push mypy/ruff/bandit hooks green.[thinking, text, redacted_thinking, tool_use]turn on both parse paths (streamed SSE sequence and non-streaming response): the follow-up request's assistant content is byte-identical to the original array, and legacy/no-thinking turns serialize exactly as before.