Skip to content

Commit

Permalink
fix: handle errors during streaming (#177)
Browse files Browse the repository at this point in the history
Ref: #176

Signed-off-by: Tomas Dvorak <toomas2d@gmail.com>
  • Loading branch information
Tomas2D committed Sep 27, 2023
1 parent a83bd17 commit 9e794ec
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/genai/services/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import httpx
from httpx import Response
from httpx_sse import connect_sse
from httpx_sse import SSEError, connect_sse

from genai._version import version
from genai.exceptions import GenAiException
from genai.options import Options
from genai.services.connection_manager import ConnectionManager
from genai.utils.http_provider import HttpProvider
Expand Down Expand Up @@ -306,8 +307,15 @@ def post_stream(endpoint, headers, json_data, files):
json=json_data,
files=files,
) as event_source:
for sse in event_source.iter_sse():
yield sse.data
try:
for sse in event_source.iter_sse():
yield sse.data
except SSEError as e:
response: Response = event_source.response
if "application/json" in response.headers["content-type"]:
response.read()
raise GenAiException(response)
raise e

@staticmethod
def get(endpoint: str, key: str, parameters: Optional[dict] = None) -> Response:
Expand Down

0 comments on commit 9e794ec

Please sign in to comment.