Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): correctly flush the stream response body #771

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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