Skip to content

Commit

Permalink
fix(streaming): improve error messages (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Mar 6, 2024
1 parent d0c928a commit 4f5ff29
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/openai/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down Expand Up @@ -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"],
)
Expand Down

0 comments on commit 4f5ff29

Please sign in to comment.