fix: skip meta-only SSE events with empty data to prevent JSONDecodeError#2933
Open
tarminik wants to merge 1 commit intoopenai:mainfrom
Open
fix: skip meta-only SSE events with empty data to prevent JSONDecodeError#2933tarminik wants to merge 1 commit intoopenai:mainfrom
tarminik wants to merge 1 commit intoopenai:mainfrom
Conversation
…rror
SSE streams may contain meta-only events (retry, id, event without data)
that the SSEDecoder emits with data="". Calling json.loads("") on these
causes JSONDecodeError. Skip events with empty data in both sync and
async __stream__() loops.
Fixes openai#2722
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.
Summary
Fixes #2722
Per the SSE spec (WHATWG HTML § 9.2), SSE streams may contain meta-only events — events with only
retry,id, oreventfields but nodata. Example:retry: 3000\n\n.The
SSEDecodercorrectly parses these and emitsServerSentEventwithdata="". However,Stream.__stream__()andAsyncStream.__stream__()unconditionally callsse.json()(which doesjson.loads("")), causingJSONDecodeError.This happens in practice when streaming through gateways/proxies that inject SSE retry directives.
Fix
Added
if not sse.data: continuein both sync and async__stream__()loops, right after the[DONE]check. Meta-only SSE events (emptydata) are control frames and carry no user-facing payload — they are now silently skipped.Tests
Added 3 new test cases (each parametrized for sync/async, 6 tests total) that exercise
__stream__()directly:JSONDecodeErrorthread.*event without data — verifies the thread-event branch also skips empty dataAll existing tests continue to pass.