Repository navigation
fix: announce rotated Copilot reasoning item ids before the SDK parse… - #835
Conversation
fbe1a29 to
21bce91
Compare
…s them GitHub Copilot's Responses API proxy emits reasoning summary events under an item_id it never announced via response.output_item.added — either a rotated id or no announcement at all. The OpenAI Responses stream parser assumes that state exists and dereferences it, so the stream dies with "Cannot read properties of undefined (reading 'summaryParts')". The repository already carries a defensive patch for this in patches/@[email protected], but pnpm patchedDependencies is a workspace-local install setting: it is not published, and dist is not bundled, so every npm/brew user resolves @ai-sdk/openai straight from the registry. ^3.0.71 currently floats to 3.0.91, which still has the unguarded dereferences, which is why the crash survived the patch. Normalizes the Copilot SSE stream instead, in code that actually ships: when a reasoning event references an unannounced item_id, a synthetic response.output_item.added is emitted ahead of it so the parser always has state. Healthy streams pass through byte for byte. Closes Nano-Collective#719
Copilot rotates the opaque reasoning item id mid-stream while output_index stays stable, as confirmed by the upstream trace in vercel/ai#18548. Treating each rotated id as a new reasoning item split one item into several and left the original never concluded. Correlate on output_index instead: a rotated id is rewritten to the id already announced at that index, so summary parts accumulate on a single reasoning item that concludes properly. A reasoning item that was never announced at all still gets a synthetic response.output_item.added. Closes Nano-Collective#719
The reasoning-item and reasoning-summary branches each carried their own copy of the announce, rewrite and synthesize logic. Resolve the item id once up front, then run a single path for all three outcomes. Behaviour is unchanged: the same 25 unit tests pass, and the streams verified against an unpatched @ai-sdk/openai produce identical results.
21bce91 to
6c7a406
Compare
|
Hey @addyCooks - thanks for this - the SSE-layer approach is right, and I checked that the response.reasoning_summary prefix covers all three activeReasoning dereference sites in @ai-sdk/[email protected], so the crash surface is fully handled. One thing I'd like to sort before merging... rewrite() bails on !trimmed.startsWith('data:'), so if Copilot frames its stream the way the upstream Responses API does, with an event: line ahead of each data: line, the normalizer is a silent no-op. I ran that exact input through the branch and got byte-identical passthrough: nothing announced, nothing rewritten, and all 5 tests still green because they're all data-only. Do you have a raw capture from your repro that shows the framing? I tried to confirm against live Copilot but /responses is gated on my account. Related: separator is hardcoded to '\n\n', and \r\n\r\n contains no two consecutive \n, so a CRLF-framed stream never splits. I confirmed it buffers the entire response and flushes in one go at close, which would stall streaming output on top of skipping the fix. Splitting on /\r\n\r\n|\n\n/ and scanning the block for its data: line would make both cases safe regardless of what Copilot actually sends. Two smaller ones:
Happy to push these if it's easier. |
|
Hi @will-lamerton , After the normalizer, the SDK runs its own SSE parser, and that one already handles event: lines and every line-ending style. Mine is stricter than the thing consuming its output, which is wrong no matter what Copilot sends. I'll split on /\r\n\r\n|\n\n/ and find the data: line inside each block so both framings work. One thing that makes this easier, the SDK's parser only reads the data: line and ignores the event: name, so the synthetic announcement can stay a plain data: block. On output_index....it's required (z.number()), and I checked what happens when it's missing. The SDK sets finishReason = 'error' and emits an error part. So the synthetic chunk doesn't just fail to help, it breaks the response another way. flush() through rewrite(), agreed. I'll push these. |
The normalizer parsed SSE more strictly than the parser directly behind it. `EventSourceParserStream` accepts LF, CRLF and lone CR, and ignores any `event:` line in front of the payload; anything it accepts that we rejected was an event we silently failed to fix. - rewrite() now operates on an SSE block rather than assuming the block is a single `data:` line, so an `event:`-framed stream is normalized instead of passed straight through. Replacements are written in place, leaving `event:`/`id:` lines intact. - Blocks split on /\r\n\r\n|\n\n|\r\r/ and are re-emitted with their own separator. Splitting on '\n\n' alone never matched a CRLF stream, so the whole body buffered until close and streaming output stalled. - flush() runs the trailing buffer through rewrite(), so a final event with no trailing separator is normalized too. - A synthetic announcement is only emitted when `output_index` is a number. It is required by the SDK schema, and a chunk that fails validation sets finishReason to 'error' rather than being skipped, so announcing without one traded the crash for a broken response. The event is passed through untouched instead, and nothing is recorded as announced, so a later well-formed event can still be repaired. Adds coverage for `event:` framing, CRLF framing, a CRLF separator split across chunk boundaries, a trailing event with no separator, and a missing `output_index`. All seven fail against the previous implementation.
|
Nice job @addyCooks - looks good and merging! Quick one, I had @akramcodez mention to me about taking issues and submitting PRs without asking for assignment first. Although your contributions are great and much appreciated - if you wouldn't mind dropping a comment on the issue asking for assignment before beginning work that would be great :) |
|
Hey @will-lamerton, |
Nano-Collective#835) * fix: announce rotated Copilot reasoning item ids before the SDK parses them GitHub Copilot's Responses API proxy emits reasoning summary events under an item_id it never announced via response.output_item.added — either a rotated id or no announcement at all. The OpenAI Responses stream parser assumes that state exists and dereferences it, so the stream dies with "Cannot read properties of undefined (reading 'summaryParts')". The repository already carries a defensive patch for this in patches/@[email protected], but pnpm patchedDependencies is a workspace-local install setting: it is not published, and dist is not bundled, so every npm/brew user resolves @ai-sdk/openai straight from the registry. ^3.0.71 currently floats to 3.0.91, which still has the unguarded dereferences, which is why the crash survived the patch. Normalizes the Copilot SSE stream instead, in code that actually ships: when a reasoning event references an unannounced item_id, a synthetic response.output_item.added is emitted ahead of it so the parser always has state. Healthy streams pass through byte for byte. Closes Nano-Collective#719 * fix: map rotated Copilot reasoning ids to the item at that output_index Copilot rotates the opaque reasoning item id mid-stream while output_index stays stable, as confirmed by the upstream trace in vercel/ai#18548. Treating each rotated id as a new reasoning item split one item into several and left the original never concluded. Correlate on output_index instead: a rotated id is rewritten to the id already announced at that index, so summary parts accumulate on a single reasoning item that concludes properly. A reasoning item that was never announced at all still gets a synthetic response.output_item.added. Closes Nano-Collective#719 * refactor: collapse the duplicated reasoning-id branches into one path The reasoning-item and reasoning-summary branches each carried their own copy of the announce, rewrite and synthesize logic. Resolve the item id once up front, then run a single path for all three outcomes. Behaviour is unchanged: the same 25 unit tests pass, and the streams verified against an unpatched @ai-sdk/openai produce identical results. * fix: normalize Copilot reasoning ids regardless of SSE framing The normalizer parsed SSE more strictly than the parser directly behind it. `EventSourceParserStream` accepts LF, CRLF and lone CR, and ignores any `event:` line in front of the payload; anything it accepts that we rejected was an event we silently failed to fix. - rewrite() now operates on an SSE block rather than assuming the block is a single `data:` line, so an `event:`-framed stream is normalized instead of passed straight through. Replacements are written in place, leaving `event:`/`id:` lines intact. - Blocks split on /\r\n\r\n|\n\n|\r\r/ and are re-emitted with their own separator. Splitting on '\n\n' alone never matched a CRLF stream, so the whole body buffered until close and streaming output stalled. - flush() runs the trailing buffer through rewrite(), so a final event with no trailing separator is normalized too. - A synthetic announcement is only emitted when `output_index` is a number. It is required by the SDK schema, and a chunk that fails validation sets finishReason to 'error' rather than being skipped, so announcing without one traded the crash for a broken response. The event is passed through untouched instead, and nothing is recorded as announced, so a later well-formed event can still be repaired. Adds coverage for `event:` framing, CRLF framing, a CRLF separator split across chunk boundaries, a trailing event with no separator, and a missing `output_index`. All seven fail against the previous implementation.
Closes #719
The bug
Copilot's Responses proxy emits reasoning summary events under an
item_idthat wasnever announced via
response.output_item.added— a rotated id, or no announcement atall. Reproduced against 3.0.91:
response.output_item.added item.id = rs_A
response.reasoning_summary_part.added item_id = rs_B ← never announced
→ TypeError: Cannot read properties of undefined (reading 'summaryParts')
The fix
The throw happens inside the SDK's own stream transform, so no middleware or model
wrapper can intercept it — it has to be corrected at the SSE layer.
createUndiciFetchin this same file already sets that precedent for the
data: [DONE]bug.createReasoningItemNormalizer()is applied to Copilot'stext/event-streamresponses.One rule: when a reasoning event references an unannounced
item_id, emit a syntheticresponse.output_item.addedahead of it so the parser always has state. This ships indist, so it holds regardless of which@ai-sdk/openaithe user resolves.