Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def _response(response: ModelResponse) -> dict[str, object]:
"name": tool_call.function.name,
"arguments": arguments,
})
if not content:
if not content and choice.finish_reason != "content_filter":
raise ValueError("LiteLLM returned no text content")
return {
"id": response.id,
Expand Down
44 changes: 44 additions & 0 deletions examples/experimental/litellm/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from litellm.exceptions import (
ContextWindowExceededError as LiteLLMContextWindowExceededError,
)
from litellm.types.utils import Choices, Message, Usage
from switchyard_litellm import LiteLLMSyClient

from switchyard.libsy import ContextWindowExceededError
Expand Down Expand Up @@ -582,3 +583,46 @@ async def test_cached_token_count_preserves_explicit_zero() -> None:
await client.aclose()

assert response["usage"]["cached_input_tokens"] == 0


def test_response_content_filter_empty_content() -> None:
"""Empty content with a content_filter finish reason must be normalized."""
from switchyard_litellm.client import _response

response = ModelResponse(
id="chatcmpl-test",
model="openai/strong",
choices=[
Choices(
message=Message(content=None, tool_calls=None),
finish_reason="content_filter",
)
],
usage=Usage(prompt_tokens=0, completion_tokens=0, total_tokens=0),
)
result = _response(response)
outputs = result["outputs"]
assert isinstance(outputs, list)
output = outputs[0]
assert isinstance(output, dict)
assert output["content"] == []
assert output["stop_reason"] == "content_filter"


def test_response_empty_content_without_content_filter_raises() -> None:
"""Empty content with any other finish reason must still raise."""
from switchyard_litellm.client import _response

response = ModelResponse(
id="chatcmpl-test",
model="openai/strong",
choices=[
Choices(
message=Message(content=None, tool_calls=None),
finish_reason="stop",
)
],
usage=Usage(prompt_tokens=0, completion_tokens=0, total_tokens=0),
)
with pytest.raises(ValueError, match="LiteLLM returned no text content"):
_response(response)
Loading