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 + tool use: signature not preserved in message history #6176
When using Claude models with extended thinking (e.g., claude-opus-4-5-thinking, claude-4-5-sonnet with thinking enabled) via custom providers (like oh-my-opencode's Antigravity), multi-turn conversations with tool use fail with:
messages.X.content.0.type: Expected `thinking` or `redacted_thinking`, but found `tool_use`.
When `thinking` is enabled, a final `assistant` message must start with a thinking block
(preceeding the lastmost set of `tool_use` and `tool_result` blocks).
Root Cause
Claude's API requires:
When thinking is enabled, every assistant message must start with a thinking block
Thinking blocks in message history MUST include the signature field (returned by Claude in original response)
The signature appears to not be preserved when storing assistant messages in OpenCode's message history. When the message is sent back in subsequent requests, the thinking block is either:
The signature field should be preserved in message history so it can be sent back in subsequent requests.
Workaround
For oh-my-opencode users, I've submitted a PR that strips unsigned thinking blocks from history to prevent the API error: code-yeongyu/oh-my-openagent#253
This is a workaround, not a fix - the proper fix is to preserve signatures in OpenCode's message storage.
Environment
OpenCode version: 1.0.x (latest)
Provider: Custom (oh-my-opencode Antigravity)
Model: claude-opus-4-5-thinking, claude-4-5-sonnet with thinking
3. ReasoningPart Schema (line 80-91) - Has metadata field:
exportconstReasoningPart=PartBase.extend({type: z.literal("reasoning"),text: z.string(),metadata: z.record(z.string(),z.any()).optional(),// ← Defined but not stored!time: z.object({start: z.number(),end: z.number().optional()}),})
Evidence: Stored Parts Missing Metadata
Checked actual stored reasoning parts in ~/.local/share/opencode/storage/part/:
{
"id": "prt_b57dc1066001...",
"type": "reasoning",
"text": "The user is clarifying...",
"time": { "start": 1766705598566, "end": 1766705620131 }
// NO metadata field! Signature is lost!
}
Signature Event Details
The signature arrives via a special stream event from @ai-sdk/anthropic:
Timing: The signature arrives in a late reasoning-delta event after all text. If Session.updatePart is called but storage doesn't persist metadata, it's lost.
Zod parsing: The fn() wrapper uses schema.parse() which validates against the schema. The schema DOES include metadata, but something may be stripping it.
Storage write: Need to verify that Storage.write() correctly serializes the metadata field.
Suggested Investigation
Add debug logging in processor.ts to verify value.providerMetadata is populated when signature arrives
Check if Session.updatePart correctly persists the metadata field
Verify the stored JSON file immediately after a reasoning block completes
Workaround in oh-my-opencode
I've identified a fix in oh-my-opencode's Antigravity adapter that preserves signatures when converting message formats. PR forthcoming.
Related Code Locations
File
Lines
Purpose
session/processor.ts
58-97
Stream event handling
session/message-v2.ts
80-91, 531-536
ReasoningPart schema & conversion
session/index.ts
371-380
updatePart function
provider/transform.ts
123-138
Anthropic-specific normalization
Let me know if you'd like me to submit a PR with debug logging to help trace where exactly the metadata is being lost.
Namespace conversion - transformCandidateThinking() and transformAnthropicThinking() now wrap signatures in providerMetadata.anthropic.signature format that AI SDK expects
Streaming signature capture - Added extractSignatureFromStreamChunk() to capture signatures from SSE chunks during streaming, stored via setThoughtSignature() on stream flush
Message converter - convertContentToParts() now handles thinking, redacted_thinking, and reasoning parts with proper signature preservation
The fix ensures signatures flow correctly through the entire pipeline:
Antigravity Response → thoughtSignature captured →
wrapped in providerMetadata.anthropic.signature →
OpenCode stores in metadata →
retrieved for next request →
injected back into Claude API request
Activity
github-actions commentedon Dec 26, 2025
This issue might be a duplicate of existing issues. Please check:
thinkingorredacted_thinking, but foundtext." #2599: [bug] v0.8.0 Introduced thinking block bug with Claude Sonnet 4 - Same error about thinking blocks not being preserved properly in message history after tool callsthinkingorredacted_thinking, but foundtool_use#3077: Expectedthinkingorredacted_thinking, but foundtool_use- Related issue with the same API error when using thinking with tool callsFeel free to ignore if none of these address your specific case.
codewithkenzo commentedon Dec 26, 2025
Deep Dive Analysis Complete
I've traced the complete data flow and found the exact locations where signatures are handled (and potentially lost).
Data Flow Summary
Key Findings in OpenCode
1. processor.ts (lines 58-97) - Stream event handling:
2. message-v2.ts (lines 531-536) - Retrieval:
3. ReasoningPart Schema (line 80-91) - Has metadata field:
Evidence: Stored Parts Missing Metadata
Checked actual stored reasoning parts in
~/.local/share/opencode/storage/part/:{ "id": "prt_b57dc1066001...", "type": "reasoning", "text": "The user is clarifying...", "time": { "start": 1766705598566, "end": 1766705620131 } // NO metadata field! Signature is lost! }Signature Event Details
The signature arrives via a special stream event from
@ai-sdk/anthropic:This is then remapped by AI SDK (dist/index.js ~line 5185):
Potential Issues
Timing: The signature arrives in a late
reasoning-deltaevent after all text. IfSession.updatePartis called but storage doesn't persistmetadata, it's lost.Zod parsing: The
fn()wrapper usesschema.parse()which validates against the schema. The schema DOES includemetadata, but something may be stripping it.Storage write: Need to verify that
Storage.write()correctly serializes themetadatafield.Suggested Investigation
processor.tsto verifyvalue.providerMetadatais populated when signature arrivesSession.updatePartcorrectly persists themetadatafieldWorkaround in oh-my-opencode
I've identified a fix in oh-my-opencode's Antigravity adapter that preserves signatures when converting message formats. PR forthcoming.
Related Code Locations
session/processor.tssession/message-v2.tssession/index.tsprovider/transform.tsLet me know if you'd like me to submit a PR with debug logging to help trace where exactly the metadata is being lost.
rekram1-node commentedon Dec 26, 2025
opencode already persists them, id have to see your plugin code for antigravity I think the loss of them is there
codewithkenzo commentedon Dec 26, 2025
Update: Verified OpenCode persists metadata correctly ✅
@rekram1-node you're right - OpenCode does persist metadata. I found tool parts with
metadata.google.thoughtSignaturestored properly.However, the issue is in oh-my-opencode's Antigravity adapter transformation:
The Bug
In
transformCandidateThinking():But AI SDK expects:
Evidence
Found in stored parts:
metadata.google.thoughtSignature✅Root Cause Chain
{ thought: true, thoughtSignature: "..." }(Google format){ type: "reasoning", thoughtSignature: "..." }providerMetadata.anthropic.signatureFix Required (in oh-my-opencode)
Will update our PR #253 with this fix.
codewithkenzo commentedon Dec 26, 2025
Fix Submitted
Created comprehensive fix PR: code-yeongyu/oh-my-openagent#256
This PR addresses all the issues identified:
Namespace conversion -
transformCandidateThinking()andtransformAnthropicThinking()now wrap signatures inproviderMetadata.anthropic.signatureformat that AI SDK expectsStreaming signature capture - Added
extractSignatureFromStreamChunk()to capture signatures from SSE chunks during streaming, stored viasetThoughtSignature()on stream flushMessage converter -
convertContentToParts()now handlesthinking,redacted_thinking, andreasoningparts with proper signature preservationThe fix ensures signatures flow correctly through the entire pipeline:
rekram1-node commentedon Dec 26, 2025
Im going to close the issue here since it isn't an opencode issue
fix(antigravity): complete thinking block signature preservation (#256)