From 681bbc5d946892fb0d99c8dc39287cdbb09bff8e Mon Sep 17 00:00:00 2001 From: yucheng Date: Mon, 26 Oct 2026 15:19:35 -0700 Subject: [PATCH] fix(logging): keep the original error when failure redaction meets non-mapping request headers 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 --- litellm/integrations/mlflow.py | 4 +-- litellm/integrations/opentelemetry.py | 4 +-- litellm/litellm_core_utils/litellm_logging.py | 17 +++------- litellm/litellm_core_utils/redact_messages.py | 31 +++++++++++++++++-- .../observability/test_failure_redaction.py | 13 ++++++++ .../test_litellm_logging.py | 22 +++++++++++++ .../test_redact_messages.py | 15 +++++++++ 7 files changed, 87 insertions(+), 19 deletions(-) diff --git a/litellm/integrations/mlflow.py b/litellm/integrations/mlflow.py index 4230c03177f..d371c3f42b0 100644 --- a/litellm/integrations/mlflow.py +++ b/litellm/integrations/mlflow.py @@ -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) diff --git a/litellm/integrations/opentelemetry.py b/litellm/integrations/opentelemetry.py index 58bd910ab6f..ced2aeb32b1 100644 --- a/litellm/integrations/opentelemetry.py +++ b/litellm/integrations/opentelemetry.py @@ -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") diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index b3215345e25..910b871104a 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -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: diff --git a/litellm/litellm_core_utils/redact_messages.py b/litellm/litellm_core_utils/redact_messages.py index 0d601ea48b1..13a5cfce469 100644 --- a/litellm/litellm_core_utils/redact_messages.py +++ b/litellm/litellm_core_utils/redact_messages.py @@ -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: diff --git a/tests/integration/observability/test_failure_redaction.py b/tests/integration/observability/test_failure_redaction.py index d3719ac49fc..e35cfadfcd9 100644 --- a/tests/integration/observability/test_failure_redaction.py +++ b/tests/integration/observability/test_failure_redaction.py @@ -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") diff --git a/tests/unit/litellm_core_utils/test_litellm_logging.py b/tests/unit/litellm_core_utils/test_litellm_logging.py index 4afeba4d2ad..9731e3e736f 100644 --- a/tests/unit/litellm_core_utils/test_litellm_logging.py +++ b/tests/unit/litellm_core_utils/test_litellm_logging.py @@ -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" diff --git a/tests/unit/litellm_core_utils/test_redact_messages.py b/tests/unit/litellm_core_utils/test_redact_messages.py index 182bd695933..b578718c7b6 100644 --- a/tests/unit/litellm_core_utils/test_redact_messages.py +++ b/tests/unit/litellm_core_utils/test_redact_messages.py @@ -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