fix: strip incompatible thinking blocks when switching to Claude (improved)#11882
fix: strip incompatible thinking blocks when switching to Claude (improved)#11882d-init-d wants to merge 1 commit intoanomalyco:devfrom
Conversation
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"
|
The following comment was made by an LLM, it may be inaccurate: Related PRs FoundImportant Note: PR #11882 is explicitly listed as the current PR and supersedes the following:
The current PR (#11882) is a direct improvement over #8958 and supersedes it, as stated in the description. |
|
This seems vibe coded and if so please close, I already merged a fix for this issue. |
|
This PR also fixes #11991 - same root cause (GLM reasoning parts stripped leaving empty content when switching to Claude). |
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:
This happens because other models produce
thinkingblocks with signatures that are incompatible with Claude's signature validation.Solution
In
normalizeMessages(), when the target model is Claude:isValidClaudeSignature()helper - Validates if a signature is a valid Claude/Anthropic signature (starts with 'ErUB')normalizeClaudeThinkingBlocks()- Dedicated function for better code organization and maintainability<assistant_thinking>for thinking blocks,<assistant_reasoning>for reasoning blocksImprovements over PR #8958
anycastsChanges
packages/opencode/src/provider/transform.ts:isValidClaudeSignature()helper function with JSDocnormalizeClaudeThinkingBlocks()with detailed documentationnormalizeMessages()to use new helper for Claudepackages/opencode/test/provider/thinking-blocks.test.ts:Testing
Related