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(logging): keep tracebacks for unmapped provider exceptions too

mateo-berriac6deec5292a
diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py
index b669ab94350..1089499a64f 100644
--- a/litellm/litellm_core_utils/core_helpers.py
+++ b/litellm/litellm_core_utils/core_helpers.py
@@ -58,20 +58,29 @@ def safe_divide(
return numerator / denominator
+def _is_provider_originated(exception: BaseException) -> bool:
+ if getattr(exception, "llm_provider", None):
+ return True
+ from litellm.llms.base_llm.chat.transformation import BaseLLMException
+
+ return isinstance(exception, BaseLLMException)
+
+
def is_expected_client_error(exception: BaseException | None) -> bool:
"""
True when the proxy itself rejected the request with an HTTP 4xx before any
provider call (bad key, budget, unknown model, guardrail). A 4xx returned by
- a provider (the exception carries ``llm_provider``) is an upstream or
- deployment problem, so it is never an expected client error and keeps its
- traceback.
+ a provider is an upstream or deployment problem, so it is never an expected
+ client error and keeps its traceback: a mapped litellm exception carries
+ ``llm_provider``, and the raw ``BaseLLMException`` that provider handlers
+ raise before mapping (the /v1/messages route surfaces it as-is) is one too.
ProxyException stores the status on .code (as a str), HTTPException and
litellm exceptions on .status_code.
"""
if exception is None:
return False
- if getattr(exception, "llm_provider", None):
+ if _is_provider_originated(exception):
return False
code: Final[object] = getattr(exception, "code", None)
status_code: Final[object] = code if code is not None else getattr(exception, "status_code", None)
diff --git a/tests/test_litellm/integrations/otel/test_otel_v2_logger.py b/tests/test_litellm/integrations/otel/test_otel_v2_logger.py
index 1bc02bab89a..455d84c764f 100644
--- a/tests/test_litellm/integrations/otel/test_otel_v2_logger.py
+++ b/tests/test_litellm/integrations/otel/test_otel_v2_logger.py
@@ -354,6 +354,26 @@ def test_provider_auth_failure_span_carries_stack_trace():
assert "test_otel_v2_logger" in span.attributes["litellm.provider.error.stack_trace"]
+def test_unmapped_provider_auth_failure_span_carries_stack_trace():
+ """Regression for LIT-6163 on /v1/messages: that route logs the provider's
+ raw exception (no llm_provider), and its error span keeps the stack trace."""
+ from litellm.litellm_core_utils.litellm_logging import StandardLoggingPayloadSetup
+ from litellm.llms.anthropic.common_utils import AnthropicError
+
+ try:
+ raise AnthropicError(status_code=401, message='{"type":"authentication_error","message":"API key is invalid."}')
+ except AnthropicError as caught:
+ error_information = StandardLoggingPayloadSetup.get_error_information(caught)
+ logger, exporter = _logger()
+ payload = _payload(status="failure", custom_llm_provider="anthropic", error_information=error_information)
+ _emit_llm(logger, _kwargs(payload=payload), fail=True)
+ (span,) = exporter.get_finished_spans()
+ assert span.status.status_code is StatusCode.ERROR
+ assert span.attributes["error.type"] == "AnthropicError"
+ assert span.attributes["litellm.provider.error.code"] == "401"
+ assert "test_otel_v2_logger" in span.attributes["litellm.provider.error.stack_trace"]
+
+
def test_idempotent_on_repeat_callback():
"""The carrier is the dedup: once the async callback closes the span and
clears the carrier, a second callback firing emits nothing."""
diff --git a/tests/test_litellm/litellm_core_utils/test_core_helpers.py b/tests/test_litellm/litellm_core_utils/test_core_helpers.py
index a3c9aadd4d9..efbf7192a0e 100644
--- a/tests/test_litellm/litellm_core_utils/test_core_helpers.py
+++ b/tests/test_litellm/litellm_core_utils/test_core_helpers.py
@@ -285,6 +285,7 @@ class TestIsExpectedClientError:
pre-call rejections (no llm_provider) are expected client errors."""
from litellm.exceptions import AuthenticationError, RateLimitError
from litellm.litellm_core_utils.core_helpers import is_expected_client_error
+ from litellm.llms.anthropic.common_utils import AnthropicError
provider_auth_failure = AuthenticationError(
message="AnthropicException - API key is invalid.", llm_provider="anthropic", model="claude-haiku-4-5"
@@ -294,6 +295,9 @@ class TestIsExpectedClientError:
provider_rate_limit = RateLimitError(message="rate limited upstream", llm_provider="openai", model="gpt-4o")
assert is_expected_client_error(provider_rate_limit) is False
+ unmapped_provider_failure = AnthropicError(status_code=401, message='{"type":"authentication_error"}')
+ assert is_expected_client_error(unmapped_provider_failure) is False
+
class RouterRejection(Exception):
def __init__(self):
self.status_code = 429
diff --git a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
index 812c91290dd..5feaeff4408 100644
--- a/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
+++ b/tests/test_litellm/litellm_core_utils/test_litellm_logging.py
@@ -5732,6 +5732,20 @@ def test_get_error_information_keeps_traceback_for_provider_4xx():
assert "test_litellm_logging" in result["traceback"]
+def test_get_error_information_keeps_traceback_for_unmapped_provider_4xx():
+ """Regression for LIT-6163 on /v1/messages: that route logs the provider's
+ raw BaseLLMException (no llm_provider), which still keeps its traceback."""
+ from litellm.litellm_core_utils.litellm_logging import StandardLoggingPayloadSetup
+ from litellm.llms.anthropic.common_utils import AnthropicError
+
+ assert litellm.log_client_error_tracebacks is False
+ raw_provider_exc = _raise_and_catch(AnthropicError(status_code=401, message='{"type":"authentication_error"}'))
+ result = StandardLoggingPayloadSetup.get_error_information(raw_provider_exc)
+ assert result["error_code"] == "401"
+ assert result["error_class"] == "AnthropicError"
+ assert "test_litellm_logging" in result["traceback"]
+
+
def test_failure_handler_helper_fn_builds_payload_once_per_exception():
"""Regression for LIT-6043: async and sync failure handlers both call
_failure_handler_helper_fn for the same failed request; the standardized