Skip to content

fix: skip meta-only SSE events with empty data to prevent JSONDecodeError#2933

Open
tarminik wants to merge 1 commit intoopenai:mainfrom
tarminik:fix/skip-empty-sse-data
Open

fix: skip meta-only SSE events with empty data to prevent JSONDecodeError#2933
tarminik wants to merge 1 commit intoopenai:mainfrom
tarminik:fix/skip-empty-sse-data

Conversation

@tarminik
Copy link

@tarminik tarminik commented Mar 6, 2026

Summary

Fixes #2722

Per the SSE spec (WHATWG HTML § 9.2), SSE streams may contain meta-only events — events with only retry, id, or event fields but no data. Example: retry: 3000\n\n.

The SSEDecoder correctly parses these and emits ServerSentEvent with data="". However, Stream.__stream__() and AsyncStream.__stream__() unconditionally call sse.json() (which does json.loads("")), causing JSONDecodeError.

This happens in practice when streaming through gateways/proxies that inject SSE retry directives.

Fix

Added if not sse.data: continue in both sync and async __stream__() loops, right after the [DONE] check. Meta-only SSE events (empty data) 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:

  1. Meta-only retry event followed by valid JSON — verifies no JSONDecodeError
  2. Event-only frame (no data) interleaved with valid data — verifies only JSON events are yielded
  3. thread.* event without data — verifies the thread-event branch also skips empty data

All existing tests continue to pass.


  • I confirm that the changes in this PR are mine and I have the right to submit them under the project's license.

…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
@tarminik tarminik requested a review from a team as a code owner March 6, 2026 10:19
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.

Streaming: JSONDecodeError when SSE events contain only meta-fields (retry, id, event)

1 participant