From e0c711964f46d2a1065579211fcc4282f130f785 Mon Sep 17 00:00:00 2001 From: sourrrish Date: Sat, 21 Mar 2026 08:34:35 +0530 Subject: [PATCH] fix(prometheus): default to status_code=500 for exceptions without status code _extract_status_code() returned None when an exception lacked status_code/code attributes. str(None) became the literal 'None' in Prometheus labels, causing litellm_proxy_total_requests_metric 4xx/5xx aggregations to not match litellm_proxy_failed_requests_metric. Fixes #24224 Co-Authored-By: Claude Opus 4.6 --- litellm/integrations/prometheus.py | 11 ++++++++++- .../integrations/test_prometheus_status_code_none.py | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/litellm/integrations/prometheus.py b/litellm/integrations/prometheus.py index 357e0229fc6..bcc74ca790e 100644 --- a/litellm/integrations/prometheus.py +++ b/litellm/integrations/prometheus.py @@ -1448,7 +1448,8 @@ class PrometheusLogger(CustomLogger): exception: Exception object to extract status code from directly Returns: - Status code as integer if found, None otherwise + Status code as integer if found, 500 when an exception is present + but carries no status code attribute, or None when no inputs provided """ status_code = None @@ -1486,6 +1487,14 @@ class PrometheusLogger(CustomLogger): except (ValueError, TypeError): status_code = None + # If an exception was provided (directly or via kwargs) but no status code + # could be extracted, default to 500 — an unclassified server error is still a 5xx. + if status_code is None and ( + exception is not None + or (kwargs and kwargs.get("exception") is not None) + ): + return 500 + return status_code def _is_invalid_api_key_request( diff --git a/tests/test_litellm/integrations/test_prometheus_status_code_none.py b/tests/test_litellm/integrations/test_prometheus_status_code_none.py index 8a6140ff896..b380d853a48 100644 --- a/tests/test_litellm/integrations/test_prometheus_status_code_none.py +++ b/tests/test_litellm/integrations/test_prometheus_status_code_none.py @@ -39,3 +39,10 @@ def test_extract_status_code_defaults_to_500_for_bare_exception(prometheus_logge def test_extract_status_code_returns_none_when_no_exception(prometheus_logger): """When no exception is provided at all, should return None.""" assert prometheus_logger._extract_status_code() is None + + +def test_extract_status_code_defaults_to_500_for_bare_exception_in_kwargs(prometheus_logger): + """Bare exception passed through kwargs should also default to 500.""" + exc = Exception("something broke") + result = prometheus_logger._extract_status_code(kwargs={"exception": exc}) + assert result == 500, f"Expected 500 for bare exception in kwargs, got {result}"