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(anthropic): satisfy strict lint and update tests pinned to the dropped served model

kerry07d49364285b
diff --git a/litellm/llms/anthropic/chat/handler.py b/litellm/llms/anthropic/chat/handler.py
index e3912138d8c..58ed9b38ccb 100644
--- a/litellm/llms/anthropic/chat/handler.py
+++ b/litellm/llms/anthropic/chat/handler.py
@@ -1069,7 +1069,8 @@ class ModelResponseIterator:
"""
message_start_block: Final = MessageStartBlock(**chunk)
start_message: Final = message_start_block["message"]
- self.served_model = start_message["model"] if "model" in start_message else None
+ if "model" in start_message:
+ self.served_model = start_message["model"]
if "usage" in message_start_block["message"]:
usage = self._handle_usage(anthropic_usage_chunk=message_start_block["message"]["usage"])
elif type_chunk == "error":
diff --git a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py
index 1e6639ef537..7db44b4709e 100644
--- a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py
+++ b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py
@@ -2756,9 +2756,7 @@ def _served_model_stream_chunks(model: str | None) -> list[dict]:
def test_message_start_model_is_carried_on_stream_chunks():
iterator: Final = ModelResponseIterator(None, sync_stream=True)
- parsed: Final = [
- iterator.chunk_parser(chunk) for chunk in _served_model_stream_chunks("claude-served-1")
- ]
+ parsed: Final = [iterator.chunk_parser(chunk) for chunk in _served_model_stream_chunks("claude-served-1")]
assert all(chunk.model == "claude-served-1" for chunk in parsed)
@@ -2766,9 +2764,7 @@ def test_message_start_model_is_carried_on_stream_chunks():
def test_message_start_without_model_leaves_chunk_model_unset():
iterator: Final = ModelResponseIterator(None, sync_stream=True)
- parsed: Final = [
- iterator.chunk_parser(chunk) for chunk in _served_model_stream_chunks(None)
- ]
+ parsed: Final = [iterator.chunk_parser(chunk) for chunk in _served_model_stream_chunks(None)]
assert all(chunk.model is None for chunk in parsed)
@@ -2777,9 +2773,7 @@ def test_served_model_reaches_assembled_stream_through_custom_stream_wrapper():
from litellm.litellm_core_utils.streaming_handler import CustomStreamWrapper
served_model: Final = "claude-served-1"
- sse_lines: Final = [
- f"data: {json.dumps(chunk)}\n".encode() for chunk in _served_model_stream_chunks(served_model)
- ]
+ sse_lines: Final = [f"data: {json.dumps(chunk)}\n".encode() for chunk in _served_model_stream_chunks(served_model)]
iterator: Final = ModelResponseIterator(iter(sse_lines), sync_stream=True)
wrapper: Final = CustomStreamWrapper(
completion_stream=iter(iterator),
@@ -2793,7 +2787,5 @@ def test_served_model_reaches_assembled_stream_through_custom_stream_wrapper():
assert len(chunks) > 1
for chunk in chunks[1:]:
assert chunk._hidden_params["provider_response_model"] == served_model
- assembled: Final = litellm.stream_chunk_builder(
- chunks=list(chunks), messages=[{"role": "user", "content": "hi"}]
- )
+ assembled: Final = litellm.stream_chunk_builder(chunks=list(chunks), messages=[{"role": "user", "content": "hi"}])
assert assembled._hidden_params["provider_response_model"] == served_model
diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_tool_permission.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_tool_permission.py
index 87a1b84acc5..427fa43ffd5 100644
--- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_tool_permission.py
+++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_tool_permission.py
@@ -1291,8 +1291,9 @@ class TestToolPermissionGuardrailAnthropicMessages:
async def test_rewrite_mode_keeps_the_stream_identity_it_had_before_the_shared_helper(self):
"""Well-formed SSE must round-trip exactly as it did before the helpers were shared.
- The shared module can stamp the upstream message id and model onto the assembled response
- for callers that ask for it; this path never did, and a client reads those bytes.
+ The shared module can stamp the upstream message id onto the assembled response for
+ callers that ask for it; this path never did, and a client reads those bytes. The model,
+ though, is now the upstream's, matching what the untouched passthrough shows clients.
"""
with patch.object(self.rewriting, "should_run_guardrail", return_value=True):
out = await self._drain(self.rewriting, self._sse_chunks("Read"))
@@ -1304,7 +1305,7 @@ class TestToolPermissionGuardrailAnthropicMessages:
if line.startswith("data: ") and json.loads(line[6:]).get("type") == "message_start"
)["message"]
assert message_start["id"].startswith("chatcmpl-"), "the rewritten stream must not adopt the upstream message id"
- assert message_start["model"] == "unknown-model", "the rewritten stream must not adopt the upstream model"
+ assert message_start["model"] == "claude-sonnet-4-5", "the rewritten stream reports the model the upstream served"
@pytest.mark.asyncio
async def test_message_start_without_a_dict_message_fails_closed(self):
diff --git a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py
index 2d7397594aa..ba8b5fa3ac4 100644
--- a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py
+++ b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py
@@ -2541,7 +2541,7 @@ class TestRecordPartialUsageForFailure:
function_id="test-partial-usage-failure",
)
- def _interrupted_chunks(self):
+ def _interrupted_chunks(self, *, model: str = "claude-sonnet-5"):
return [
self._sse(
"message_start",
@@ -2551,7 +2551,7 @@ class TestRecordPartialUsageForFailure:
"id": "msg_abc",
"type": "message",
"role": "assistant",
- "model": "claude-sonnet-5",
+ "model": model,
"content": [],
"stop_reason": None,
"stop_sequence": None,
@@ -2588,7 +2588,7 @@ class TestRecordPartialUsageForFailure:
AnthropicPassthroughLoggingHandler.record_partial_usage_for_failure(
litellm_logging_obj=logging_obj,
request_body={"model": "claude-unpriced-test-model", "stream": True},
- all_chunks=self._interrupted_chunks(),
+ all_chunks=self._interrupted_chunks(model="claude-unpriced-test-model"),
)
usage = logging_obj.model_call_details["combined_usage_object"]