diff --git a/src/anthropic/_streaming.py b/src/anthropic/_streaming.py index bfb3e821..6a086238 100644 --- a/src/anthropic/_streaming.py +++ b/src/anthropic/_streaming.py @@ -85,7 +85,7 @@ def __stream__(self) -> Iterator[_T]: if sse.event == "completion": yield process_data(data=sse.json(), cast_to=cast_to, response=response) - if ( + elif ( sse.event == "message_start" or sse.event == "message_delta" or sse.event == "message_stop" @@ -100,10 +100,10 @@ def __stream__(self) -> Iterator[_T]: yield process_data(data=data, cast_to=cast_to, response=response) - if sse.event == "ping": + elif sse.event == "ping": continue - if sse.event == "error": + elif sse.event == "error": body = sse.data try: @@ -117,6 +117,10 @@ def __stream__(self) -> Iterator[_T]: body=body, response=self.response, ) + + else: + data = sse.json() + yield process_data(data=data, cast_to=cast_to, response=response) finally: # Ensure the response is closed even if the consumer doesn't read all data response.close() @@ -206,7 +210,7 @@ async def __stream__(self) -> AsyncIterator[_T]: if sse.event == "completion": yield process_data(data=sse.json(), cast_to=cast_to, response=response) - if ( + elif ( sse.event == "message_start" or sse.event == "message_delta" or sse.event == "message_stop" @@ -221,10 +225,10 @@ async def __stream__(self) -> AsyncIterator[_T]: yield process_data(data=data, cast_to=cast_to, response=response) - if sse.event == "ping": + elif sse.event == "ping": continue - if sse.event == "error": + elif sse.event == "error": body = sse.data try: @@ -238,6 +242,10 @@ async def __stream__(self) -> AsyncIterator[_T]: body=body, response=self.response, ) + + else: + data = sse.json() + yield process_data(data=data, cast_to=cast_to, response=response) finally: # Ensure the response is closed even if the consumer doesn't read all data await response.aclose()