Skip to content

fix(streaming): do not dispatch SSE events without a data field - #1214

Open
pcbeingused333 wants to merge 2 commits into
digitalocean:mainfrom
pcbeingused333:fix/sse-skip-empty-events
Open

pcbeingused333 wants to merge 2 commits into
digitalocean:mainfrom
pcbeingused333:fix/sse-skip-empty-events

Conversation

@pcbeingused333

Copy link
Copy Markdown

Related issues: none (found by code review).

Cause: SSEReader.Next dispatched an event for any block containing at least one field. A block with only id:, retry:, event: or an unknown field therefore yielded an event with empty Data; ChatCompletionStream.Next then tried to JSON-decode it and failed with unexpected end of JSON input, ending the stream and losing every later chunk.

Fix: only dispatch when a data field was seen, as the SSE spec requires (https://html.spec.whatwg.org/multipage/server-sent-events.html#dispatchMessageEvent). Fields of a data-less block are discarded; id still updates the last event ID. The same rule applies to a final block at EOF.

Tests: TestSSEReader_SkipsEventsWithoutData (4 metadata variants) and TestSSEReader_DatalessFinalEventAtEOF in streaming_test.go. Red before the fix, green after; go test . and go vet . pass.

Disclosure: this change was prepared with an AI coding assistant (Claude) and submitted by the account owner.

pcbeingused333 and others added 2 commits November 6, 2026 12:25
A block containing only id/retry/event/unknown fields was dispatched as
an event with empty data, which made ChatCompletionStream fail with
'unexpected end of JSON input' and drop all later chunks. Per the SSE
spec such blocks are ignored.
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