opencode icon indicating copy to clipboard operation
opencode copied to clipboard

fix: strip incompatible thinking blocks when switching to Claude

Open coleleavitt opened this issue 21 hours ago • 2 comments

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:

  1. Other models produce thinking blocks with signatures that are incompatible with Claude's signature validation
  2. Claude requires the last assistant message to start with a valid thinking block when thinking is enabled
  3. reasoning parts from other models don't have the signature format Claude expects

Solution

In normalizeMessages(), when the target model is Claude:

  1. Convert thinking blocks with signatures to wrapped text - Preserves the reasoning content while removing the invalid signature. Wrapped in <assistant_thinking> tags.
  2. Convert reasoning parts to text - Same approach, wrapped in <assistant_reasoning> tags.
  3. Track which messages had thinking blocks converted - Using a Set to avoid incorrectly removing messages that we already handled.
  4. 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

  1. Start a session with GLM 4.7 or MiniMax (models that generate thinking/reasoning)
  2. Send a few messages, including one that triggers tool use
  3. Switch to Claude with extended thinking enabled
  4. Send a message - should no longer get signature errors
  5. 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)

coleleavitt avatar Jan 16 '26 20:01 coleleavitt