Skip to content

SSE stream drops the final event (finish_reason / response.completed lost) when the server omits the trailing blank line #2725

Closed

Description

@hbmask

Summary

When an SSE stream ends right after the final data: line (without the trailing blank line \n\n), the final event is silently dropped. For the Responses API this event is usually response.completed, so downstream strict clients see the stream end with no terminal event and raise "OpenAI Responses stream ended before a terminal response event", then retry.

Reproduced on openai-node 6.40.0 and still present on 7.15.0 (Node v22).

Repro

A local HTTP server returns a text/event-stream body for a chat.completions streaming call, in three variants:

  • A final event ends with a trailing blank line \n\n (conformant)
  • B final event ends with a single \n
  • C final event ends with no newline at all
for await (const p of stream) { if (p.choices[0].finish_reason) ... }

Result: A reports finish_reason; B and C never do (1 chunk instead of 2), with no error raised.

Root cause

core/streaming.mjs -> streamSSE(): SSEDecoder.decode(line) only emits a parsed event when it sees an empty line; iterSSEChunks() yields the leftover bytes at EOF, but the decoder is never flushed, so a trailing unterminated event is discarded.

Proposed fix (verified on 6.40.0 and 7.15.0)

After the lineDecoder.flush() loop in streamSSE(), flush any pending event:

const sse = sseDecoder.decode("");
if (sse) yield sse;

Verified against conformant, multi-event, and comment-trailing streams: no regression; the trailing event is now delivered.

Impact

Any gateway/proxy that closes the connection immediately after the final data: line (a common server race, especially under load) triggers this. The symptom is intermittent and load-dependent, so it reads as network flakiness.

Activity

  1. added a commit that references this issue on Sep 16, 2026
    94d6418
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