@@ -341,18 +341,22 @@ def _iter_response_stream(self) -> Iterator[str]:
341341
342342 chunk = ''
343343 balance = 0
344+ data_buffer : list [str ] = []
344345 if isinstance (self .response_stream , httpx .Response ):
345346 response_stream = self .response_stream .iter_lines ()
346347 else :
347348 response_stream = self .response_stream .iter_lines (decode_unicode = True )
348349 for line in response_stream :
349350 if not line :
351+ if data_buffer :
352+ yield '\n ' .join (data_buffer )
353+ data_buffer = []
350354 continue
351355
352356 # In streaming mode, the response of JSON is prefixed with "data: " which
353357 # we must strip before parsing.
354358 if line .startswith ('data: ' ):
355- yield line [len ('data: ' ):]
359+ data_buffer . append ( line [len ('data: ' ):])
356360 continue
357361
358362 # When API returns an error message, it comes line by line. So we buffer
@@ -372,6 +376,8 @@ def _iter_response_stream(self) -> Iterator[str]:
372376 # If there is any remaining chunk, yield it.
373377 if chunk :
374378 yield chunk
379+ if data_buffer :
380+ yield '\n ' .join (data_buffer )
375381
376382 async def _aiter_response_stream (self ) -> AsyncIterator [str ]:
377383 """Asynchronously iterates over chunks retrieved from the API."""
@@ -387,16 +393,20 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
387393
388394 chunk = ''
389395 balance = 0
396+ data_buffer : list [str ] = []
390397 # httpx.Response has a dedicated async line iterator.
391398 if isinstance (self .response_stream , httpx .Response ):
392399 try :
393400 async for line in self .response_stream .aiter_lines ():
394401 if not line :
402+ if data_buffer :
403+ yield '\n ' .join (data_buffer )
404+ data_buffer = []
395405 continue
396406 # In streaming mode, the response of JSON is prefixed with "data: "
397407 # which we must strip before parsing.
398408 if line .startswith ('data: ' ):
399- yield line [len ('data: ' ):]
409+ data_buffer . append ( line [len ('data: ' ):])
400410 continue
401411
402412 # When API returns an error message, it comes line by line. So we buffer
@@ -415,6 +425,8 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
415425 # If there is any remaining chunk, yield it.
416426 if chunk :
417427 yield chunk
428+ if data_buffer :
429+ yield '\n ' .join (data_buffer )
418430 finally :
419431 # Close the response and release the connection.
420432 await self .response_stream .aclose ()
@@ -432,12 +444,15 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
432444 # Decode the bytes and remove trailing whitespace and newlines.
433445 line = line_bytes .decode ('utf-8' ).rstrip ()
434446 if not line :
447+ if data_buffer :
448+ yield '\n ' .join (data_buffer )
449+ data_buffer = []
435450 continue
436451
437452 # In streaming mode, the response of JSON is prefixed with "data: "
438453 # which we must strip before parsing.
439454 if line .startswith ('data: ' ):
440- yield line [len ('data: ' ):]
455+ data_buffer . append ( line [len ('data: ' ):])
441456 continue
442457
443458 # When API returns an error message, it comes line by line. So we
@@ -456,6 +471,8 @@ async def _aiter_response_stream(self) -> AsyncIterator[str]:
456471 # If there is any remaining chunk, yield it.
457472 if chunk :
458473 yield chunk
474+ if data_buffer :
475+ yield '\n ' .join (data_buffer )
459476 finally :
460477 # Release the connection back to the pool for potential reuse.
461478 self .response_stream .release ()
0 commit comments