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(streaming_handler): replay a cached completion with no choices as an empty stream
mateo-berri29af8b734913
diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.pyindex 6ae17bac6ff..db23929e0c3 100644--- a/litellm/litellm_core_utils/streaming_handler.py+++ b/litellm/litellm_core_utils/streaming_handler.py@@ -1473,17 +1473,14 @@ class CustomStreamWrapper:self.received_finish_reason = response_obj["finish_reason"]elif self.custom_llm_provider == "cached_response":cached_chunk: Final = cast(ModelResponseStream, chunk)- chunk_finish_reason: Final = cached_chunk.choices[0].finish_reason+ cached_choice: Final = cached_chunk.choices[0] if cached_chunk.choices else None+ chunk_finish_reason: Final = cached_choice.finish_reason if cached_choice is not None else Noneresponse_obj = {- "text": cached_chunk.choices[0].delta.content,+ "text": cached_choice.delta.content if cached_choice is not None else None,"is_finished": chunk_finish_reason is not None,"finish_reason": chunk_finish_reason,"original_chunk": cached_chunk,- "tool_calls": (- cached_chunk.choices[0].delta.tool_calls- if hasattr(cached_chunk.choices[0].delta, "tool_calls")- else None- ),+ "tool_calls": (getattr(cached_choice.delta, "tool_calls", None) if cached_choice is not None else None),}completion_obj["content"] = response_obj["text"]diff --git a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py b/tests/test_litellm/litellm_core_utils/test_streaming_handler.pyindex 0aa73833677..ced7d6d677d 100644--- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py+++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py@@ -6,7 +6,7 @@ import pytestimport asyncioimport traceback-from typing import Optional+from typing import Final, Optionalimport litellmfrom litellm import verbose_logger@@ -2633,6 +2633,47 @@ def test_dispatch_cached_response_extracts_delta(assert initialized_custom_stream_wrapper.response_id == "chatcmpl-cache-1"+def test_dispatch_cached_response_without_choices_is_an_empty_chunk(+ initialized_custom_stream_wrapper: CustomStreamWrapper,+):+ """A cached completion with no choices replays as an empty, unfinished chunk+ instead of raising IndexError on choices[0]."""+ initialized_custom_stream_wrapper.custom_llm_provider = "cached_response"+ chunk: Final = ModelResponseStream(id="chatcmpl-cache-empty", choices=[])++ result, model_response, completion_obj = _run_dispatch(+ initialized_custom_stream_wrapper, chunk+ )++ assert isinstance(result, _ProviderChunkParsed)+ assert completion_obj["content"] is None+ assert initialized_custom_stream_wrapper.received_finish_reason is None+ assert model_response.id == "chatcmpl-cache-empty"+++async def test_cached_response_without_choices_streams_a_single_stop_chunk(+ logging_obj: Logging,+):+ """A stream cache hit on a completion stored with choices == [] ends with one+ finish_reason=stop chunk, the same shape the live empty stream produced."""++ async def cached_chunks():+ yield ModelResponseStream(id="chatcmpl-cache-empty", choices=[])++ wrapper: Final = CustomStreamWrapper(+ completion_stream=cached_chunks(),+ model="test-model",+ logging_obj=logging_obj,+ custom_llm_provider="cached_response",+ )++ chunks: Final = tuple([chunk async for chunk in wrapper])++ assert tuple(choice.finish_reason for chunk in chunks for choice in chunk.choices) == ("stop",)+ assert all(choice.delta.content in (None, "") for chunk in chunks for choice in chunk.choices)++def test_dispatch_vertex_ai_legacy_text_and_finish_reason(initialized_custom_stream_wrapper: CustomStreamWrapper,):