Skip to content

fix: announce rotated Copilot reasoning item ids before the SDK parse… - #835

Merged
will-lamerton merged 4 commits into
Nano-Collective:mainfrom
addyCooks:fix/copilot-reasoning-item-id-rotation
Aug 13, 2026
Merged

will-lamerton merged 4 commits into
Nano-Collective:mainfrom
addyCooks:fix/copilot-reasoning-item-id-rotation

Conversation

@addyCooks

Copy link
Copy Markdown
Collaborator

Closes #719

The bug

Copilot's Responses proxy emits reasoning summary events under an item_id that was
never announced via response.output_item.added — a rotated id, or no announcement at
all. 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. createUndiciFetch
in this same file already sets that precedent for the data: [DONE] bug.

createReasoningItemNormalizer() is applied to Copilot's text/event-stream responses.
One rule: when a reasoning event references an unannounced item_id, emit a synthetic
response.output_item.added ahead of it so the parser always has state. This ships in
dist, so it holds regardless of which @ai-sdk/openai the user resolves.

@addyCooks

Copy link
Copy Markdown
Collaborator Author

cc @will-lamerton

@addyCooks
addyCooks force-pushed the fix/copilot-reasoning-item-id-rotation branch 2 times, most recently from fbe1a29 to 21bce91 Compare August 10, 2026 14:00
…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.
@addyCooks
addyCooks force-pushed the fix/copilot-reasoning-item-id-rotation branch from 21bce91 to 6c7a406 Compare August 12, 2026 07:27
@will-lamerton

Copy link
Copy Markdown
Member

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:

  • flush() enqueues the trailing buffer raw, so a final event with no trailing separator skips normalization. Wrapping it in rewrite() covers it.
  • On the never-announced path, announce() runs even when output_index isn't a number, and JSON.stringify then drops the key. The SDK schema requires output_index, so the synthetic chunk fails validation rather than repairing anything. Worth guarding.

Happy to push these if it's easier.

@addyCooks

Copy link
Copy Markdown
Collaborator Author

Hi @will-lamerton ,
I don't have raw bytes. I traced it off the SDK's parsed chunk stream, so I genuinely can't tell whether there's an event: line in front of each data, that detail is already gone by the point I was looking. Good catch.

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.
I'm thinking to skip the rewrite entirely when the field isn't there rather than guess an index.

flush() through rewrite(), agreed.

I'll push these.
Adding tests for event: framing, CRLF, and a final event with no trailing separator the existing five all use plain data with \n\n, which is how this slipped through.

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.
@will-lamerton

Copy link
Copy Markdown
Member

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

@will-lamerton
will-lamerton merged commit 9d808a0 into Nano-Collective:main Aug 13, 2026
11 checks passed
@addyCooks

addyCooks commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Hey @will-lamerton,
moving forward I'll handle it that way
do let me know if there's anything specific to help with (maybe a few task?)
also really loving the codebase

AryanNandanwar pushed a commit to AryanNandanwar/nanocoder that referenced this pull request Aug 13, 2026
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.
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.

[Bug] Cannot read properties of undefined (reading 'summaryParts') when using Github Copilot with GPT-5.3 Codex

2 participants