diff --git a/src/openai/_streaming.py b/src/openai/_streaming.py index 2bc8d6a14d..41ed11074f 100644 --- a/src/openai/_streaming.py +++ b/src/openai/_streaming.py @@ -65,8 +65,15 @@ def __stream__(self) -> Iterator[_T]: if sse.event is None: data = sse.json() if is_mapping(data) and data.get("error"): + message = None + error = data.get("error") + if is_mapping(error): + message = error.get("message") + if not message or not isinstance(message, str): + message = "An error occurred during streaming" + raise APIError( - message="An error occurred during streaming", + message=message, request=self.response.request, body=data["error"], ) @@ -145,8 +152,15 @@ async def __stream__(self) -> AsyncIterator[_T]: if sse.event is None: data = sse.json() if is_mapping(data) and data.get("error"): + message = None + error = data.get("error") + if is_mapping(error): + message = error.get("message") + if not message or not isinstance(message, str): + message = "An error occurred during streaming" + raise APIError( - message="An error occurred during streaming", + message=message, request=self.response.request, body=data["error"], )