fix(logging): keep the original error when failure redaction meets non-mapping request headers
Some checks failed
LiteLLM Rust / rust-lint (push) Has been cancelled
LiteLLM Rust / rust-test (push) Has been cancelled
LiteLLM Rust / rust-wheel (push) Has been cancelled

Failure logging now decides redaction with should_redact_failure_logging, which treats metadata.headers
that is not a mapping as carrying no redaction header instead of raising AttributeError. The async failure
handler no longer replaces a provider error or a pre-auth 401 with that AttributeError, and the proxy
failure hooks, OTEL and MLflow failure spans keep logging those requests
This commit is contained in:
yucheng 2026-10-26 15:19:35 -07:00
parent 6792d82921
commit 681bbc5d94
7 changed files with 87 additions and 19 deletions

View file

@ -6,7 +6,7 @@ from typing import TYPE_CHECKING, Any, Final
from litellm._logging import verbose_logger
from litellm.constants import REDACTED_BY_LITELLM
from litellm.integrations.custom_logger import CustomLogger
from litellm.litellm_core_utils.redact_messages import should_redact_message_logging
from litellm.litellm_core_utils.redact_messages import should_redact_failure_logging
if TYPE_CHECKING:
from litellm.types.utils import StandardLoggingPayload
@ -100,7 +100,7 @@ class MlflowLogger(CustomLogger):
"exception.stacktrace": REDACTED_BY_LITELLM,
},
)
if should_redact_message_logging(kwargs)
if should_redact_failure_logging(kwargs)
else SpanEvent.from_exception(exception)
)
span.add_event(exception_event)

View file

@ -2298,9 +2298,9 @@ class OpenTelemetry(OTELGenAISemconvMixin, CustomLogger):
from litellm.integrations._types.open_inference import (
ErrorAttributes,
)
from litellm.litellm_core_utils.redact_messages import should_redact_message_logging
from litellm.litellm_core_utils.redact_messages import should_redact_failure_logging
redact: Final = should_redact_message_logging(kwargs) if redact_content is None else redact_content
redact: Final = should_redact_failure_logging(kwargs) if redact_content is None else redact_content
# Get the exception object if available
exception: Final = kwargs.get("exception")

View file

@ -117,6 +117,7 @@ from litellm.litellm_core_utils.logging_utils import (
from litellm.litellm_core_utils.model_param_helper import ModelParamHelper
from litellm.litellm_core_utils.ptu_pricing import is_spilled_over_ptu_request
from litellm.litellm_core_utils.redact_messages import (
redact_failure_from_logging,
redact_message_input_output_from_custom_logger,
redact_message_input_output_from_logging,
redacted_model_call_details_for_custom_logger,
@ -4268,14 +4269,8 @@ class Logging(LiteLLMLoggingBaseClass):
global_callbacks=litellm.failure_callback,
)
result: object = None # result sent to all loggers, init this to None incase it's not created
result = _REDACTED_RESULT.validate_python(
redact_message_input_output_from_logging(
model_call_details=(self.model_call_details if hasattr(self, "model_call_details") else {}),
result=result,
)
)
redact_failure_from_logging(self.model_call_details)
result: Final = None
self.mark_logging_complete(event_type="sync_failure")
for callback in callbacks:
try:
@ -4470,10 +4465,8 @@ class Logging(LiteLLMLoggingBaseClass):
global_callbacks=litellm._async_failure_callback,
)
result: Final = redact_message_input_output_from_logging(
model_call_details=self.model_call_details,
result=None,
)
redact_failure_from_logging(self.model_call_details)
result: Final = None
self.mark_logging_complete(event_type="async_failure")
for callback in callbacks:
try:

View file

@ -430,7 +430,7 @@ def should_redact_failed_request(request_data: Mapping[str, object]) -> bool:
if key == "metadata" or key in request_data
}
)
return should_redact_message_logging(
return should_redact_failure_logging(
{
"litellm_params": litellm_params,
"standard_callback_dynamic_params": {"turn_off_message_logging": dynamic_param},
@ -715,14 +715,33 @@ def should_redact_message_logging(model_call_details: Mapping) -> bool:
is_redaction_enabled_via_header = True
break
return _redaction_decision(
_get_turn_off_message_logging_from_dynamic_params(model_call_details),
enabled_via_header=is_redaction_enabled_via_header,
)
def should_redact_failure_logging(model_call_details: Mapping) -> bool:
"""
`should_redact_message_logging` for failure logging, where request headers that are not
a mapping carry no redaction header instead of raising.
"""
try:
return should_redact_message_logging(model_call_details)
except AttributeError:
return _redaction_decision(
_get_turn_off_message_logging_from_dynamic_params(model_call_details), enabled_via_header=False
)
def _redaction_decision(dynamic_turn_off: bool | None, *, enabled_via_header: bool) -> bool:
# Priority 1: Check dynamic parameter first (if explicitly set)
dynamic_turn_off: Final = _get_turn_off_message_logging_from_dynamic_params(model_call_details)
if dynamic_turn_off is not None:
# Dynamic parameter is explicitly set, use it
return dynamic_turn_off
# Priority 2: Check if header explicitly enables redaction
if is_redaction_enabled_via_header:
if enabled_via_header:
return True
# Priority 3: Fall back to global setting
@ -739,6 +758,12 @@ def redact_message_input_output_from_logging(model_call_details: dict, result, i
return result
def redact_failure_from_logging(model_call_details: dict[str, object]) -> None:
"""Redacts the failure logging object in place when `should_redact_failure_logging` says so"""
if should_redact_failure_logging(model_call_details):
perform_redaction(model_call_details, None)
def _get_turn_off_message_logging_from_dynamic_params(
model_call_details: Mapping,
) -> bool | None:

View file

@ -955,6 +955,19 @@ def test_e7_spend_logs_detail_endpoint_redacted(rig: Rig) -> None:
assert secret not in json.dumps(detail), json.dumps(detail)[:2000]
@pytest.mark.parametrize("headers", ("x", None, ["a"]), ids=lambda v: type(v).__name__)
def test_e8_non_mapping_metadata_headers_keep_the_401(rig_off: Rig, headers: JsonValue) -> None:
with rig_off.proxy.scenario() as scenario:
model: Final = scenario.model(api_base=rig_off.provider.url + "/v1", api_key="synthetic-provider-key")
rejected: Final = rig_off.proxy.request(
"POST",
"/v1/chat/completions",
{"model": model, "messages": [{"role": "user", "content": "hi"}], "metadata": {"headers": headers}},
key="sk-bogus",
)
assert rejected.status_code == 401, rejected.text
# --- F. Edge ----------------------------------------------------------------------------
_F1_SLOTS: Final = ("top", "metadata", "litellm_metadata")

View file

@ -11571,6 +11571,28 @@ async def test_async_failure_handler_honours_excluded_fields(monkeypatch):
assert "hidden_params" not in payload
@pytest.mark.asyncio
@pytest.mark.parametrize("headers", [None, "x", ["a"]], ids=["none", "str", "list"])
@pytest.mark.parametrize("redaction_on", [True, False], ids=["redaction_on", "redaction_off"])
async def test_async_failure_handler_logs_requests_with_non_mapping_headers(monkeypatch, headers, redaction_on):
secret = "secret-prompt-marker"
capture = _CapturingFailureLogger()
monkeypatch.setattr(litellm, "turn_off_message_logging", redaction_on)
monkeypatch.setattr(litellm, "_async_failure_callback", [capture])
monkeypatch.setattr(litellm, "standard_logging_payload_excluded_fields", None)
logging_obj = _failure_logging_obj(secret)
logging_obj.model_call_details["litellm_params"] = {"metadata": {"headers": headers}}
await logging_obj.async_failure_handler(
litellm.BadRequestError(message=f"Unsupported content: {secret}", model="gpt-4o", llm_provider="openai"),
f"Traceback ... {secret} ...",
)
assert capture.async_kwargs is not None
logged = str(capture.async_kwargs.get("traceback_exception"))
assert (secret in logged) is not redaction_on, logged
@pytest.mark.asyncio
async def test_per_callback_turn_off_redacts_error_fields_even_for_self_redacting_logger(monkeypatch):
secret = "secret-prompt-marker"

View file

@ -1400,6 +1400,21 @@ class TestShouldRedactFailedRequest:
monkeypatch.setattr(litellm, "turn_off_message_logging", True)
assert should_redact_failed_request(_request_data(turn_off_message_logging=False)) is False
@pytest.mark.parametrize("headers", [None, "x", ["a"]], ids=["none", "str", "list"])
@pytest.mark.parametrize("global_on", [True, False], ids=["global_on", "global_off"])
def test_non_mapping_headers_follow_the_global_setting(self, monkeypatch, headers, global_on):
from litellm.litellm_core_utils.redact_messages import should_redact_failed_request
monkeypatch.setattr(litellm, "turn_off_message_logging", global_on)
assert should_redact_failed_request(_request_data(metadata={"headers": headers})) is global_on
def test_non_mapping_headers_keep_the_dynamic_param(self, monkeypatch):
from litellm.litellm_core_utils.redact_messages import should_redact_failed_request
monkeypatch.setattr(litellm, "turn_off_message_logging", True)
request_data = _request_data(metadata={"headers": "x"}, turn_off_message_logging=False)
assert should_redact_failed_request(request_data) is False
def test_dynamic_param_in_metadata_slot(self):
from litellm.litellm_core_utils.redact_messages import should_redact_failed_request