GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms by bbartels · Pull Request #23755 · anomalyco/opencode

Conversation

@bbartels

@bbartels bbartels commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

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

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
All reactions
  • 👍 6 reactions
bbartels added 3 commits April 20, 2026 16:21
@bbartels
Refactor content filtering and annotation logic
1c19eaf
@bbartels
Update transform.test.ts
244b228
@bbartels
Merge branch 'anomalyco:dev' into dev
e0da282
@github-actions github-actions Bot added the contributor label Apr 21, 2026
@github-actions

github-actions Bot commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

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

Potential Duplicate/Related PRs Found:

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

    • Directly related to the empty content filtering issue addressed in the current PR
  2. PR fix(provider): preserve assistant message content when reasoning blocks present #21370 - fix(provider): preserve assistant message content when reasoning blocks present

    • Addresses similar issue of preserving assistant messages when reasoning blocks are present
  3. PR fix(provider): preserve redacted_thinking blocks and fix signature validation #12131 - fix(provider): preserve redacted_thinking blocks and fix signature validation

    • Related to preserving redacted_thinking block integrity
  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

    • Similar fix for preserving thinking block positions with empty text handling

These PRs appear to be addressing overlapping issues with preserving thinking/reasoning blocks in Anthropic message transforms. The current PR (23755) consolidates fixes for three specific code paths (empty content filtering, tool-use reordering, and cache control hints) that were inadvertently modifying these blocks.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

bbartels and others added 12 commits April 21, 2026 23:04
@bbartels
Merge branch 'dev' into dev
af56b5c
@bbartels
Merge branch 'dev' into dev
ab5fb4d
@rekram1-node
feat: update codex plugin to support 5.5 (anomalyco#23789)
97f3c74
@Hona
chore: bump Bun to 1.3.13 (anomalyco#23791)
69e2f3b
@Brendonovich
fix(app): improve icon override handling in project edit dialog (anom…
a45d9a9
…alyco#23768)
@opencode-agent
chore: update nix node_modules hashes
ed3d364
@Hona
fix: preserve BOM in text tool round-trips (anomalyco#23797)
bb69648
@opencode-agent
chore: generate
bfb954e
@Hona
test: fix cross-spawn stderr race on Windows CI (anomalyco#23808)
0595c28
@opencode-agent
chore: generate
6aa475f
@Brendonovich
fix: consolidate project avatar source logic (anomalyco#23819)
88c5f6b
@bbartels
Merge branch 'dev' into dev
47d3411
@bbartels

bbartels commented Apr 22, 2026

Copy link
Copy Markdown
Contributor Author

@rekram1-node Any chance you could have a look? ~20 LOC of actual changes, rest is tests

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rekram1-node
rekram1-node force-pushed the dev branch from 1026791 to ac26394 Compare April 23, 2026 04:27
@rekram1-node
rekram1-node requested a review from adamdotdevin as a code owner April 23, 2026 04:27
@bbartels bbartels mentioned this pull request Apr 23, 2026
fix: preserve thinking/redacted_thinking blocks in Anthropic message transforms #23574
Closed
3 of 6 tasks
@bainos bainos mentioned this pull request Apr 23, 2026
fix(opencode): preserve reasoning providerMetadata across model switches #23104
Closed
3 tasks done
@bbartels bbartels mentioned this pull request May 5, 2026
fix(provider): preserve anthropic provider-executed tool pairs #25861
Closed
This was referenced May 22, 2026
Bug: OpenAI Responses API rejects assistant messages containing internal reasoning / step-* parts #28839
Closed
Bug: OpenAI provider fails with GPT-5.4 after reasoning/todowrite in v1.15.7 (OpenAI Responses assistant messages only support text and tool-call content) #28750
Closed
@codeg-dev

codeg-dev commented May 31, 2026

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@codeg-dev codeg-dev mentioned this pull request May 31, 2026
fix(session): preserve Anthropic thinking signature across differentModel #30046
Closed
3 of 6 tasks
@codeg-dev

codeg-dev commented May 31, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Jun 6, 2026
fix(session): skip empty text parts in assistant message replay #31045
Closed
3 of 6 tasks
@luiscga luiscga mentioned this pull request Jun 6, 2026
Empty text parts in assistant message history cause 422 on strict providers #31046
Closed
@github-actions github-actions Bot mentioned this pull request Jun 7, 2026
fix(provider): drop whitespace-only text blocks for Anthropic/Bedrock #31260
Closed
@thdxr
thdxr requested review from Brendonovich and Hona as code owners June 11, 2026 00:18
@github-actions github-actions Bot mentioned this pull request Jun 12, 2026
fix(opencode): preserve reasoning_content for Moonshot/Kimi tool-call assistant messages #31995
Closed
3 of 6 tasks
This was referenced Jun 24, 2026
fix: add final empty-content guard in message() pipeline #33647
Closed
fix: add final empty-content guard in message() pipeline #34272
Closed
@github-actions github-actions Bot mentioned this pull request Aug 28, 2026
fix(session): preserve Anthropic thinking for errored same-model replay (fixes #38620) #45951
Closed
3 of 6 tasks
@bbartels bbartels closed this Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@adamdotdevin adamdotdevin Awaiting requested review from adamdotdevin
@Hona Hona Awaiting requested review from Hona
@Brendonovich Brendonovich Awaiting requested review from Brendonovich

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

contributor

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Amazon Bedrock Opus 4.6 compaction failure

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@bbartels @codeg-dev @rekram1-node @Hona @Brendonovich
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.