You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ChatCompletionStream/ResponseStream: mid-stream errors are silently swallowed when iterating with for await #2046
Confirm this is a Node library issue and not an underlying OpenAI API issue
This is an issue with the Node library
Describe the bug
When consuming a streaming helper with for await (const chunk of stream), a terminal error that occurs mid-stream can be silently swallowed — the loop finishes cleanly as if the response completed successfully, when it actually failed (e.g. the connection dropped).
The [Symbol.asyncIterator]() implementations in ChatCompletionStream (src/lib/ChatCompletionStream.ts) and ResponseStream (src/lib/responses/ResponseStream.ts) buffer incoming events in an internal pushQueue when the producer is ahead of the consumer. The error/abort handlers only reject readers that are waiting at that exact moment (readQueue); they never store the error. So if the error fires while events are buffered and no reader is currently waiting, it rejects nobody and just sets done = true. The consumer then drains the buffered events and next() returns { done: true } without ever throwing — the error is lost.
This happens in the common case where the loop body does any await (persisting a chunk, processing, etc.), which lets the network/producer run ahead and fill the buffer.
Notably, the SDK's own EventStream.events() (src/lib/EventStream.ts) already handles this correctly with failure/failureDelivered bookkeeping; the two [Symbol.asyncIterator] implementations were just never given the same treatment.
To Reproduce
Start a streaming request via openai.chat.completions.stream(...) (or openai.responses.stream(...)).
Iterate it with for await, doing some await work per chunk so the internal buffer fills.
Have the stream fail partway through (e.g. connection drop / an error frame).
Observe that the for await loop ends normally and does not throw — the error is swallowed.
Code snippets
conststream=openai.chat.completions.stream({model: 'gpt-4o',messages: [{role: 'user',content: 'hi'}],});try{forawait(constchunkofstream){awaitpersist(chunk);// the await lets the internal buffer fill}console.log('completed');// ← reached even when the stream errored mid-way}catch(err){console.error('stream failed',err);// ← never runs, error is lost}
Opened a fix for this: #2047. It captures the terminal error in both stream iterators and re-throws it after the buffer drains (mirroring EventStream.events()), with a regression test for each stream.
I reviewed the async iterator error-handling path in ChatCompletionStream and ResponseStream and the existing failure bookkeeping in EventStream.events().
I would like to reproduce the buffered-error case locally and implement a focused fix with regression coverage for errors that occur while events are already buffered.
I have not started a PR yet and will coordinate here before proceeding.
Activity
pouyashahrdami commentedon Aug 3, 2026
Opened a fix for this: #2047. It captures the terminal error in both stream iterators and re-throws it after the buffer drains (mirroring
EventStream.events()), with a regression test for each stream.fix(streaming): surface mid-stream errors when iterating buffered str…
Vish12345678673 commentedon Sep 18, 2026
Hi! I would like to work on this issue.
I reviewed the async iterator error-handling path in ChatCompletionStream and ResponseStream and the existing failure bookkeeping in EventStream.events().
I would like to reproduce the buffered-error case locally and implement a focused fix with regression coverage for errors that occur while events are already buffered.
I have not started a PR yet and will coordinate here before proceeding.