Skip to content

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]

fix(streaming): guard raise_on_model_repetition against empty choices (#30485)

Shivam Rawat4f54f997f88a
diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py
index f3274151e5a..2d0bd88c79f 100644
--- a/litellm/litellm_core_utils/streaming_handler.py
+++ b/litellm/litellm_core_utils/streaming_handler.py
@@ -295,6 +295,12 @@ class CustomStreamWrapper:
if len(self.chunks) < 2:
return
+ # Providers like Vertex Gemini (Flash / Flash Lite with web search) emit
+ # metadata-only / usage-only chunks with no choices. These get stored in
+ # self.chunks but carry no comparable content, so skip repetition detection.
+ if not self.chunks[-1].choices or not self.chunks[-2].choices:
+ return
+
last_content = self.chunks[-1].choices[0].delta.content
if (
diff --git a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py
index b2002f9a0f9..e88010739c5 100644
--- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py
+++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py
@@ -1509,6 +1509,44 @@ def test_raise_on_model_repetition(
wrapper.raise_on_model_repetition()
+ "empty_chunk_index",
+ [-1, -2],
+ ids=["last_chunk_empty", "second_to_last_chunk_empty"],
+)
+def test_raise_on_model_repetition_tolerates_empty_choices(
+ initialized_custom_stream_wrapper: CustomStreamWrapper,
+ empty_chunk_index: int,
+):
+ """
+ Regression test for https://github.com/BerriAI/litellm/issues/28884
+
+ Vertex Gemini Flash / Flash Lite with web search streaming emits
+ metadata-only and usage-only chunks that carry no choices. These are
+ appended to self.chunks, and raise_on_model_repetition() previously
+ accessed choices[0] unconditionally, raising IndexError mid-stream
+ (surfaced to users as MidStreamFallbackError -> APIConnectionError).
+ """
+ wrapper = initialized_custom_stream_wrapper
+
+ chunks = [
+ _make_chunk("hello world"),
+ ModelResponseStream(
+ id="usage-only",
+ created=1741037890,
+ model="vertex_ai/gemini-3.1-flash-lite",
+ choices=[],
+ usage=Usage(prompt_tokens=10, completion_tokens=0, total_tokens=10),
+ ),
+ ]
+ if empty_chunk_index == -2:
+ chunks.append(_make_chunk("hello world again"))
+
+ for chunk in chunks:
+ wrapper.chunks.append(chunk)
+ wrapper.raise_on_model_repetition()
+
+
def test_usage_chunk_after_finish_reason_updates_hidden_params(logging_obj):
"""
Test that provider-reported usage from a post-finish_reason chunk