Skip to content

Commit

Permalink
fix(client): correctly flush the stream response body (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Nov 10, 2023
1 parent c18e5ed commit a60d543
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/anthropic/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ def __stream__(self) -> Iterator[ResponseT]:
cast_to = self._cast_to
response = self.response
process_data = self._client._process_response_data
iterator = self._iter_events()

for sse in self._iter_events():
for sse in iterator:
if sse.event == "completion":
yield process_data(data=sse.json(), cast_to=cast_to, response=response)

Expand All @@ -68,6 +69,10 @@ def __stream__(self) -> Iterator[ResponseT]:
response=self.response,
)

# Ensure the entire stream is consumed
for sse in iterator:
...


class AsyncStream(Generic[ResponseT]):
"""Provides the core interface to iterate over an asynchronous stream response."""
Expand Down Expand Up @@ -102,8 +107,9 @@ async def __stream__(self) -> AsyncIterator[ResponseT]:
cast_to = self._cast_to
response = self.response
process_data = self._client._process_response_data
iterator = self._iter_events()

async for sse in self._iter_events():
async for sse in iterator:
if sse.event == "completion":
yield process_data(data=sse.json(), cast_to=cast_to, response=response)

Expand All @@ -125,6 +131,10 @@ async def __stream__(self) -> AsyncIterator[ResponseT]:
response=self.response,
)

# Ensure the entire stream is consumed
async for sse in iterator:
...


class ServerSentEvent:
def __init__(
Expand Down

0 comments on commit a60d543

Please sign in to comment.