Skip to content

Conversation

@c4milo
Copy link

@c4milo c4milo commented Oct 31, 2025

Fixes an issue where SSE events containing only meta-fields (retry, id, event) but no data field would cause JSONDecodeError when the SDK tried to parse empty strings as JSON.

Per the SSE specification (WHATWG HTML § 9.2), retry directives and other meta-only events are valid and should not be treated as data events:

    retry: 3000

    data: {"actual":"content"}

The SDK's SSE decoder correctly parses these events, setting the retry field but leaving data empty. However, the Stream.__stream__() and AsyncStream.__stream__() methods were attempting to call .json() on all events, including those with empty data, leading to JSONDecodeError.

Changes:

  • Added check in Stream.__stream__() to skip events with empty/whitespace data
  • Added check in AsyncStream.__stream__() to skip events with empty/whitespace data
  • Added test cases for retry directives and meta-only events

The fix ensures that only events with actual data are parsed as JSON, preventing the error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

that occurred when trying to parse empty strings.

Example error this fixes:

    File "openai/_streaming.py", line 82, in __stream__
        data = sse.json()
    File "openai/_streaming.py", line 259, in json
        return json.loads(self.data)
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Additional context & links

Fixes an issue where SSE events containing only meta-fields (retry, id, event)
but no data field would cause JSONDecodeError when the SDK tried to parse
empty strings as JSON.

Per the SSE specification (WHATWG HTML § 9.2), retry directives and other
meta-only events are valid and should not be treated as data events:

    retry: 3000

    data: {"actual":"content"}

The SDK's SSE decoder correctly parses these events, setting the retry field
but leaving data empty. However, the Stream.__stream__() and AsyncStream.__stream__()
methods were attempting to call .json() on all events, including those with
empty data, leading to JSONDecodeError.

Changes:
- Added check in Stream.__stream__() to skip events with empty/whitespace data
- Added check in AsyncStream.__stream__() to skip events with empty/whitespace data
- Added test cases for retry directives and meta-only events

The fix ensures that only events with actual data are parsed as JSON,
preventing the error: "json.decoder.JSONDecodeError: Expecting value: line 1
column 1 (char 0)" that occurred when trying to parse empty strings.

Example error this fixes:
    File "openai/_streaming.py", line 82, in __stream__
        data = sse.json()
    File "openai/_streaming.py", line 259, in json
        return json.loads(self.data)
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
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