Repository navigation
Conversation
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
|
You're right, the per-message merge was too narrow and changed valid replays. I switched to the suggestion from the issue: 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.
|
There was a problem hiding this comment.
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.
Issue for this PR
Closes #61641
Type of change
What does this PR do?
Legacy session history can contain
reasoningparts with neither an item id norencrypted_content.lowerMessagesreplayed each one as a separate Responsesreasoninginput item, and upstreams that identify input items by id reject several id-less items withDuplicate item found with id ..lowerReasoningnow returnsundefinedfor 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?
bun test test/provider/open-responses-replay.test.ts test/provider/openai-responses.test.ts: 164 pass.bun run testinpackages/ai: 2050 pass, 28 skip.bun run checkat the repo root: 35 tasks successful.Checklist
Added by the opencode bot (AI-generated).
Related: #55357