Repository navigation
fix(streaming): surface mid-stream errors when iterating buffered streams - #2047
Conversation
…eams The `[Symbol.asyncIterator]()` implementations in ChatCompletionStream and ResponseStream buffer events in `pushQueue` when the producer is ahead of the consumer. The error/abort handlers only rejected readers waiting at that moment and never stored the error, so an error that fired while events were buffered (no reader waiting) was lost: the consumer drained the buffer and `next()` returned done without throwing. Capture the terminal error and re-throw it once the buffer drains, mirroring the existing EventStream.events() pattern. Behavior when a reader is already waiting is unchanged. Adds a regression test to each stream. Closes openai#2046
jbeckwith-oai
left a comment
There was a problem hiding this comment.
Blocking: the same buffered-terminal-error loss remains in two public handwritten streaming paths. ChatCompletionStreamingRunner.toReadableStream() overrides the fixed base conversion with its own pushQueue/readQueue/error handlers, and AssistantStreamSymbol.asyncIterator has the same old pattern. I reproduced both on this exact head: enqueue two items with no reader waiting, emit OpenAIError('boom'), then consume; each returned both items and ended normally with thrown=null. This means tool-runner readable-stream bridges and Assistants iteration can still report a failed response as successful. Please apply the retained-failure semantics (preferably via a shared/EventStream.events-based adapter so these copies cannot drift), including listener/queue cleanup on return/end, to every buffered adapter, and add regressions for these two paths. The two paths changed here otherwise behave correctly: their 17 targeted tests, TypeScript, ESLint, Prettier, build, and diff checks pass. Please also make the new tests deterministic by waiting for the stream's terminal error/done signal instead of a fixed 10 ms sleep, and assert the exact error/message rather than any Error.
…s survive in every stream bridge
Address review: the retained-failure fix previously covered only
ChatCompletionStream and ResponseStream, while
ChatCompletionStreamingRunner.toReadableStream() and
AssistantStream[Symbol.asyncIterator]() kept their own copies of the
buffering logic and still dropped terminal errors once events were
buffered with no reader waiting.
- extract EventStream#_createIterator, a shared buffered async-iterator
adapter (the events() logic, generalized); events() now delegates to it
- rebuild all four duplicated adapters on the shared helper: chunk/event
iterators keep their break-aborts semantics via onReturn, the runner
bridge keeps eager listener registration, AssistantStream keeps
structuredClone-at-push
- remove listeners and drain queues on end/return (previously leaked);
mark the terminal promise handled when the consumer explicitly ends
iteration so the self-inflicted abort no longer surfaces as an
unhandled rejection
- add regression tests for the runner bridge and AssistantStream paths,
plus edge-case tests for abort retention, exactly-once failure
delivery, break-aborts, post-end iteration, and events('error')
- make the buffered-error tests deterministic by awaiting the stream's
terminal signal and asserting the exact error class and message
|
@jbeckwith-oai Thanks for the thorough review — all points addressed in 5f98601. Shared adapter: Went with your suggestion: extracted the Cleanup: Listeners are removed and queues drained on end/return. This surfaced one subtlety worth flagging: the old leaked listeners incidentally suppressed the intentional unhandled-rejection warning when a consumer breaks out of iteration (the self-triggered abort found no listeners after cleanup). Tests: Added regressions for both paths you flagged (both fail on the previous head), plus edge cases: abort retention through a full buffer, exactly-once failure delivery with a pending reader, break-aborts, and post-end iteration returning done instead of hanging. The buffered-error tests now await the stream's terminal Full suite, type check, lint, format, and build are green locally. Ready for another look. |
…ffered-stream-errors # Conflicts: # src/lib/EventStream.ts # src/lib/responses/ResponseStream.ts # tests/lib/ChatCompletionStream.test.ts # tests/lib/EventStream.test.ts # tests/lib/ResponseStream.test.ts
Superseded by verified head 03a2cb1: all buffered adapters now share retained-error handling, deterministic regressions cover Chat, Assistants, Responses, and runner serialization, and 1,727 tests plus build and compatibility checks pass.
HAYDEN-OAI
left a comment
There was a problem hiding this comment.
Verified against actual buffered provider failures across Chat Completions, Assistants, Responses, and tool-runner readable streams. Shared iterator preserves exact-once errors, cancellation, listener cleanup, Assistant snapshots, and existing SSE/custom-tool fixes. Full handwritten suite: 1,727 passing; lint, breaking-change check, package build, and TypeScript 4.9/current checks all pass.
|
Appreciate your feedback! |
Summary
Fixes #2046.
Streaming consumers could silently treat a failed request as successful when chunks or events were already buffered before a terminal error arrived. Chat Completions, Assistants, and streaming tool-runner readable-stream bridges each retained their own lossy iterator implementation; Responses streaming now also uses the same shared implementation consistently.
Extract the existing retained-failure behavior into a shared, documented
EventStreamiterator. Every handwritten streaming adapter now drains queued values, rejects exactly once with the original terminal error or abort, removes producer and lifecycle listeners, and cancels its underlying request when iteration stops early. Assistant events remain cloned when queued, and tool-runner chunk/tool-message listeners remain eager and ordered.The implementation preserves current Responses API nested/flat SSE error normalization, provider error metadata, structured-output parsing, and GPT-5 custom-tool support. All modified SDK source lives under the handwritten
src/lib/tree.Regression coverage
ChatCompletionStreamingRunner.toReadableStream().errorandabortevents without changing their existing semantics.Verification
main.