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(proxy): keep ProxyException status codes on /v1/messages instead of wrapping into 500

mateo-berric894697a2a88
diff --git a/litellm/proxy/anthropic_endpoints/endpoints.py b/litellm/proxy/anthropic_endpoints/endpoints.py
index f75899b91dc..a48ef0f08bb 100644
--- a/litellm/proxy/anthropic_endpoints/endpoints.py
+++ b/litellm/proxy/anthropic_endpoints/endpoints.py
@@ -193,6 +193,9 @@ async def anthropic_response(
)
verbose_proxy_logger.exception("litellm.proxy.proxy_server.anthropic_response(): Exception occured - %s", e)
+ if isinstance(e, ProxyException):
+ raise
+
# Extract model_id from request metadata (same as success path)
litellm_metadata: Final = data.get("litellm_metadata", {}) or {}
model_info: Final = litellm_metadata.get("model_info", {}) or {}
diff --git a/tests/test_litellm/proxy/anthropic_endpoints/test_endpoints.py b/tests/test_litellm/proxy/anthropic_endpoints/test_endpoints.py
index 69d90a8b59b..0a427df0cb7 100644
--- a/tests/test_litellm/proxy/anthropic_endpoints/test_endpoints.py
+++ b/tests/test_litellm/proxy/anthropic_endpoints/test_endpoints.py
@@ -125,6 +125,45 @@ class TestBlockedResponseUsage:
mock_logging.post_call_failure_hook.assert_awaited_once()
+class TestProxyExceptionPassthrough:
+ @pytest.mark.asyncio
+ async def test_anthropic_response_reraises_proxy_exception_unwrapped(self):
+ """A 400 ProxyException from request validation must surface as-is,
+ not be re-wrapped into a code-500 ProxyException."""
+ import litellm.proxy.anthropic_endpoints.endpoints as ep
+ import litellm.proxy.proxy_server as proxy_server
+ from litellm.proxy._types import ProxyErrorTypes, ProxyException
+
+ exc = ProxyException(
+ message="Invalid type for 'metadata': expected an object, but got a string instead.",
+ type=ProxyErrorTypes.bad_request_error,
+ param="metadata",
+ code=400,
+ )
+
+ with (
+ patch.object(ep, "_read_request_body", new=AsyncMock(return_value={})),
+ patch.object(
+ ep.ProxyBaseLLMRequestProcessing,
+ "base_process_llm_request",
+ new=AsyncMock(side_effect=exc),
+ ),
+ patch.object(proxy_server, "proxy_logging_obj") as mock_logging,
+ ):
+ mock_logging.post_call_failure_hook = AsyncMock()
+ with pytest.raises(ProxyException) as exc_info:
+ await ep.anthropic_response(
+ fastapi_response=MagicMock(),
+ request=MagicMock(),
+ user_api_key_dict=MagicMock(),
+ )
+
+ assert exc_info.value is exc
+ assert exc_info.value.code == "400"
+ assert exc_info.value.param == "metadata"
+ mock_logging.post_call_failure_hook.assert_awaited_once()
+
+
class TestEventLoggingBatchEndpoint:
"""Test the stubbed event logging batch endpoint"""