From e05a9aca744176463971326427f20ddf685db6b8 Mon Sep 17 00:00:00 2001 From: Maxwell Calkin <101308415+MaxwellCalkin@users.noreply.github.com> Date: Sun, 8 Mar 2026 03:25:11 -0400 Subject: [PATCH] fix(streaming): move unreachable error event check out of thread. branch The `sse.event == "error"` check in both `Stream.__stream__` and `AsyncStream.__stream__` was nested inside the `if sse.event.startswith("thread.")` branch, making it dead code since "error" never starts with "thread.". This was introduced in commit 48188cc when the branch logic was refactored to check for `thread.` events first instead of `response.*`/`transcript.*` events. The error check was left in the wrong branch, and the subsequent indentation fix in abc25966 preserved the mistake. The fix moves the error event handling into its own `elif` branch so it can actually be reached when the server sends an SSE with `event: error`. --- src/openai/_streaming.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/openai/_streaming.py b/src/openai/_streaming.py index 45c13cc11d..7896a57456 100644 --- a/src/openai/_streaming.py +++ b/src/openai/_streaming.py @@ -67,7 +67,10 @@ def __stream__(self) -> Iterator[_T]: if sse.event and sse.event.startswith("thread."): data = sse.json() - if sse.event == "error" and is_mapping(data) and data.get("error"): + yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response) + elif sse.event == "error": + data = sse.json() + if is_mapping(data) and data.get("error"): message = None error = data.get("error") if is_mapping(error): @@ -80,8 +83,6 @@ def __stream__(self) -> Iterator[_T]: request=self.response.request, body=data["error"], ) - - yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response) else: data = sse.json() if is_mapping(data) and data.get("error"): @@ -177,7 +178,10 @@ async def __stream__(self) -> AsyncIterator[_T]: if sse.event and sse.event.startswith("thread."): data = sse.json() - if sse.event == "error" and is_mapping(data) and data.get("error"): + yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response) + elif sse.event == "error": + data = sse.json() + if is_mapping(data) and data.get("error"): message = None error = data.get("error") if is_mapping(error): @@ -190,8 +194,6 @@ async def __stream__(self) -> AsyncIterator[_T]: request=self.response.request, body=data["error"], ) - - yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response) else: data = sse.json() if is_mapping(data) and data.get("error"):