mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-15 23:31:29 +00:00
fix(streaming_handler): replay a cached completion with no choices as an empty stream
A stream cache hit on an entry stored with choices == [] indexed choices[0] in the cached_response branch and failed with IndexError, so the streaming converters' empty chunk had no working consumer. The branch now treats a chunk without choices as empty and lets the wrapper close the stream with its usual finish_reason stop chunk
This commit is contained in:
parent
f3a2844080
commit
29af8b7349
2 changed files with 46 additions and 8 deletions
|
|
@ -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 None
|
||||
response_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"]
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import pytest
|
|||
|
||||
import asyncio
|
||||
import traceback
|
||||
from typing import Optional
|
||||
from typing import Final, Optional
|
||||
|
||||
import litellm
|
||||
from 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"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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,
|
||||
):
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue