GeezerVison is showing github.com — fix(provider): preserve redacted_thinking blocks and fix signature validation by DonaldoDes · Pull Request #12131 · anomalyco/opencode
Conversation
Summary
Fix "Invalid data in redacted_thinking block" errors when using extended thinking with Claude.
Problem
When extended thinking is enabled, the Anthropic API returns thinking and redacted_thinking blocks with cryptographic signatures. Two issues caused these blocks to be rejected on subsequent API calls:
trimEnd() on thinking text — The signature is computed on the exact text including trailing whitespace. Trimming invalidates it.
- Block ordering — The API requires thinking/redacted_thinking blocks to appear first in assistant message content arrays.
Changes
- processor.ts: Remove
trimEnd() call on thinking text to preserve signature integrity
- transform.ts: Add reordering logic to place reasoning blocks first in
normalizeMessages()
- message-v2.ts: Preserve all reasoning blocks including redacted_thinking in
toModelMessages()
Testing
Reproduced the error by enabling extended thinking (High) with Claude Opus 4.5, triggering the error on the 2nd message of a session. After the fix, multi-turn conversations with extended thinking work without errors.
Closes #10970
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
The following comment was made by an LLM, it may be inaccurate:
Based on my search, I found related PRs that deal with thinking/reasoning blocks, but they appear to be addressing different issues:
Related PRs (not exact duplicates):
-
fix: strip incompatible thinking blocks when switching to Claude (improved) #11882 - "fix: strip incompatible thinking blocks when switching to Claude (improved)"
- Related to handling thinking blocks but focuses on stripping them when switching models, not preserving them
-
fix: strip reasoning parts when switching to non-interleaved models #11572 - "fix: strip reasoning parts when switching to non-interleaved models"
- Similar theme of handling reasoning blocks during model switching
-
fix: strip incompatible thinking blocks when switching to Claude #8958 - "fix: strip incompatible thinking blocks when switching to Claude"
- Earlier version addressing thinking block compatibility
These are related to thinking/reasoning blocks but address model switching scenarios, whereas PR #12131 focuses on preserving the integrity of redacted_thinking blocks during message processing and multi-turn conversations.
No exact duplicate PRs found - PR #12131 appears to be the only one addressing the specific issue of signature validation and block ordering for extended thinking with Claude.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went through an AI assisted code review of this PR, as I've faced similar issues with OpenRouter models and was hoping this may help indirectly. Unfortunately, it appears it doesn't, however, it also doesn't impact other model providers. Here is the summary of my AI assisted code review:
Code Review: PR #12131 - Fix "Invalid data in redacted_thinking block" Errors
Summary
This PR fixes signature validation errors when using extended thinking with Claude by removing overly strict handling of redacted_thinking blocks.
Changes Overview
1. packages/opencode/src/provider/transform.ts
Lines 44-85 - normalizeMessages() function:
- Removed: Special handling for preserving
redacted_thinking blocks (blocks with providerMetadata?.anthropic)
- Removed: Reordering of content blocks to place reasoning first for Anthropic
- Rationale: The previous strict handling was causing signature validation errors when replaying assistant messages
2. packages/opencode/src/session/message-v2.ts
Lines 586-596 - toModelMessages() function:
- Removed: Comment about preserving ALL reasoning parts including redacted_thinking blocks
- Rationale: The comment was documenting behavior that was causing issues
3. packages/opencode/src/session/processor.ts
Lines 88-104 - reasoning-end case:
- Changed: Removed comment about NOT trimmingEnd() thinking text
- Changed: Added
part.text = part.text.trimEnd() to trim trailing whitespace
- Rationale: The signature is computed on the exact text including trailing whitespace; modifying it invalidates the signature
4. packages/opencode/src/session/prompt.ts
Lines 968-991 - File handling:
- Changed: Added
.catch(() => undefined) to Bun.file(filepath).stat()
- Changed: Added optional chaining
stat?.isDirectory() for safer directory check
- Changed: Added
.catch(() => []) to LSP.documentSymbol(filePathURI)
- Rationale: Prevents failures when file parts reference missing files
5. packages/opencode/test/session/prompt-missing-file.test.ts
New file: Test to ensure prompt does not fail when a file part is missing
6. packages/app/e2e/actions.ts & packages/app/e2e/session/session.spec.ts
UI/UX improvements: More robust session menu interactions and state management
Impact Analysis
Affected Providers
- Anthropic models (including Claude on Bedrock): These changes directly affect Anthropic models by removing strict
redacted_thinking block handling
NOT Affected Providers
- OpenRouter: Has separate handling in
variants() and providerOptions() functions
- OpenAI: Has its own interleaved reasoning handling
- Google Gemini: Has its own interleaved reasoning handling
- Mistral: Has its own tool call ID normalization
- GitHub Copilot: Has its own provider-specific handling
Testing
- New test added:
prompt-missing-file.test.ts verifies graceful handling of missing files
- Existing E2E tests updated for more robust session menu interactions
Concerns
- Breaking change for Anthropic: Removing the strict
redacted_thinking block handling may affect existing sessions that rely on this behavior
- Trailing whitespace: Trimming thinking text may affect signature validation if the API expects exact text
Recommendation
Approve with caution. The changes fix a known issue with extended thinking on Claude, but the removal of strict redacted_thinking block handling should be monitored for any unexpected side effects.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
DonaldoDes reacted with thumbs up emoji
The Anthropic API computes a cryptographic signature on the exact
thinking text including trailing whitespace. Calling trimEnd()
invalidates the signature, causing "Invalid data in
redacted_thinking block" errors when thinking blocks are replayed
in subsequent API calls.
Closes anomalyco#10970
The Anthropic API requires thinking/redacted_thinking blocks to
appear before other content blocks in assistant messages. Add
reordering logic in normalizeMessages() and preserve all reasoning
blocks including redacted_thinking in toModelMessages().
Ref anomalyco#10970
|
@thdxr @adamdotdevin (just tagging some subset of contributors who seem like they're working on opencode)
Can this PR get reviewed and merged? We've been encountering similar issues with redacted thinking block errors for a few weeks, and spent some time triaging it to discover the same bugs as described here still present in the latest version. There's also this PR related to thinking block issues when compaction happens: #14393
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Mar 9, 2026
This was referenced Apr 17, 2026
|
Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Claude Models Thinking block error
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|