Skip to content

Conversation

@yurekami
Copy link

Summary

  • Fixed unreachable dead code in _streaming.py where the error event check was incorrectly placed inside the thread.* event handling block
  • Added regression tests for error event handling

Problem

In src/openai/_streaming.py, the check for sse.event == "error" was placed inside the if sse.event.startswith("thread.") block. Since "error" doesn't start with "thread.", this condition could never be true, making lines 67-79 (sync) and 170-182 (async) unreachable dead code.

# Before (dead code):
if sse.event and sse.event.startswith("thread."):
    data = sse.json()
    if sse.event == "error":  # <-- Can never be true!
        raise APIError(...)

This was identified as a regression from a previous refactoring commit.

Solution

Moved the error event check to be before the thread.* check, so it's evaluated independently:

# After:
if sse.event == "error":
    data = sse.json()
    raise APIError(...)

if sse.event and sse.event.startswith("thread."):
    # Handle thread.* events

Also simplified the error handling to correctly parse the ErrorObject structure from the Assistants API (which has message directly in data, not nested under data.error).

Test plan

  • Added test_error_event_raises_api_error - verifies error events raise APIError with correct message
  • Added test_error_event_with_missing_message - verifies default message is used when message field is missing
  • All 24 streaming tests pass (including 4 new test cases for sync/async variants)

Fixes #2796

🤖 Generated with Claude Code

The error event check (`sse.event == "error"`) was incorrectly placed
inside the `if sse.event.startswith("thread.")` block, making it
unreachable since "error" doesn't start with "thread.".

This fix:
- Moves the error event check to be before the thread.* event check
- Properly handles the ErrorObject structure from the Assistants API
- Adds regression tests for error event handling

Fixes openai#2796

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@yurekami yurekami requested a review from a team as a code owner December 28, 2025 21:08
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