fix(streaming): skip empty SSE data events and move error-event check out of thread. block#2943
Open
s-zx wants to merge 1 commit intoopenai:mainfrom
Open
fix(streaming): skip empty SSE data events and move error-event check out of thread. block#2943s-zx wants to merge 1 commit intoopenai:mainfrom
s-zx wants to merge 1 commit intoopenai:mainfrom
Conversation
…ad. block Two related bugs in Stream.__stream__ and AsyncStream.__stream__: 1. JSONDecodeError on meta-only SSE events (fixes openai#2722) The SSE specification allows events with no data field (e.g. standalone 'retry:' or 'id:' directives). The SDK's SSE parser correctly sets data='' for these but __stream__ called sse.json() unconditionally, raising JSONDecodeError: Expecting value. Fix: skip events whose data is empty or whitespace before any JSON parsing. 2. Unreachable sse.event == 'error' check (fixes openai#2796) The error-event guard was nested inside the startswith('thread.') branch, making it logically impossible to trigger because 'error' != 'thread.*'. This was a regression from commit abc2596 ('fix(streaming): correct indentation') where the check was accidentally moved inside the block. Fix: move the error-event handling to the else branch (non-thread events), which is the correct location for standalone 'error' SSE events.
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
Two related bugs in
Stream.__stream__andAsyncStream.__stream__(src/openai/_streaming.py), both sync and async paths.Bug 1:
JSONDecodeErroron meta-only SSE events (closes #2722)The SSE specification allows events that carry only meta-fields (
retry:,id:,event:) with nodatafield. The SDK's SSE parser correctly handles these by settingdata = "", but__stream__calledsse.json()unconditionally:This crashes with
JSONDecodeError: Expecting value: line 1 column 1 (char 0)when any API gateway or proxy injects an SSEretry:directive.Fix: skip events whose
datafield is empty or whitespace before any JSON parsing.Bug 2: Unreachable
sse.event == "error"check (closes #2796)The error-event guard was nested inside the
if sse.event.startswith("thread.")branch:Since
"error"does not start with"thread.", this check can never be true — it's dead code and was a regression from commitabc25966. The error-event handling belongs in theelsebranch (non-thread events).Fix: move the
sse.event == "error"guard to theelsebranch.Changes
src/openai/_streaming.py— syncStream.__stream__and asyncAsyncStream.__stream__if not sse.data or not sse.data.strip(): continuesse.event == "error"check fromthread.block toelseblock