Skip to content

ChatCompletionStream/ResponseStream: mid-stream errors are silently swallowed when iterating with for await #2046

Closed

Description

@pouyashahrdami

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

  1. Start a streaming request via openai.chat.completions.stream(...) (or openai.responses.stream(...)).
  2. Iterate it with for await, doing some await work per chunk so the internal buffer fills.
  3. Have the stream fail partway through (e.g. connection drop / an error frame).
  4. Observe that the for await loop ends normally and does not throw — the error is swallowed.

Code snippets

const stream = openai.chat.completions.stream({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'hi' }],
});

try {
  for await (const chunk of stream) {
    await persist(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
}

OS

macOS 26.6

Node version

Node v22.23.1

Library version

openai v7.3.0

Activity

  1. pouyashahrdami commented on Aug 3, 2026

    @pouyashahrdami
    ContributorAuthor

    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.

  2. added a commit that references this issue on Aug 12, 2026
    8cc6cac
  3. Vish12345678673 commented on Sep 18, 2026

    @Vish12345678673

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions