remove infinit loop for streaming

This commit is contained in:
Ishaan Jaff 2025-03-12 11:55:17 -07:00
parent 9ea0c89a2d
commit 1f7c21fd1b

View file

@ -109,21 +109,21 @@ class ResponsesAPIStreamingIterator(BaseResponsesAPIStreamingIterator):
async def __anext__(self) -> ResponsesAPIStreamingResponse: async def __anext__(self) -> ResponsesAPIStreamingResponse:
try: try:
# Get the next chunk from the stream while True:
try: # Get the next chunk from the stream
chunk = await self.stream_iterator.__anext__() try:
except StopAsyncIteration: chunk = await self.stream_iterator.__anext__()
self.finished = True except StopAsyncIteration:
raise StopAsyncIteration self.finished = True
raise StopAsyncIteration
result = self._process_chunk(chunk) result = self._process_chunk(chunk)
if self.finished: if self.finished:
raise StopAsyncIteration raise StopAsyncIteration
elif result is not None: elif result is not None:
return result return result
else: # If result is None, continue the loop to get the next chunk
return await self.__anext__()
except httpx.HTTPError as e: except httpx.HTTPError as e:
# Handle HTTP errors # Handle HTTP errors
@ -170,21 +170,21 @@ class SyncResponsesAPIStreamingIterator(BaseResponsesAPIStreamingIterator):
def __next__(self): def __next__(self):
try: try:
# Get the next chunk from the stream while True:
try: # Get the next chunk from the stream
chunk = next(self.stream_iterator) try:
except StopIteration: chunk = next(self.stream_iterator)
self.finished = True except StopIteration:
raise StopIteration self.finished = True
raise StopIteration
result = self._process_chunk(chunk) result = self._process_chunk(chunk)
if self.finished: if self.finished:
raise StopIteration raise StopIteration
elif result is not None: elif result is not None:
return result return result
else: # If result is None, continue the loop to get the next chunk
return self.__next__()
except httpx.HTTPError as e: except httpx.HTTPError as e:
# Handle HTTP errors # Handle HTTP errors