You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Claude Extended Thinking: assistant message content order causes API error #9364
When using Claude models with Extended Thinking enabled through a proxy (e.g., via @ai-sdk/anthropic), multi-turn conversations with tool use fail with the following error:
messages.45.content.0.type: Expected 'thinking' or 'redacted_thinking', but found 'tool_use'
Root Cause
Claude Extended Thinking requires that assistant messages must start with a thinking or redacted_thinking block when thinking is enabled. However, OpenCode's message reconstruction does not guarantee this ordering.
Code Analysis
In packages/opencode/src/session/message-v2.ts, the toModelMessage function builds message parts in iteration order:
for(constpartofmsg.parts){if(part.type==="text")/* add text */if(part.type==="tool")/* add tool */if(part.type==="reasoning")/* add reasoning */}
The parts are sorted by their generated ID:
result.sort((a,b)=>(a.id>b.id ? 1 : -1))
In packages/opencode/src/provider/transform.ts, the normalizeMessages function for Anthropic only filters empty content but does not reorder parts to ensure reasoning blocks come first:
if(model.api.npm==="@ai-sdk/anthropic"){msgs=msgs.map((msg)=>{// ... only filters empty text/reasoning partsconstfiltered=msg.content.filter((part)=>{if(part.type==="text"||part.type==="reasoning"){returnpart.text!==""}returntrue})// ...})}
Reproduction Steps
Configure a Claude model with Extended Thinking enabled (e.g., claude-sonnet-4-5 with thinking.budgetTokens > 0)
Start a conversation that triggers tool use
Continue the conversation for multiple turns
Eventually, the API returns the error about unexpected tool_use at position 0
Proposed Fix
Add reordering logic in normalizeMessages for Anthropic models to ensure reasoning parts always come before other content types:
if(model.api.npm==="@ai-sdk/anthropic"){msgs=msgs.map((msg)=>{if(msg.role==="assistant"&&Array.isArray(msg.content)){// Ensure reasoning blocks come first (Claude Extended Thinking requirement)constreasoningParts=msg.content.filter(p=>p.type==="reasoning")constotherParts=msg.content.filter(p=>p.type!=="reasoning")return{ ...msg,content: [...reasoningParts, ...otherParts]}}returnmsg})// ... existing empty content filtering}
Environment
OpenCode version: latest
Provider: Custom proxy using Anthropic-compatible API
Model: Claude models with Extended Thinking enabled
The most relevant duplicate appears to be #6176, which was closed on Dec 26, 2025 but the issue persists. The underlying cause seems to be message content ordering not being enforced properly across different providers and model configurations.
Feel free to ignore if your specific case differs from these.
After reviewing #6176, it appears this may be the same root cause - the Antigravity adapter not correctly converting thoughtSignature to the providerMetadata.anthropic.signature format that AI SDK expects.
My Setup
Plugin: opencode-antigravity-auth@1.3.0
Proxy: CLIProxyAPI (custom proxy)
Provider configured as @ai-sdk/anthropic compatible
Activity
github-actions commentedon Jan 19, 2026
This issue might be a duplicate of existing issues. Please check:
thinkingorredacted_thinking, but foundtool_use#3077: Expectedthinkingorredacted_thinking, but foundtool_use(same error)thinkingorredacted_thinking, but foundtext." #2599: v0.8.0 Introduced thinking block bug with Claude Sonnet 4 (same root cause)thinkingorredacted_thinkingbut foundtool_use#8010: Expectedthinkingorredacted_thinkingbut foundtool_use(AWS Bedrock variant)tool_useerror #8377: Most sessions eventually gets atool_useerror (related message ordering)The most relevant duplicate appears to be #6176, which was closed on Dec 26, 2025 but the issue persists. The underlying cause seems to be message content ordering not being enforced properly across different providers and model configurations.
Feel free to ignore if your specific case differs from these.
Soein commentedon Jan 19, 2026
Update: Related to #6176
After reviewing #6176, it appears this may be the same root cause - the Antigravity adapter not correctly converting
thoughtSignatureto theproviderMetadata.anthropic.signatureformat that AI SDK expects.My Setup
opencode-antigravity-auth@1.3.0@ai-sdk/anthropiccompatiblePossible Causes
transform.tsdoesn't reorder reasoning parts to be firstQuestions
opencode-antigravity-authbased on oh-my-opencode's Antigravity adapter?If this is confirmed as a plugin issue rather than OpenCode core, please feel free to close as duplicate of #6176.
rekram1-node commentedon Jan 21, 2026
Oh if you are using a plugin and getting an issue I highly recommend opening issue w/ them