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]

Merge commit '36b8be7d81b' into litellm_mantle_native_anthropic_messages_b4dc

mateo-berri4eed951e6f1e
diff --cc tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py
index 9fa3ef153be,e8bfcb86bf6..cc4eb1d4136
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_anthropic_experimental_pass_through_messages_handler.py
@@@ -1440,43 -1440,107 +1440,147 @@@ async def test_anthropic_messages_leave
assert "Traceback" not in str(excinfo.value)
+def _recording_client(seen_urls: list[str]) -> AsyncHTTPHandler:
+ def record_and_answer(request: httpx.Request) -> httpx.Response:
+ seen_urls.append(str(request.url))
+ return httpx.Response(
+ 200,
+ json={
+ "id": "msg_test",
+ "type": "message",
+ "role": "assistant",
+ "model": "deepseek-chat",
+ "content": [{"type": "text", "text": "pong"}],
+ "stop_reason": "end_turn",
+ "stop_sequence": None,
+ "usage": {"input_tokens": 3, "output_tokens": 1},
+ },
+ )
+
+ upstream = AsyncHTTPHandler()
+ upstream.client = httpx.AsyncClient(transport=httpx.MockTransport(record_and_answer))
+ return upstream
+
+
+async def test_provider_messages_api_base_env_is_not_shadowed_by_the_chat_default(monkeypatch):
+ from litellm.llms.anthropic.experimental_pass_through.messages import handler
+
+ monkeypatch.delenv("DEEPSEEK_API_BASE", raising=False)
+ monkeypatch.setenv("DEEPSEEK_ANTHROPIC_API_BASE", "https://deepseek.internal.example/anthropic")
+ seen_urls: list[str] = []
+
+ await handler.anthropic_messages(
+ max_tokens=16,
+ messages=[{"role": "user", "content": "ping"}],
+ model="deepseek/deepseek-chat",
+ api_key="sk-test",
+ client=_recording_client(seen_urls),
+ )
+
+ assert seen_urls == ["https://deepseek.internal.example/anthropic/v1/messages"]
+
+ @pytest.mark.asyncio
+ async def test_anthropic_messages_forwards_safeguards_and_unknown_beta_to_anthropic():
+ """Shapes are what Claude Code 2.1.278 sends and api.anthropic.com returns, captured 2026-09-21."""
+ from litellm.llms.anthropic.experimental_pass_through.messages import handler
+
+ safeguards = [{"type": "dangerous_tool_use", "classifier_context": {"v": 1, "permission_mode": "auto"}}]
+ client_betas = "dangerous-tool-use-2026-09-03,interleaved-thinking-2025-05-14"
+ safeguard_results = [{"type": "dangerous_tool_use", "status": {"type": "available", "tool_uses": {}}}]
+ captured: dict[str, object] = {}
+
+ def upstream_records_the_request(request: httpx.Request) -> httpx.Response:
+ captured["body"] = json.loads(request.content)
+ captured["anthropic-beta"] = request.headers.get("anthropic-beta")
+ return httpx.Response(
+ 200,
+ json={
+ "id": "msg_1",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [{"type": "text", "text": "ok"}],
+ "stop_reason": "end_turn",
+ "stop_sequence": None,
+ "usage": {"input_tokens": 1, "output_tokens": 1},
+ "safeguard_results": safeguard_results,
+ },
+ request=request,
+ )
+
+ upstream = AsyncHTTPHandler()
+ upstream.client = httpx.AsyncClient(transport=httpx.MockTransport(upstream_records_the_request))
+
+ response = await handler.anthropic_messages(
+ max_tokens=16,
+ messages=[{"role": "user", "content": "hi"}],
+ model="anthropic/claude-haiku-4-5",
+ custom_llm_provider="anthropic",
+ api_key="sk-test",
+ client=upstream,
+ safeguards=safeguards,
+ extra_headers={"anthropic-beta": client_betas},
+ )
+
+ assert captured["body"]["safeguards"] == safeguards
+ assert set(captured["anthropic-beta"].split(",")) == set(client_betas.split(","))
+ assert response["safeguard_results"] == safeguard_results
+
+
+ @pytest.mark.asyncio
+ async def test_anthropic_messages_streaming_forwards_safeguards_and_keeps_safeguard_results():
+ """Shapes are what Claude Code 2.1.278 sends and api.anthropic.com returns, captured 2026-09-21."""
+ from litellm.llms.anthropic.experimental_pass_through.messages import handler
+
+ safeguards = [{"type": "dangerous_tool_use", "classifier_context": {"v": 1, "permission_mode": "auto"}}]
+ tool_verdicts = {"toolu_01": {"type": "evaluated", "outcome": "not_flagged"}}
+ safeguard_results = [{"type": "dangerous_tool_use", "status": {"type": "available", "tool_uses": tool_verdicts}}]
+ captured: dict[str, object] = {}
+ message_start = {
+ "type": "message_start",
+ "message": {
+ "id": "msg_1",
+ "type": "message",
+ "role": "assistant",
+ "model": "claude-haiku-4-5",
+ "content": [],
+ "stop_reason": None,
+ "stop_sequence": None,
+ "usage": {"input_tokens": 1, "output_tokens": 0},
+ "safeguard_results": safeguard_results,
+ },
+ }
+ message_delta = {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn", "stop_sequence": None, "safeguard_results": safeguard_results},
+ "usage": {"output_tokens": 1},
+ }
+ sse = "".join(
+ f"event: {event['type']}\ndata: {json.dumps(event)}\n\n"
+ for event in (message_start, message_delta, {"type": "message_stop"})
+ )
+
+ def upstream_streams_safeguard_results(request: httpx.Request) -> httpx.Response:
+ captured["body"] = json.loads(request.content)
+ return httpx.Response(200, headers={"content-type": "text/event-stream"}, content=sse.encode(), request=request)
+
+ upstream = AsyncHTTPHandler()
+ upstream.client = httpx.AsyncClient(transport=httpx.MockTransport(upstream_streams_safeguard_results))
+
+ stream = await handler.anthropic_messages(
+ max_tokens=16,
+ messages=[{"role": "user", "content": "hi"}],
+ model="anthropic/claude-haiku-4-5",
+ custom_llm_provider="anthropic",
+ api_key="sk-test",
+ client=upstream,
+ stream=True,
+ safeguards=safeguards,
+ )
+ raw = b"".join([chunk async for chunk in stream]).decode()
+ events = [json.loads(line[len("data: ") :]) for line in raw.splitlines() if line.startswith("data: ")]
+
+ assert captured["body"]["safeguards"] == safeguards
+ assert events[0]["message"]["safeguard_results"] == safeguard_results
+ assert [e for e in events if e["type"] == "message_delta"][0]["delta"]["safeguard_results"] == safeguard_results