Skip to content

fix(streaming): remove unreachable sse.event == error check#2844

Open
Mr-Neutr0n wants to merge 1 commit intoopenai:mainfrom
Mr-Neutr0n:fix/streaming-error-check-dead-code
Open

fix(streaming): remove unreachable sse.event == error check#2844
Mr-Neutr0n wants to merge 1 commit intoopenai:mainfrom
Mr-Neutr0n:fix/streaming-error-check-dead-code

Conversation

@Mr-Neutr0n
Copy link

Summary

Fixes dead code bug where the sse.event == "error" condition was unreachable.

Problem

The sse.event == "error" check was nested inside a block that requires sse.event.startswith("thread."):

if sse.event and sse.event.startswith("thread."):
    data = sse.json()
    if sse.event == "error" and is_mapping(data) and data.get("error"):  # Always False!
        raise APIError(...)

Since "error" does not start with "thread.", this condition can never be True.

Solution

Changed the check to look for error data in the payload rather than the event name:

if sse.event and sse.event.startswith("thread."):
    data = sse.json()
    if is_mapping(data) and data.get("error"):  # Now works!
        raise APIError(...)

This is consistent with how errors are handled in the else branch for non-thread events (lines 84-96).

Changes

  • src/openai/_streaming.py: Fixed both sync (line 67) and async (line 170) versions

Test Plan

  • Syntax validation passes
  • Logic now allows error detection for thread.* events by checking the data payload

Fixes #2796

The `sse.event == "error"` condition was inside a block that requires
`sse.event.startswith("thread.")`. Since "error" does not start with
"thread.", this check was always False and unreachable.

Changed from:
  if sse.event == "error" and is_mapping(data) and data.get("error"):

To:
  if is_mapping(data) and data.get("error"):

This allows error detection for thread.* events by checking the data
payload instead of the event name, consistent with how errors are
handled in the else branch for non-thread events.

Fixes both sync (line 67) and async (line 170) versions.

Fixes openai#2796
@Mr-Neutr0n Mr-Neutr0n requested a review from a team as a code owner February 5, 2026 15:03
@Mr-Neutr0n
Copy link
Author

Friendly follow-up - is there anything I can improve in this PR? Happy to address any feedback.

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.

Bug: Dead code - sse.event == "error" check is unreachable in _streaming.py

1 participant