fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23574
fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms#23574bbartels wants to merge 2 commits intoanomalyco:devfrom
Conversation
|
This PR doesn't fully meet our contributing guidelines and PR template. What needs to be fixed:
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. |
|
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:
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. |
|
Thanks for your contribution! This PR doesn't have a linked issue. All PRs must reference an existing issue. Please:
See CONTRIBUTING.md for details. |
|
@rekram1-node should be a fairly straightforward set of changes. Runtime changes are small, tests make up main part of the changes |
|
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. |
Issue for this PR
Closes #14332
Type of change
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
thinkingandredacted_thinkingblocks in assistant messages are sent back byte-for-byte identical in subsequent requests. Three code paths innormalizeMessages/applyCachinginsrc/provider/transform.tswere inadvertently modifying these blocks:redacted_thinkingblocks. 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 noproviderOptions.[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.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