From 775246e3408324e39e886cf8ba4fd0c9e8b19608 Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Wed, 6 Mar 2024 11:59:07 +0000 Subject: [PATCH] fix(streaming): improve error messages https://github.com/openai/openai-python/issues/1160 --- src/openai/_streaming.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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"], )