Commit 7e2b2544

Stainless Bot <107565488+stainless-bot@users.noreply.github.com>
2023-11-10 18:00:50
fix(client): correctly flush the stream response body (#771)
1 parent 460a282
Changed files (1)
src
src/openai/_streaming.py
@@ -47,8 +47,9 @@ class Stream(Generic[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
 
@@ -63,6 +64,10 @@ class Stream(Generic[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."""
@@ -97,8 +102,9 @@ class AsyncStream(Generic[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
 
@@ -113,6 +119,10 @@ class AsyncStream(Generic[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__(