mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-15 04:19:08 +00:00
fix: address review feedback on PR #32103
Fixed both P1 edge cases Greptile flagged in _get_mock_response_content (empty choices list and None message content) by returning an empty string to still short-circuit rather than crashing or falling through to the real model, and added two regression tests covering them.
This commit is contained in:
parent
262e596b9b
commit
ac87eb0eae
2 changed files with 53 additions and 1 deletions
|
|
@ -368,9 +368,12 @@ def _get_mock_response_content(
|
|||
if isinstance(mock_response, str):
|
||||
return mock_response
|
||||
if isinstance(mock_response, litellm.ModelResponse):
|
||||
if not mock_response.choices:
|
||||
return ""
|
||||
first_choice = mock_response.choices[0]
|
||||
if isinstance(first_choice, litellm.Choices):
|
||||
return first_choice.message.content
|
||||
return first_choice.message.content or ""
|
||||
return ""
|
||||
return None
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -676,6 +676,55 @@ def test_handler_short_circuits_on_model_response_mock_response():
|
|||
assert result["content"][0]["text"] == "Blocked by guardrail"
|
||||
|
||||
|
||||
def test_handler_short_circuits_on_model_response_with_empty_choices():
|
||||
"""A ModelResponse mock with no choices must still short-circuit instead
|
||||
of crashing on an unguarded choices[0] access."""
|
||||
from litellm.llms.anthropic.experimental_pass_through.messages import handler
|
||||
|
||||
blocked_response = ModelResponse(choices=[], model="bedrock-guardrail")
|
||||
|
||||
with patch.object(
|
||||
handler.base_llm_http_handler,
|
||||
"anthropic_messages_handler",
|
||||
) as mock_base_handler:
|
||||
result = handler.anthropic_messages_handler(
|
||||
max_tokens=10,
|
||||
messages=[{"role": "user", "content": "some blocked content"}],
|
||||
model="anthropic/claude-3-5-sonnet-20241022",
|
||||
custom_llm_provider="anthropic",
|
||||
mock_response=blocked_response,
|
||||
)
|
||||
|
||||
mock_base_handler.assert_not_called()
|
||||
assert result["content"][0]["text"] == ""
|
||||
|
||||
|
||||
def test_handler_short_circuits_on_model_response_with_none_content():
|
||||
"""A ModelResponse mock whose message content is None must still
|
||||
short-circuit rather than silently falling through to the real model."""
|
||||
from litellm.llms.anthropic.experimental_pass_through.messages import handler
|
||||
|
||||
blocked_response = ModelResponse(
|
||||
choices=[Choices(message=Message(content=None))],
|
||||
model="bedrock-guardrail",
|
||||
)
|
||||
|
||||
with patch.object(
|
||||
handler.base_llm_http_handler,
|
||||
"anthropic_messages_handler",
|
||||
) as mock_base_handler:
|
||||
result = handler.anthropic_messages_handler(
|
||||
max_tokens=10,
|
||||
messages=[{"role": "user", "content": "some blocked content"}],
|
||||
model="anthropic/claude-3-5-sonnet-20241022",
|
||||
custom_llm_provider="anthropic",
|
||||
mock_response=blocked_response,
|
||||
)
|
||||
|
||||
mock_base_handler.assert_not_called()
|
||||
assert result["content"][0]["text"] == ""
|
||||
|
||||
|
||||
def test_presanitized_flag_not_leaked_to_provider_params():
|
||||
"""The private sentinel must be popped, never forwarded as a request param."""
|
||||
from litellm.llms.anthropic.experimental_pass_through.messages import handler
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue