Skip to content

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

@joyceleeor

Description

@joyceleeor

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

In src/openai/_streaming.py lines 169-181, there's unreachable dead code.

The outer condition requires sse.event.startswith("thread."), but the inner condition checks sse.event == "error". Since "error" does not start with "thread.", this check can never be true.

This appears to be a regression from commit abc25966 ("fix(streaming): correct indentation"). The original code had the error check in the else branch, but after refactors it was incorrectly moved inside the thread. branch.

Reference: https://github.com/openai/openai-python/blob/main/src/openai/_streaming.py#L169-L181

To Reproduce

  1. Open src/openai/_streaming.py
  2. Go to line 167-181
  3. Observe that sse.event == "error" is inside if sse.event.startswith("thread.") block
  4. This condition is logically impossible to satisfy

Code snippets

# Line 167-168: outer condition
if sse.event and sse.event.startswith("thread."):
    data = sse.json()

    # Line 170: inner condition - ALWAYS False
    # "error" does not start with "thread.", so this is unreachable
    if sse.event == "error" and is_mapping(data) and data.get("error"):
        raise APIError(...)  # Dead code!

OS

Windows

Python version

Python v3.11.4

Library version

openai v2.11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions