Skip to content

Commit

Permalink
fix(client): correctly flush the stream response body (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Nov 10, 2023
1 parent 460a282 commit 7e2b254
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/openai/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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.data.startswith("[DONE]"):
break

Expand All @@ -63,6 +64,10 @@ def __stream__(self) -> Iterator[ResponseT]:

yield process_data(data=data, cast_to=cast_to, response=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 @@ -97,8 +102,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.data.startswith("[DONE]"):
break

Expand All @@ -113,6 +119,10 @@ async def __stream__(self) -> AsyncIterator[ResponseT]:

yield process_data(data=data, cast_to=cast_to, response=response)

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


class ServerSentEvent:
def __init__(
Expand Down

0 comments on commit 7e2b254

Please sign in to comment.