opencode
opencode copied to clipboard
fix: strip incompatible thinking blocks when switching to Claude
Fixes #6418
Problem
When switching from models like GLM 4.7 or MiniMax to Claude with extended thinking enabled, users get API errors:
messages.X.content.0: Invalid 'signature' in 'thinking' block
or
messages.X.content.0.type: Expected 'thinking' or 'redacted_thinking', but found 'text'
This happens because:
- Other models produce
thinkingblocks with signatures that are incompatible with Claude's signature validation - Claude requires the last assistant message to start with a valid thinking block when thinking is enabled
-
reasoningparts from other models don't have the signature format Claude expects
Solution
In normalizeMessages(), when the target model is Claude:
-
Convert thinking blocks with signatures to wrapped text - Preserves the reasoning content while removing the invalid signature. Wrapped in
<assistant_thinking>tags. -
Convert reasoning parts to text - Same approach, wrapped in
<assistant_reasoning>tags. - Track which messages had thinking blocks converted - Using a Set to avoid incorrectly removing messages that we already handled.
- Only remove last assistant message if safe - Only when it doesn't start with valid thinking, has no tool calls, AND didn't have converted thinking blocks.
This approach preserves tool_use/tool_result pairing while still handling the signature validation issues.
Changes
-
packages/opencode/src/provider/transform.ts: Added thinking block conversion logic in Claude-specific section -
packages/opencode/test/provider/thinking-blocks.test.ts: New test file with comprehensive coverage -
packages/opencode/test/provider/transform.test.ts: Updated existing test expectation
Testing
- Start a session with GLM 4.7 or MiniMax (models that generate thinking/reasoning)
- Send a few messages, including one that triggers tool use
- Switch to Claude with extended thinking enabled
- Send a message - should no longer get signature errors
- Tool results should still work correctly
Related
- Supersedes #6748 (similar approach but that PR has been stale)
- Related to #7779 (GLM 4.7 thinking format issues)