fix(anthropic): demote reordered thinking signatures on interleaved multi-thinking turns#35976
Open
fesalfayed wants to merge 1 commit into
Open
Conversation
…ulti-thinking turns Extended-thinking models interleave thinking and tool_use blocks (thinking, tool_use, thinking, ...) and sign each thinking block against its original position. _convert_assistant_message rebuilds the turn as [all thinking][text][all tool_use], so any latest turn with 2+ thinking blocks alongside tool_use is reordered and its signatures are dead; replaying them returns a non-retryable HTTP 400 (the reported content.N lands in the tool_use region). reasoning_details carries no positional anchor to re-interleave from, so demote those thinking blocks to text in _manage_thinking_signatures, preserving the reasoning. A single leading thinking block is unaffected (its position is unchanged).
9 tasks
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 does this PR do?
Extended-thinking Claude models interleave
thinkingandtool_useblocks (thinking, tool_use, thinking, tool_use, …) and sign eachthinkingblock against its original position in that sequence._convert_assistant_messagerebuilds the assistant turn as[all thinking][text][all tool_use], so any latest turn carrying 2+ thinking blocks alongside tool_use is reordered and its signatures no longer match. Replaying them returns a non-retryable HTTP 400:Since the turn is rebuilt from the store every iteration, the gateway loops on it with no recovery. The reported
content.Mlands in the tool_use region (Anthropic is validating the original interleaved layout), which is the tell that this is a reorder rather than the orphan-strip case fixed in #35859.reasoning_detailscarries no positional anchor to re-interleave the blocks from, so faithful reconstruction is impossible. This extends the existing dead-signature handling in_manage_thinking_signatures: when the latest turn has 2+ thinking blocks and any tool_use, demote those thinking blocks to text (reasoning preserved) instead of replaying dead signatures. A single leading thinking block is unaffected — its position is unchanged, so its signature is still valid.This is distinct from #35847/#35859 (orphan-stripped tool_use): there are no orphans here — every tool_use is answered — so that fix does not cover it.
Related Issue
Fixes #35975
Type of Change
Changes Made
agent/anthropic_adapter.py—_manage_thinking_signatures: mark the latest turn's signature dead (→ demote thinking to text) when it has 2+ thinking blocks alongside tool_use.tests/agent/test_anthropic_adapter.py— two tests: interleaved multi-thinking demotion, and a single-thinking + tool_use no-over-fire guard.How to Test
(21 passed locally — the 2 new tests plus the existing thinking/signature/orphan suite, confirming no over-fire regression on
test_signed_thinking_preserved_on_last_turn.)Checklist
Code
Documentation & Housekeeping