Skip to content

fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23574

Closed
bbartels wants to merge 2 commits intoanomalyco:devfrom
bbartels:dev
Closed

fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23574
bbartels wants to merge 2 commits intoanomalyco:devfrom
bbartels:dev

Conversation

@bbartels
Copy link
Copy Markdown
Contributor

@bbartels bbartels commented Apr 20, 2026

Issue for this PR

Closes #14332

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Fixes the Anthropic API error: messages.X.content.Y: 'thinking' or 'redacted_thinking' blocks in the latest assistant message cannot be modified.
The Anthropic API requires that thinking and redacted_thinking blocks in assistant messages are sent back byte-for-byte identical in subsequent requests. Three code paths in normalizeMessages / applyCaching in src/provider/transform.ts were inadvertently modifying these blocks:

  1. Empty content filtering removed redacted_thinking blocks. The AI SDK represents these as { type: "reasoning", text: "", providerOptions: { anthropic: { redactedData: "..." } } } — the empty text caused them to be filtered out. Fix: only filter reasoning parts that have no providerOptions.
  2. Tool-use reordering split assistant messages containing [thinking, tool_call, text] into two separate messages, changing which message the thinking blocks belong to. Fix: skip the reorder when reasoning blocks are present.
  3. Cache control hints could be applied to a reasoning block if it was the last content block in the message. Fix: find the last non-reasoning block for cache annotations instead.

How did you verify your code works?

Added tests covering all three fixes to test/provider/transform.test.ts. All 143 tests pass (130 existing + 13 new).
bun test test/provider/transform.test.ts
143 pass, 0 fail

Screenshots / recordings

N/A - not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 20, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • No issue referenced. Please add Closes #<number> linking to the relevant issue.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions
Copy link
Copy Markdown
Contributor

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

Based on my search, here are potentially related PRs that address similar issues with Anthropic thinking/redacted_thinking blocks:

Potentially Related PRs:

  1. PR fix(opencode): preserve reasoning providerMetadata across model switches #23104 - "fix(opencode): preserve reasoning providerMetadata across model switches"

    • Relates to preserving reasoning blocks across transformations
  2. PR fix(provider): skip empty-text filtering for assistant messages in normalizeMessages (#16748) #16750 - "fix(provider): skip empty-text filtering for assistant messages in normalizeMessages (normalizeMessages() removes empty text parts between reasoning blocks, invalidating Anthropic thinking block signatures #16748)"

    • Directly addresses the empty-text filtering issue mentioned in your PR as one of the three fixes
  3. PR fix(provider): preserve assistant message content when reasoning blocks present #21370 - "fix(provider): preserve assistant message content when reasoning blocks present"

    • Also deals with preserving reasoning blocks during message transformations
  4. PR fix: replace empty text in reasoning messages to preserve thinking block positions #21860 - "fix: replace empty text in reasoning messages to preserve thinking block positions"

    • Addresses similar empty text handling in reasoning messages
  5. PR fix: preserve thinking block signatures and fix compaction headroom asymmetry #14393 - "fix: preserve thinking block signatures and fix compaction headroom asymmetry"

    • Related to thinking block preservation

These PRs appear to be addressing similar or overlapping issues with Anthropic thinking/reasoning block preservation. I'd recommend reviewing them to check if any address the same issues or if this PR supersedes any of them.

@github-actions
Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@bbartels
Copy link
Copy Markdown
Contributor Author

@rekram1-node should be a fairly straightforward set of changes. Runtime changes are small, tests make up main part of the changes

@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Apr 20, 2026
@github-actions github-actions Bot closed this Apr 20, 2026
@bainos
Copy link
Copy Markdown
Contributor

bainos commented Apr 23, 2026

@bbartels would you mind reopening? Closes #14332 has been added to the description? The fix looks correct and is needed — see related discussion in #23104.

@bbartels
Copy link
Copy Markdown
Contributor Author

@bbartels would you mind reopening? Closes #14332 has been added to the description? The fix looks correct and is needed — see related discussion in #23104.

Already created another one #23755

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Amazon Bedrock Opus 4.6 compaction failure

2 participants