Repository navigation
fix(streaming): do not dispatch SSE events without a data field - #1214
Open
pcbeingused333 wants to merge 2 commits into
Open
pcbeingused333 wants to merge 2 commits into
pcbeingused333 wants to merge 2 commits into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issues: none (found by code review).
Cause:
SSEReader.Nextdispatched an event for any block containing at least one field. A block with onlyid:,retry:,event:or an unknown field therefore yielded an event with emptyData;ChatCompletionStream.Nextthen tried to JSON-decode it and failed withunexpected end of JSON input, ending the stream and losing every later chunk.Fix: only dispatch when a
datafield 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;idstill updates the last event ID. The same rule applies to a final block at EOF.Tests:
TestSSEReader_SkipsEventsWithoutData(4 metadata variants) andTestSSEReader_DatalessFinalEventAtEOFinstreaming_test.go. Red before the fix, green after;go test .andgo vet .pass.Disclosure: this change was prepared with an AI coding assistant (Claude) and submitted by the account owner.