fix(ssestream): skip events with empty data to prevent JSON unmarshal… #555
+159
−0
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.
… errors
Fix crash when parsing SSE streams that contain empty events from retry: directives or comment lines. Fixes #556
Problem
The eventStreamDecoder creates events with empty Data when it encounters empty lines after non-data SSE fields (like "retry: 3000").
Stream.Next()then attemptsjson.Unmarshalon empty bytes, causing "unexpected end of JSON input" error. This breaks streaming with any SSE server using the retry directive.Root Cause
Per the SSE specification [1], events are dispatched when empty lines are encountered, regardless of whether data was present.
The spec states for empty line handling:
And for the retry field:
For empty data handling:
This means that a sequence like:
Creates a valid empty event according to the spec. Servers commonly send this for reconnection configuration, but the SDK assumed all events contain JSON data.
[1] https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events
Solution
Check if event.Data is empty before attempting to unmarshal. Skip empty events and continue processing the stream. This maintains compatibility with OpenAI API while supporting standard SSE practices per spec.
Tests Added
All tests pass:
Impact
Real-World Testing
Tested with Anthropic Claude 3.5 streaming API via AI Gateway:
Fixes stream crashes with "unexpected end of JSON input" when encountering SSE streams with retry directives or comment lines.