The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: filter empty SSE lines in BaseModelResponseIterator to prevent extra empty chunks
Chesars16a7e0ce8fe8
diff --git a/litellm/llms/base_llm/base_model_iterator.py b/litellm/llms/base_llm/base_model_iterator.pyindex 6953b1c5878..62cd503a89e 100644--- a/litellm/llms/base_llm/base_model_iterator.py+++ b/litellm/llms/base_llm/base_model_iterator.py@@ -125,26 +125,32 @@ class BaseModelResponseIterator:)def __next__(self):- try:- chunk = self.response_iterator.__next__()- except StopIteration:- raise StopIteration- except ValueError as e:- raise RuntimeError(f"Error receiving chunk from stream: {e}")-- try:- str_line = chunk- if isinstance(chunk, bytes): # Handle binary data- str_line = chunk.decode("utf-8") # Convert bytes to string- index = str_line.find("data:")- if index != -1:- str_line = str_line[index:]- # chunk is a str at this point- return self._handle_string_chunk(str_line=str_line)- except StopIteration:- raise StopIteration- except ValueError as e:- raise RuntimeError(f"Error parsing chunk: {e},\nReceived chunk: {chunk}")+ while True:+ try:+ chunk = self.response_iterator.__next__()+ except StopIteration:+ raise StopIteration+ except ValueError as e:+ raise RuntimeError(f"Error receiving chunk from stream: {e}")++ try:+ str_line = chunk+ if isinstance(chunk, bytes): # Handle binary data+ str_line = chunk.decode("utf-8") # Convert bytes to string+ index = str_line.find("data:")+ if index != -1:+ str_line = str_line[index:]++ # Skip empty lines (common in SSE streams between events)+ if not str_line or not str_line.strip():+ continue++ # chunk is a str at this point+ return self._handle_string_chunk(str_line=str_line)+ except StopIteration:+ raise StopIteration+ except ValueError as e:+ raise RuntimeError(f"Error parsing chunk: {e},\nReceived chunk: {chunk}")# Async iteratordef __aiter__(self):@@ -152,30 +158,35 @@ class BaseModelResponseIterator:return selfasync def __anext__(self):- try:- chunk = await self.async_response_iterator.__anext__()-- except StopAsyncIteration:- raise StopAsyncIteration- except ValueError as e:- raise RuntimeError(f"Error receiving chunk from stream: {e}")-- try:- str_line = chunk- if isinstance(chunk, bytes): # Handle binary data- str_line = chunk.decode("utf-8") # Convert bytes to string- index = str_line.find("data:")- if index != -1:- str_line = str_line[index:]-- # chunk is a str at this point- chunk = self._handle_string_chunk(str_line=str_line)-- return chunk- except StopAsyncIteration:- raise StopAsyncIteration- except ValueError as e:- raise RuntimeError(f"Error parsing chunk: {e},\nReceived chunk: {chunk}")+ while True:+ try:+ chunk = await self.async_response_iterator.__anext__()++ except StopAsyncIteration:+ raise StopAsyncIteration+ except ValueError as e:+ raise RuntimeError(f"Error receiving chunk from stream: {e}")++ try:+ str_line = chunk+ if isinstance(chunk, bytes): # Handle binary data+ str_line = chunk.decode("utf-8") # Convert bytes to string+ index = str_line.find("data:")+ if index != -1:+ str_line = str_line[index:]++ # Skip empty lines (common in SSE streams between events)+ if not str_line or not str_line.strip():+ continue++ # chunk is a str at this point+ chunk = self._handle_string_chunk(str_line=str_line)++ return chunk+ except StopAsyncIteration:+ raise StopAsyncIteration+ except ValueError as e:+ raise RuntimeError(f"Error parsing chunk: {e},\nReceived chunk: {chunk}")class MockResponseIterator: # for returning ai21 streaming responses