fix(anthropic): validate thinking block requirement and prevent silent reasoning drop#12931
Open
giulio-leone wants to merge 2 commits intovercel:mainfrom
Open
fix(anthropic): validate thinking block requirement and prevent silent reasoning drop#12931giulio-leone wants to merge 2 commits intovercel:mainfrom
giulio-leone wants to merge 2 commits intovercel:mainfrom
Conversation
f8220d0 to
3978add
Compare
Author
|
Intervention note for this PR: Current blocker appears to be Vercel deployment authorization, not code/test correctness. Observed pattern:
Recommended unblock sequence:
If useful, I can follow up with a PR-by-PR status sweep after authorization is completed. |
Author
|
This PR is ready for review — all CI checks pass, no merge conflicts, and all review threads have been resolved. Ready to merge when approved. 🚀 |
added 2 commits
March 5, 2026 21:43
…t reasoning drop Three targeted fixes for Anthropic extended thinking + multi-step tool use: 1. **Thinking block validation**: When thinking is enabled, validate that the last assistant message starts with a thinking/redacted_thinking block before sending to the Anthropic API. Throws a clear SDK error instead of getting a cryptic API error. 2. **Improved drop warnings**: When reasoning parts are dropped due to missing providerOptions/signature metadata, the warning message now explains the Anthropic API requirement and suggests preserving providerOptions through the message round-trip. 3. **Ghost reasoning prevention**: When sendReasoning is false, reasoning-start and reasoning-end events are now properly suppressed (previously only reasoning-delta was gated), preventing empty ReasoningUIPart objects on the client. Fixes vercel#7729
3978add to
abd5e80
Compare
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.
Summary
Three targeted fixes for Anthropic extended thinking + multi-step tool use (#7729).
Problem
When using Anthropic models with extended thinking enabled and multi-step tool use, several failure modes exist:
Cryptic API errors: If the last assistant message doesn't start with a
thinkingblock (Anthropic API requirement), the API returns a vague error. This commonly happens when messages round-trip through the UI layer without preservingproviderOptions.Silent reasoning drop: When
reasoningparts lackproviderOptions/signaturemetadata, the converter silently drops them with a generic warning — leaving developers confused about why their multi-step calls fail.Ghost reasoning parts: When
sendReasoning: falseis set,reasoning-startandreasoning-endevents were still emitted to the UI stream (onlyreasoning-deltawas gated), creating emptyReasoningUIPartobjects on the client.Root Cause Analysis
Through exhaustive tracing of the entire reasoning/thinking block lifecycle:
signature_delta→reasoning-delta(withproviderMetadata.anthropic.signature) → accumulated in stream-text.ts →toResponseMessages()→ convertertoUIMessageStream→process-ui-message-stream→convertToModelMessages→ converterThe standard multi-step flow actually preserves signatures correctly through all layers. The issues are situational:
providerOptionsmetadataprepareStepslicing modifying message ordersendReasoning: falsecreating malformed UI partsFixes
Pre-flight validation (
anthropic-messages-language-model.ts): After prompt conversion, if thinking is enabled, validates that the last assistant message starts with athinkingorredacted_thinkingblock. Throws a clear, actionable error instead of a cryptic Anthropic API error.Improved drop warnings (
convert-to-anthropic-messages-prompt.ts): Warning messages now explain the Anthropic API requirement and suggest preservingproviderOptionsthrough the message round-trip.Ghost reasoning prevention (
stream-text.ts): All three reasoning events (reasoning-start,reasoning-delta,reasoning-end) are now consistently gated behindsendReasoning, preventing empty reasoning parts on the client.Tests
sendReasoning: falseghost reasoning preventionRelated
Fixes #7729 (14 👍, reported by multiple users including framework contributors)