openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

fix(streaming): skip SSE events with no data to prevent JSONDecodeError

Open c4milo opened this issue 3 months ago • 1 comments

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)
  • [x] I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

Additional context & links

c4milo avatar Oct 31 '25 13:10 c4milo

I think this is an issue with the model, not with the OpenAI library. For example, in the same error, the model returned a non-standard JSON, causing the error.https://github.com/gpustack/gpustack/issues/4147

tao-xiaoxin avatar Jan 05 '26 09:01 tao-xiaoxin