Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import re
import time
import traceback
from collections.abc import Iterable, Sequence
from collections.abc import Sequence
from typing import Final, Literal, cast

import litellm
Expand Down Expand Up @@ -179,7 +179,7 @@ async def convert_to_streaming_response_async(

choice_list: Final[list[StreamingChoices]] = []

if not response_object.get("choices"):
if not isinstance(response_object.get("choices"), list):
from litellm.exceptions import APIError

raise APIError(
Expand Down Expand Up @@ -287,7 +287,7 @@ def convert_to_streaming_response(
model_response_object: Final = ModelResponseStream()
choice_list: Final[list[StreamingChoices]] = []

if not response_object.get("choices"):
if not isinstance(response_object.get("choices"), list):
from litellm.exceptions import APIError

raise APIError(
Expand Down Expand Up @@ -623,7 +623,7 @@ def convert_to_model_response_object(
return convert_to_streaming_response(response_object=response_object)
choice_list: Final[list[Choices]] = []

if not response_object.get("choices") or not isinstance(response_object["choices"], Iterable):
if not isinstance(response_object.get("choices"), list):
from litellm.exceptions import APIError

raise APIError(
Expand Down
11 changes: 4 additions & 7 deletions litellm/litellm_core_utils/streaming_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1473,17 +1473,14 @@ def _dispatch_provider_chunk(
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),
Comment thread
mateo-berri marked this conversation as resolved.
Resolved
}

completion_obj["content"] = response_obj["text"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,9 @@ def translate_openai_response_to_anthropic(
anthropic_content.insert(0, polyfill_result.compaction_block)

## extract finish reason
openai_finish_reason: Final = response.choices[0].finish_reason if response.choices else "stop"
translated_finish_reason: Final = self._translate_openai_finish_reason_to_anthropic(
openai_finish_reason=response.choices[0].finish_reason
openai_finish_reason=openai_finish_reason
)
anthropic_finish_reason: Final = (
"refusal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1623,29 +1623,26 @@ def test_convert_to_model_response_object_no_choices_raises_api_error(self):

assert "no 'choices'" in exc_info.value.message

def test_convert_to_model_response_object_empty_choices_raises_api_error(self):
"""Empty choices list raises APIError, same as missing/null choices.
def test_convert_to_model_response_object_empty_choices_returns_empty_list(self):
"""An empty choices list is a real provider answer, so it converts to choices=[] instead of raising.

Provider-specific repair (e.g. github_copilot synthesizing choices for
Anthropic-native responses) happens before this guard, in the provider
config; the core utility keeps treating empty choices as an error.
See: https://github.com/BerriAI/litellm/issues/40276
"""
from litellm.exceptions import APIError

response_object = {
"id": "msg_123",
"model": "some-model",
"choices": [],
"usage": {"prompt_tokens": 10, "completion_tokens": 1, "total_tokens": 11},
}

with pytest.raises(APIError) as exc_info:
convert_to_model_response_object(
response_object=response_object,
model_response_object=ModelResponse(),
)
result = convert_to_model_response_object(
response_object=response_object,
model_response_object=ModelResponse(),
)

assert "no 'choices'" in exc_info.value.message
assert isinstance(result, ModelResponse)
assert result.choices == []
assert result.usage.prompt_tokens == 10

def test_convert_to_model_response_object_null_choices_raises_api_error(self):
"""choices=None raises APIError."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Final

import pytest

from litellm.constants import RESPONSE_FORMAT_TOOL_NAME
from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (
Expand Down Expand Up @@ -99,3 +101,96 @@ def test_handle_invalid_parallel_tool_calls_skips_custom_tool_calls():
)
result = _handle_invalid_parallel_tool_calls([custom_tool_call, function_tool_call])
assert result == [custom_tool_call, function_tool_call]


def test_convert_empty_choices_response() -> None:
from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (
convert_to_streaming_response,
)

resp: Final = {
"id": "x",
"created": 1,
"model": "gemini-3.5-flash",
"object": "chat.completion",
"choices": [],
"usage": {"prompt_tokens": 10, "completion_tokens": 0, "total_tokens": 10},
"vertex_ai_safety_results": ["blocked"],
}
result: Final = convert_to_model_response_object(
response_object=resp,
model_response_object=ModelResponse(),
response_type="completion",
)
assert result.choices == []
assert getattr(result, "vertex_ai_safety_results") == ["blocked"]

sync_stream: Final = list(convert_to_streaming_response(response_object=resp))
assert len(sync_stream) == 1
assert sync_stream[0].choices == []


@pytest.mark.asyncio
async def test_convert_empty_choices_response_async() -> None:
from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (
convert_to_streaming_response_async,
)

resp: Final = {
"id": "x",
"created": 1,
"model": "gemini-3.5-flash",
"object": "chat.completion",
"choices": [],
"usage": {"prompt_tokens": 10, "completion_tokens": 0, "total_tokens": 10},
}
async_chunks: Final = [chunk async for chunk in convert_to_streaming_response_async(response_object=resp)]
assert len(async_chunks) == 1
assert async_chunks[0].choices == []


def test_convert_missing_choices_raises_api_error() -> None:
from litellm.exceptions import APIError

resp: Final = {
"id": "x",
"created": 1,
"model": "gemini-3.5-flash",
"object": "chat.completion",
}
with pytest.raises(APIError) as exc_info:
convert_to_model_response_object(
response_object=resp,
model_response_object=ModelResponse(),
response_type="completion",
)
assert "no 'choices'" in str(exc_info.value)


@pytest.mark.parametrize("choices", [{}, "", None, 0])
@pytest.mark.asyncio
async def test_convert_non_list_choices_raises_api_error(choices: object) -> None:
from litellm.exceptions import APIError
from litellm.litellm_core_utils.llm_response_utils.convert_dict_to_response import (
convert_to_streaming_response,
convert_to_streaming_response_async,
)

resp: Final = {
"id": "x",
"created": 1,
"model": "gemini-3.5-flash",
"object": "chat.completion",
"choices": choices,
}
with pytest.raises(APIError, match="no 'choices'"):
convert_to_model_response_object(
response_object=resp,
model_response_object=ModelResponse(),
response_type="completion",
)
with pytest.raises(APIError, match="no 'choices'"):
list(convert_to_streaming_response(response_object=resp))
with pytest.raises(APIError, match="no 'choices'"):
async for _ in convert_to_streaming_response_async(response_object=resp):
pass
43 changes: 42 additions & 1 deletion tests/test_litellm/litellm_core_utils/test_streaming_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import asyncio
import traceback
from typing import Optional
from typing import Final, Optional

import litellm
from litellm import verbose_logger
Expand Down Expand Up @@ -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,
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@
)


def test_translate_openai_response_to_anthropic_empty_choices() -> None:
response: Final = ModelResponse(
id="chatcmpl-empty",
model="gemini-3.5-flash",
choices=[],
usage=Usage(prompt_tokens=10, completion_tokens=0, total_tokens=10),
)

result: Final = LiteLLMAnthropicMessagesAdapter().translate_openai_response_to_anthropic(response)

assert result["content"] == []
assert result["stop_reason"] == "end_turn"
assert result["usage"]["input_tokens"] == 10


def test_translate_chat_refusal_to_anthropic_response():
response = ModelResponse(
id="chatcmpl-refusal",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_litellm/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_completion_missing_role(openai_api_response):
print(f"openai_api_response: {openai_api_response}")

with patch.object(
client.chat.completions.with_raw_response, "create", mock_raw_response
client.chat.completions.with_raw_response, "create", MagicMock(return_value=mock_raw_response)
) as mock_create:
litellm.completion(
model="gpt-4o-mini",
Expand Down
Loading