Skip to content

fix(core): preserve reasoning metadata on errored assistant turns#38763

Open
loulanyue wants to merge 1 commit into
anomalyco:devfrom
loulanyue:fix/38620-errored-reasoning-metadata
Open

fix(core): preserve reasoning metadata on errored assistant turns#38763
loulanyue wants to merge 1 commit into
anomalyco:devfrom
loulanyue:fix/38620-errored-reasoning-metadata

Conversation

@loulanyue

Copy link
Copy Markdown

Issue for this PR

Closes #38620

Type of change

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

What does this PR do?

When a session turn is interrupted (network error, timeout, etc.) and contains both a thinking block and a tool_use block, the existing replay logic in to-llm-message.ts stripped providerMetadata from the reasoning block because message.error was defined. This left the tool_use block without a valid preceding thinking block, causing Anthropic to return a 400 error on replay.

The fix is minimal: always pass item.providerMetadata for reasoning blocks, regardless of whether the message has an error. The reuseProviderMetadata flag still guards tool-related metadata on errored turns; only reasoning metadata is now unconditionally preserved.

How did you verify your code works?

Updated the existing test drops provider-native continuation metadata from failed assistant turns (now renamed to reflect the new behavior) to assert that reasoning blocks retain their providerMetadata while tool-related continuation metadata is still dropped. All session tests pass:

bun test test/session
203 tests passed

Screenshots / recordings

No UI changes.

Checklist

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

Fixes anomalyco#38620. Anthropic rejects a replayed `tool_use` if its preceding `thinking` block is dropped. By relaxing the `message.error === undefined` guard for reasoning blocks, we ensure the signature remains and the block is not filtered out as empty, keeping the message valid for the provider.
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.

V2: errored-message replay can 400 with Anthropic thinking + tool use

1 participant