Skip to content
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