GeezerVison is showing github.com — fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms by bbartels · Pull Request #23755 · anomalyco/opencode
Conversation
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:
- 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.
- 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.
- 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
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
thyecust, bainos, jahkeup, jonathan-mayer, bradkk-safer, and ysm-dev reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@rekram1-node Any chance you could have a look? ~20 LOC of actual changes, rest is tests
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced May 22, 2026
|
Confirmed this fixes the three transform.ts paths cleanly — verified against messages.X.content.Y: 'thinking' or 'redacted_thinking' blocks in the latest assistant message cannot be modified on claude-opus-4-x (extended thinking, tool-heavy multi-turn).
One gap worth folding in: message-v2.ts strips the thinking signature before transform.ts ever runs, on a path this PR doesn't touch. In toModelMessagesEffect, the reasoning branch is roughly:
if (part.type === "reasoning") {
if (differentModel) { /* demote to text, drop providerMetadata */ }
// providerMetadata only kept when !differentModel
}
with differentModel = ${model.providerID}/${model.id} !== ${msg.info.providerID}/${msg.info.modelID}. For Anthropic thinking blocks the providerMetadata carries the cryptographic signature, so dropping it on differentModel re-triggers the exact same cannot be modified 400 on the next turn — even with this PR's transform.ts fix applied.
It reliably reproduces whenever provider/model identity shifts between turns: fallback-model switches, variant changes, and proxy setups where the stored providerID differs from the live one (we hit it through a proxy front where requests resolve under a different provider id than what's persisted in the session). Stripping text/tool metadata there is correct; stripping thinking metadata is not.
Minimal fix that pairs with this PR:
if (part.type === "reasoning") {
const isAnthropicReasoning =
model.providerID === "anthropic" ||
model.providerID.includes("anthropic") ||
model.api?.npm === "@ai-sdk/anthropic"
// keep the signature for Anthropic even across differentModel
...(differentModel && !isAnthropicReasoning ? {} : { providerMetadata: part.metadata })
}
Happy to open a small follow-up PR (rebased on this) if useful.
Disclosure: investigation + draft assisted by an AI agent; reproduction and the message-v2.ts gap were validated against a live multi-provider (CLIProxyAPI) deployment.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Heads-up on the empty-reasoning filter in this PR — the providerOptions != null check is too loose and still lets the 400 through in one case:
if (part.type === "reasoning") {
return part.text !== "" || part.providerOptions != null
}
With Anthropic interleaved thinking across several tool-call rounds, a turn that is cut off (e.g. output-length / max-tokens) can leave trailing reasoning parts that have empty text and providerOptions: {} (an empty object — still != null). Those get kept and re-sent as malformed thinking blocks, and Anthropic rejects the next turn with the very error this PR targets:
messages.N.content.M: `thinking` or `redacted_thinking` blocks in the latest assistant message cannot be modified.
We hit this in production after applying an equivalent of this filter: the error recurred on a long Opus extended-thinking + multi-tool session. Tightening the predicate to require an actual signature/redactedData fixed it:
if (part.type === "reasoning") {
if (part.text !== "") return true
const po = part.providerOptions ?? {}
// keep ONLY genuine redacted_thinking / signature-only blocks
return Object.values(po).some((v) => v != null && (v.signature != null || v.redactedData != null))
}
Empty reasoning with {} / no signature is a truncated remnant and should be dropped, not replayed. Might be worth folding into this PR. (Related: #30046 fixes the upstream message-v2.ts strip that drops the signature on differentModel.)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Jun 24, 2026
Reviewers
Hona
Awaiting requested review from Hona
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
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.
|