Skip to content

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]

fix(guardrails): tolerate a model-less request when translating Anthropic response context

yucheng43ae9aff3dc2
diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
index 8ff9f2e0679..ed01d16bd1b 100644
--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
@@ -1180,7 +1180,7 @@ class LiteLLMAnthropicMessagesAdapter:
self._add_system_message_to_messages(new_messages, anthropic_message_request)
new_kwargs: Final[ChatCompletionRequest] = {
- "model": anthropic_message_request["model"],
+ "model": anthropic_message_request.get("model", ""),
"messages": new_messages,
}
## CONVERT METADATA (user_id + litellm metadata)
diff --git a/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py b/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py
index 7c82028ddbc..92d3d485c3f 100644
--- a/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py
+++ b/tests/test_litellm/llms/anthropic/chat/guardrail_translation/test_anthropic_guardrail_handler.py
@@ -2793,3 +2793,19 @@ class TestAnthropicResponseScanCarriesRequestConversation:
]
assert inputs["structured_messages"][-1] == {"role": "assistant", "content": "Paris is the capital"}
assert inputs["tools"][0]["function"]["name"] == "run_shell"
+
+ @pytest.mark.asyncio
+ async def test_streaming_response_scan_survives_a_request_without_a_model(self):
+ handler = AnthropicMessagesHandler()
+ guardrail = TypedInputsRecordingGuardrail()
+ request = {key: value for key, value in self._request().items() if key != "model"}
+
+ await handler.process_output_streaming_response(
+ responses_so_far=self._sse_chunks(ended=True),
+ guardrail_to_apply=guardrail,
+ litellm_logging_obj=MagicMock(),
+ request_data=request,
+ )
+
+ [(_, inputs)] = guardrail.seen
+ assert [m["role"] for m in inputs["structured_messages"]] == ["system", "user", "assistant", "tool", "assistant"]