Skip to content

Conversation

@jay-dhamale
Copy link

Summary

Fixes #2796 - Removes dead code in streaming error handling

Problem

The error handling logic for sse.event == "error" was incorrectly nested inside the if sse.event.startswith("thread.") condition at:

  • Lines 67-79 in Stream.__stream__ method
  • Lines 170-182 in AsyncStream.__stream__ method

This made the error handling code logically unreachable (dead code) because a string cannot simultaneously:

  • Equal "error"
  • Start with "thread."

Root Cause

Regression introduced in commit abc25966 which fixed indentation but accidentally moved the error handler from an else branch into the thread.* condition block.

Solution

Restructured the conditional logic to:

  1. Check for sse.event == "error" FIRST (before any event type routing)
  2. Handle thread.* events with their special data structure format
  3. Handle all other events in the else block

This ensures error events are properly caught and raise APIError with the appropriate error message.

Changes

  • Modified src/openai/_streaming.py:
    • Extracted error event handling to top-level if statement
    • Used independent if statements (not elif) for clear logic flow
    • Applied identical fix to both Stream and AsyncStream classes

Testing

  • ✅ All existing tests pass (20/20 in test_streaming.py)
  • ✅ Linting passes (ruff check .)
  • ✅ Formatting passes (ruff format)
  • ✅ No regressions in streaming functionality
  • ✅ Module imports correctly

Test Plan

  • Run full test suite: All streaming tests pass
  • Verify both sync and async streams handle errors correctly
  • No breaking changes - the buggy code path was unreachable

Fixes openai#2796

The error handling logic for `sse.event == "error"` was incorrectly
nested inside the `if sse.event.startswith("thread.")` condition,
making it logically unreachable (dead code). A string cannot both
equal "error" AND start with "thread." simultaneously.

This was a regression introduced in commit abc2596 which attempted
to fix indentation but accidentally moved the error handler into the
wrong conditional block.

The fix restructures the logic to:
1. Check for error events first (before any event type routing)
2. Handle thread.* events with their special data structure
3. Handle all other events in the else block

This ensures error events are properly caught and raise APIError
with the appropriate error message.
@jay-dhamale jay-dhamale requested a review from a team as a code owner December 25, 2025 16:54
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