Skip to content

Conversation

@d-init-d
Copy link

@d-init-d d-init-d commented Feb 3, 2026

Summary

Fixes #6418 - Improves upon #8958 with additional bug fixes and edge case handling.

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

This happens because other models produce thinking blocks with signatures that are incompatible with Claude's signature validation.

Solution

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

  1. Add isValidClaudeSignature() helper - Validates if a signature is a valid Claude/Anthropic signature (starts with 'ErUB')
  2. Extract normalizeClaudeThinkingBlocks() - Dedicated function for better code organization and maintainability
  3. Convert ONLY invalid signatures - Valid Claude thinking blocks are preserved, only incompatible ones from GLM/MiniMax/etc are converted
  4. Use distinct tags - <assistant_thinking> for thinking blocks, <assistant_reasoning> for reasoning blocks
  5. Handle empty content - Filter out messages with empty content after processing
  6. Preserve devstral support - Maintains the devstral check in Mistral normalization

Improvements over PR #8958

Issue PR #8958 This PR
Devstral support ❌ Removed ✅ Preserved
Valid Claude signatures ❌ Converted ✅ Preserved
Tag consistency ❌ Uses same tag for both ✅ Distinct tags
Empty content handling ❌ Not handled ✅ Filtered out
Type safety ❌ Many any casts ✅ Better types
Code organization ❌ Inline ✅ Extracted helper

Changes

  • packages/opencode/src/provider/transform.ts:

    • Added isValidClaudeSignature() helper function with JSDoc
    • Added normalizeClaudeThinkingBlocks() with detailed documentation
    • Updated normalizeMessages() to use new helper for Claude
    • Preserved devstral support in Mistral check
  • packages/opencode/test/provider/thinking-blocks.test.ts:

    • Comprehensive test coverage including:
      • Valid Claude signature preservation
      • Invalid signature conversion
      • Empty thinking block handling
      • Multiple thinking blocks
      • Tool pairing preservation
      • Tool call ID normalization

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
  6. Try with devstral model - should still work

Related

Fixes anomalyco#6418

## Problem
When switching from models like GLM 4.7 or MiniMax to Claude with
extended thinking enabled, users get API errors because other models
produce thinking blocks with signatures incompatible with Claude.

## Solution
1. Add `isValidClaudeSignature()` helper to detect valid Claude signatures
2. Extract `normalizeClaudeThinkingBlocks()` for better code organization
3. Convert ONLY invalid signatures - preserve valid Claude thinking blocks
4. Use distinct `<assistant_reasoning>` tags for reasoning blocks
5. Filter out messages with empty content after processing
6. Preserve devstral support in Mistral check

## Improvements over PR anomalyco#8958
- Only converts thinking blocks with INVALID signatures (not all)
- Uses distinct tags for thinking vs reasoning
- Handles empty content edge case
- Better type safety (reduced `any` usage)
- Extracted helper function for maintainability
- More comprehensive test coverage

## Testing
1. Start session with GLM 4.7 or MiniMax
2. Send messages including tool use
3. Switch to Claude with extended thinking
4. Should no longer get signature errors
5. Tool results still work correctly"
@github-actions
Copy link
Contributor

github-actions bot commented Feb 3, 2026

The following comment was made by an LLM, it may be inaccurate:

Related PRs Found

Important Note: PR #11882 is explicitly listed as the current PR and supersedes the following:

  1. PR fix: strip incompatible thinking blocks when switching to Claude #8958 - fix: strip incompatible thinking blocks when switching to Claude

  2. PR fix: strip incompatible thinking blocks when switching to Anthropic models #6748 - fix: strip incompatible thinking blocks when switching to Anthropic models

  3. PR fix: strip reasoning parts when switching to non-interleaved models #11572 - fix: strip reasoning parts when switching to non-interleaved models

The current PR (#11882) is a direct improvement over #8958 and supersedes it, as stated in the description.

@rekram1-node
Copy link
Collaborator

This seems vibe coded and if so please close, I already merged a fix for this issue.

@srs-adamr
Copy link

This PR also fixes #11991 - same root cause (GLM reasoning parts stripped leaving empty content when switching to Claude).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: Invalid signature in thinking block

3 participants