Skip to content

Conversation

@DonaldoDes
Copy link

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:

  1. trimEnd() on thinking text — The signature is computed on the exact text including trailing whitespace. Trimming invalidates it.
  2. 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

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

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):

  1. 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
  2. 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
  3. 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.

@DonaldoDes DonaldoDes force-pushed the fix/redacted-thinking-blocks branch 2 times, most recently from e962b09 to e255791 Compare February 4, 2026 13:53
Copy link

@cryptictech cryptictech left a 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

  1. Breaking change for Anthropic: Removing the strict redacted_thinking block handling may affect existing sessions that rely on this behavior
  2. 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.

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
@DonaldoDes DonaldoDes force-pushed the fix/redacted-thinking-blocks branch from 25b0b05 to 993c109 Compare February 6, 2026 16:38
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.

Claude Models Thinking block error

2 participants