Skip to content

fix(stream): usage on finalChatCompletion, fence-strip in .text, APIUserAbortError on abort#11

Merged
Abhinavexist merged 1 commit into
mainfrom
fix/stream-usage-fence-abort
Jul 22, 2026
Merged

fix(stream): usage on finalChatCompletion, fence-strip in .text, APIUserAbortError on abort#11
Abhinavexist merged 1 commit into
mainfrom
fix/stream-usage-fence-abort

Conversation

@Abhinavexist

Copy link
Copy Markdown
Collaborator

Three streaming bugs in src/stream.ts:

  1. finalChatCompletion() dropped usage. The streamed usage chunk (empty choices, populated usage) was skipped by #accumulate's if (!choice) return. Now usage + system_fingerprint are read before the choice check and included in the result (mirrors interfaze-python's fix).
  2. .text getter didn't strip the json_object fence. Same stream gave two answers — finalChatCompletion() content was {"a":1} but stream.text returned the ```json-fenced form. stripFence was applied in `#build()` but not the getter; now both strip.
  3. Aborting mid-stream was silent. AbortController.abort() during iteration just ended the stream — no error — so truncated content looked final. Now iterating (or finalChatCompletion()/textDeltas()) throws APIUserAbortError when the signal is aborted, consistent with the non-streaming path.

Tests added for all three; full suite 52 passing, typecheck + build clean.

…xt, raise APIUserAbortError on abort

- finalChatCompletion() now captures the streamed usage chunk (empty choices) +
  system_fingerprint, read before the choice early-return, and includes them in the result.
- .text getter applies the json_object fence strip (stripFence), matching #build(); previously
  finalChatCompletion() content was unfenced but stream.text still returned the ```json fence.
- Aborting mid-stream now throws APIUserAbortError (consistent with non-streaming) instead of
  ending silently, so a truncated/aborted stream is distinguishable from a completed one.
@Abhinavexist
Abhinavexist merged commit 5b9fa96 into main Jul 22, 2026
3 checks passed
@Abhinavexist
Abhinavexist deleted the fix/stream-usage-fence-abort branch July 24, 2026 08:23
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.

1 participant