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): map only provider failures on the /v1/messages boundary

mateo-berri5cfc1608f95d
diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py
index 7459d1b2da5..283c706e45e 100644
--- a/litellm/llms/anthropic/experimental_pass_through/messages/handler.py
+++ b/litellm/llms/anthropic/experimental_pass_through/messages/handler.py
@@ -22,6 +22,7 @@ from litellm.llms.anthropic.common_utils import (
from litellm.llms.base_llm.anthropic_messages.transformation import (
BaseAnthropicMessagesConfig,
)
+from litellm.llms.base_llm.chat.transformation import BaseLLMException
from litellm.llms.custom_httpx.http_handler import AsyncHTTPHandler
from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler
from litellm.types.llms.anthropic_messages.anthropic_request import AnthropicMetadata
@@ -388,7 +389,7 @@ async def anthropic_messages(
if asyncio.iscoroutine(init_response):
return await init_response
return init_response
- except Exception as e: # noqa: BLE001 # the mapping boundary must see every provider-layer failure, like acompletion
+ except BaseLLMException as e:
raise exception_type(
model=model,
custom_llm_provider=custom_llm_provider,
diff --git 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
index 5c838789798..b690b3448ec 100644
--- 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
@@ -7,6 +7,7 @@ from typing import Any, Dict, List
import httpx
import pytest
from fastapi.testclient import TestClient
+from pydantic import ValidationError
from unittest.mock import AsyncMock, MagicMock, patch
@@ -1352,3 +1353,30 @@ async def test_anthropic_messages_maps_provider_exception_before_failure_logging
assert error_information.get("error_class") == expected_exception.__name__
assert error_information.get("llm_provider") == "anthropic"
assert error_information.get("error_code") == str(upstream_status)
+
+
+async def test_anthropic_messages_leaves_non_provider_failures_unmapped():
+ """The mapping boundary is for provider failures only. A request rejected before
+ the provider call (here invalid metadata) must surface as the original exception,
+ not as the mapper's APIConnectionError, whose message embeds a server traceback."""
+ from litellm.llms.anthropic.experimental_pass_through.messages import handler
+
+ def upstream_must_not_be_called(request: httpx.Request) -> httpx.Response:
+ raise AssertionError("the provider must not be called for a request rejected locally")
+
+ upstream = AsyncHTTPHandler()
+ upstream.client = httpx.AsyncClient(transport=httpx.MockTransport(upstream_must_not_be_called))
+
+ with pytest.raises(ValidationError) as excinfo:
+ 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-invalid",
+ client=upstream,
+ metadata={"user_id": 123},
+ )
+
+ assert "Traceback" not in str(excinfo.value)