From b6f7aefece7ddb3c5f62c283a75c5b310a1b8b77 Mon Sep 17 00:00:00 2001 From: jay-dhamale Date: Thu, 25 Dec 2025 22:21:43 +0530 Subject: [PATCH 1/2] fix(streaming): move error event handling outside thread.* condition Fixes #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 abc25966 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. --- src/openai/_streaming.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/openai/_streaming.py b/src/openai/_streaming.py index 61a742668a..107cdfacf0 100644 --- a/src/openai/_streaming.py +++ b/src/openai/_streaming.py @@ -60,11 +60,10 @@ def __stream__(self) -> Iterator[_T]: if sse.data.startswith("[DONE]"): break - # we have to special case the Assistants `thread.` events since we won't have an "event" key in the data - if sse.event and sse.event.startswith("thread."): + # Check for error events first + if sse.event == "error": data = sse.json() - - if sse.event == "error" and is_mapping(data) and data.get("error"): + if is_mapping(data) and data.get("error"): message = None error = data.get("error") if is_mapping(error): @@ -78,6 +77,9 @@ def __stream__(self) -> Iterator[_T]: body=data["error"], ) + # we have to special case the Assistants `thread.` events since we won't have an "event" key in the data + if sse.event and sse.event.startswith("thread."): + data = sse.json() yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response) else: data = sse.json() @@ -163,11 +165,10 @@ async def __stream__(self) -> AsyncIterator[_T]: if sse.data.startswith("[DONE]"): break - # we have to special case the Assistants `thread.` events since we won't have an "event" key in the data - if sse.event and sse.event.startswith("thread."): + # Check for error events first + if sse.event == "error": data = sse.json() - - if sse.event == "error" and is_mapping(data) and data.get("error"): + if is_mapping(data) and data.get("error"): message = None error = data.get("error") if is_mapping(error): @@ -181,6 +182,9 @@ async def __stream__(self) -> AsyncIterator[_T]: body=data["error"], ) + # we have to special case the Assistants `thread.` events since we won't have an "event" key in the data + if sse.event and sse.event.startswith("thread."): + data = sse.json() yield process_data(data={"data": data, "event": sse.event}, cast_to=cast_to, response=response) else: data = sse.json() From 6a18028b8f9db50cebc3e8ed0fdab9a0e1401b2c Mon Sep 17 00:00:00 2001 From: jay-dhamale Date: Thu, 25 Dec 2025 22:42:55 +0530 Subject: [PATCH 2/2] fix(beta/realtime): make model parameter optional for transcription mode Fixes #2652 The Beta AsyncRealtime and Realtime classes required the model parameter, but the OpenAI transcription API rejects requests with model when intent=transcribe is specified. Changes: - Made model parameter optional (str | Omit = omit) in all connect() methods - Conditionally include model in URL query params only when provided - Added Azure client validation to require model for Azure Realtime API - Applied fix to both sync (Realtime) and async (AsyncRealtime) versions This enables transcription mode while maintaining backward compatibility with existing code that explicitly passes model parameter. --- .../resources/beta/realtime/realtime.py | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/openai/resources/beta/realtime/realtime.py b/src/openai/resources/beta/realtime/realtime.py index 4fa35963b6..edab55c613 100644 --- a/src/openai/resources/beta/realtime/realtime.py +++ b/src/openai/resources/beta/realtime/realtime.py @@ -19,7 +19,7 @@ SessionsWithStreamingResponse, AsyncSessionsWithStreamingResponse, ) -from ...._types import NOT_GIVEN, Query, Headers, NotGiven +from ...._types import NOT_GIVEN, Omit, Query, Headers, NotGiven, omit from ...._utils import ( is_azure_client, maybe_transform, @@ -93,7 +93,7 @@ def with_streaming_response(self) -> RealtimeWithStreamingResponse: def connect( self, *, - model: str, + model: str | Omit = omit, extra_query: Query = {}, extra_headers: Headers = {}, websocket_connection_options: WebsocketConnectionOptions = {}, @@ -149,7 +149,7 @@ def with_streaming_response(self) -> AsyncRealtimeWithStreamingResponse: def connect( self, *, - model: str, + model: str | Omit = omit, extra_query: Query = {}, extra_headers: Headers = {}, websocket_connection_options: WebsocketConnectionOptions = {}, @@ -327,7 +327,7 @@ def __init__( self, *, client: AsyncOpenAI, - model: str, + model: str | Omit = omit, extra_query: Query, extra_headers: Headers, websocket_connection_options: WebsocketConnectionOptions, @@ -361,12 +361,15 @@ async def __aenter__(self) -> AsyncRealtimeConnection: await self.__client._refresh_api_key() auth_headers = self.__client.auth_headers if is_async_azure_client(self.__client): - url, auth_headers = await self.__client._configure_realtime(self.__model, extra_query) + model = self.__model + if not model or model is omit: + raise OpenAIError("`model` is required for Azure Realtime API") + url, auth_headers = await self.__client._configure_realtime(model, extra_query) else: url = self._prepare_url().copy_with( params={ **self.__client.base_url.params, - "model": self.__model, + **({"model": self.__model} if self.__model is not omit else {}), **extra_query, }, ) @@ -510,7 +513,7 @@ def __init__( self, *, client: OpenAI, - model: str, + model: str | Omit = omit, extra_query: Query, extra_headers: Headers, websocket_connection_options: WebsocketConnectionOptions, @@ -544,12 +547,15 @@ def __enter__(self) -> RealtimeConnection: self.__client._refresh_api_key() auth_headers = self.__client.auth_headers if is_azure_client(self.__client): - url, auth_headers = self.__client._configure_realtime(self.__model, extra_query) + model = self.__model + if not model or model is omit: + raise OpenAIError("`model` is required for Azure Realtime API") + url, auth_headers = self.__client._configure_realtime(model, extra_query) else: url = self._prepare_url().copy_with( params={ **self.__client.base_url.params, - "model": self.__model, + **({"model": self.__model} if self.__model is not omit else {}), **extra_query, }, )