diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.py index df527d08af8..36a8791a2c0 100644 --- a/litellm/proxy/utils.py +++ b/litellm/proxy/utils.py @@ -5148,11 +5148,17 @@ def handle_exception_on_proxy(e: Exception) -> ProxyException: ) elif isinstance(e, ProxyException): return e + original_status_code = getattr(e, "status_code", None) + if isinstance(original_status_code, int) and 400 <= original_status_code < 600: + error_code = original_status_code + else: + error_code = status.HTTP_500_INTERNAL_SERVER_ERROR return ProxyException( - message="Internal Server Error, " + str(e), + message=getattr(e, "message", str(e)), type=ProxyErrorTypes.internal_server_error, param=getattr(e, "param", "None"), - code=status.HTTP_500_INTERNAL_SERVER_ERROR, + code=error_code, + headers=getattr(e, "headers", None), ) diff --git a/tests/test_litellm/proxy/test_proxy_utils.py b/tests/test_litellm/proxy/test_proxy_utils.py index 4b50e9a4d31..7b27287c02e 100644 --- a/tests/test_litellm/proxy/test_proxy_utils.py +++ b/tests/test_litellm/proxy/test_proxy_utils.py @@ -190,3 +190,26 @@ def test_get_projected_spend_over_limit_includes_current_spend(monkeypatch): projected_spend, projected_exceeded_date = result assert projected_spend == 290.0 assert projected_exceeded_date == real_datetime.date(2026, 4, 21) + + +def test_handle_exception_on_proxy_preserves_429_status_code(): + """Exceptions with status_code=429 should not be mapped to 500.""" + from litellm.proxy.utils import handle_exception_on_proxy + + class FakeRateLimitError(Exception): + def __init__(self): + self.status_code = 429 + self.message = "Rate limit exceeded" + self.param = None + super().__init__(self.message) + + result = handle_exception_on_proxy(FakeRateLimitError()) + assert result.code == "429" + + +def test_handle_exception_on_proxy_defaults_to_500_without_status_code(): + """Exceptions without status_code should map to 500.""" + from litellm.proxy.utils import handle_exception_on_proxy + + result = handle_exception_on_proxy(Exception("unknown error")) + assert result.code == "500"