Skip to content

fix(ai): drop unreplayable legacy reasoning on responses replay - #61973

Open
rsdenck wants to merge 2 commits into
anomalyco:devfrom
rsdenck:legacy-reasoning-replay
Open

rsdenck wants to merge 2 commits into
anomalyco:devfrom
rsdenck:legacy-reasoning-replay

Conversation

@rsdenck

@rsdenck rsdenck commented Nov 5, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #61641

Type of change

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

What does this PR do?

Legacy session history can contain reasoning parts with neither an item id nor encrypted_content. lowerMessages replayed each one as a separate Responses reasoning input item, and upstreams that identify input items by id reject several id-less items with Duplicate item found with id ..

lowerReasoning now returns undefined for reasoning that has no id and no encrypted state. That state cannot be matched or continued by the upstream anyway, so dropping it removes the duplicate empty ids without changing valid replay. Reasoning with a valid id or with encrypted content is still replayed as before, including the existing same-id merge path. No synthetic ids are generated.

The previous revision only merged adjacent id-less items within one assistant message. This version also handles histories with legacy reasoning in multiple turns, while leaving encrypted items and malformed-but-encrypted ids alone.

How did you verify your code works?

  • Replay test with legacy id-less reasoning across two assistant turns: the compiled input contains no reasoning items and preserves both text messages.
  • Existing malformed-id test now expects the encrypted item to survive and the unencrypted item to be dropped.
  • bun test test/provider/open-responses-replay.test.ts test/provider/openai-responses.test.ts: 164 pass.
  • bun run test in packages/ai: 2050 pass, 28 skip.
  • bun run check at the repo root: 35 tasks successful.

Checklist

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

Added by the opencode bot (AI-generated).

Related: #55357

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the single-message case from #61641's reproduction, but not the general case: a legacy session with more than one assistant turn still fails with Duplicate item found with id ..

I ran the issue's reproduction (a mock Responses server that rejects more than one reasoning item without an id) from source. On the base, the second turn fails. With this PR it succeeds, and the two summaries are merged into one item. Then I gave each of two assistant messages one legacy reasoning part with no id. With this PR the next turn still failed, because the request held two id-less reasoning items, one per message. The merge state is reset for every assistant message and by any part that isn't reasoning, so it never spans turns, tool calls or text. Legacy sessions usually have id-less reasoning in every turn.

Merging also changes valid replays: reasoning items whose ids were dropped for not being shaped like rs_… (#55357) get joined, and the earlier item's encrypted_content is lost (see inline). A more robust fix is the one suggested in the issue: don't send reasoning that has neither an id nor encrypted content (have lowerReasoning return undefined). It has no replayable state anyway. That fixes multi-turn sessions and leaves encrypted items alone.

Public API: unchanged (only the Responses request body built from history).

The two changed test files pass (164 tests), and tsc --noEmit in packages/ai is clean.

Related: #55357

if (message.role === "assistant") {
const content: TextPart[] = []
const reasoningItems: Record<string, OpenResponsesReasoningInput> = {}
let idlessReasoning: OpenResponsesReasoningInput | undefined

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This state is created for each assistant message, and any non-reasoning part (L719) resets it. So id-less reasoning in different turns, or split by a tool call or text, is still sent as several id-less items. I reproduced this with two assistant messages that each have one legacy reasoning part: the request is still rejected with Duplicate item found with id .. Most legacy sessions have more than one turn. Dropping id-less reasoning that has no encrypted_content in lowerReasoning would cover every case.


const mergeReasoning = (target: OpenResponsesReasoningInput, source: OpenResponsesReasoningInput) => {
target.summary.push(...source.summary)
if (typeof source.encrypted_content === "string") target.encrypted_content = source.encrypted_content

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When two adjacent id-less items both carry encrypted_content (for example, provider items whose non-rs_ ids were stripped, as in #55357), this keeps only the last one, so the first item's reasoning state is silently lost. The updated test in openai-responses.test.ts shows two distinct provider items being joined, with one item's summary attached to the other's encrypted state. Items that have encrypted content should probably not be merged.

@rsdenck rsdenck changed the title fix(ai): merge id-less reasoning items on responses replay fix(ai): drop unreplayable legacy reasoning on responses replay Nov 5, 2026
@rsdenck

rsdenck commented Nov 5, 2026

Copy link
Copy Markdown
Author

You're right, the per-message merge was too narrow and changed valid replays. I switched to the suggestion from the issue: lowerReasoning now returns undefined when a reasoning part has neither a valid item id nor encrypted_content. Such parts have nothing the upstream can match or continue, so they are dropped instead of being merged.

This handles legacy reasoning in multiple assistant turns and leaves reasoning items with encrypted state alone, including the malformed-id case from #55357. The replay test now has two assistant turns with legacy id-less reasoning and asserts that neither produces an input item, while the existing malformed-id test asserts the encrypted item survives and the unencrypted one is dropped.

open-responses-replay.test.ts + openai-responses.test.ts: 164 pass; full packages/ai suite: 2050 pass, 28 skip; bun run check at the root: 35 tasks successful.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both earlier points are fixed. Merging is gone, and lowerReasoning now drops reasoning that has neither an id nor encrypted content, so legacy sessions work no matter how many turns they have.

I reran the #61641 reproduction from source, including a variant with legacy id-less reasoning in two assistant turns. On the base, the request carries two reasoning items without an id and is rejected. With this PR it carries none and the turn succeeds. Reasoning with a valid id is still replayed. The two changed test files pass (164 tests), and tsc --noEmit in packages/ai is clean.

Public API: unchanged.

One side effect worth knowing about, not blocking: itemID() discards ids that aren't shaped like rs_… (#55357). For providers that issue such ids, reasoning without encrypted content is now left out of the replay, where before it was sent without an id. That helps gateways that reject id-less reasoning items. It could matter for a provider that needs reasoning sent back; keeping those ids is #56879's job.

This branch has not been deployed

No deployments
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.

session: legacy reasoning replay fails with "Duplicate item found with id ."

1 participant