Skip to content

fix: move unreachable error event check before thread event guard in streaming#2906

Closed
giulio-leone wants to merge 1 commit intoopenai:mainfrom
giulio-leone:fix/streaming-unreachable-error-check
Closed

fix: move unreachable error event check before thread event guard in streaming#2906
giulio-leone wants to merge 1 commit intoopenai:mainfrom
giulio-leone:fix/streaming-unreachable-error-check

Conversation

@giulio-leone
Copy link

Summary

Fixes #2796

The sse.event == "error" check inside both Stream.__stream__() and AsyncStream.__stream__() was nested inside the if sse.event.startswith("thread.") block. Since "error" never starts with "thread.", the error-event check was unreachable dead code.

Changes

Move the error-event check to execute before the thread-event guard so that SSE error events are properly detected regardless of event name prefix. The data-level error check in the else branch is preserved as a second line of defence for non-thread events.

Before

if sse.event and sse.event.startswith("thread."):
    data = sse.json()
    if sse.event == "error" ...  # ← unreachable: "error" never starts with "thread."
        raise APIError(...)
    yield ...
else:
    ...

After

if sse.event == "error":
    data = sse.json()
    if is_mapping(data) and data.get("error"):
        raise APIError(...)

if sse.event and sse.event.startswith("thread."):
    data = sse.json()
    yield ...
else:
    ...

Applied to both Stream.__stream__() (sync) and AsyncStream.__stream__() (async).

Testing

All 20 streaming tests pass (2 consecutive clean runs).

@giulio-leone giulio-leone requested a review from a team as a code owner February 28, 2026 07:43
…streaming

The `sse.event == "error"` check inside both `Stream.__stream__()` and
`AsyncStream.__stream__()` was nested inside the
`if sse.event.startswith("thread.")` block. Since "error" never starts
with "thread.", the error-event check was unreachable dead code.

Move the error-event check to execute *before* the thread-event guard so
that SSE error events are properly detected regardless of event name
prefix. The data-level error check in the else branch is preserved as a
second line of defence for non-thread events.

Fixes openai#2796
@giulio-leone giulio-leone force-pushed the fix/streaming-unreachable-error-check branch from d91d286 to ca89da6 Compare February 28, 2026 14:41
@giulio-leone
Copy link
Author

Closing — this unreachable code check is too marginal to warrant a PR.

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