diff --git a/litellm/litellm_core_utils/litellm_logging.py b/litellm/litellm_core_utils/litellm_logging.py index 6451a046e09..298f83fff16 100644 --- a/litellm/litellm_core_utils/litellm_logging.py +++ b/litellm/litellm_core_utils/litellm_logging.py @@ -4416,12 +4416,11 @@ class Logging(LiteLLMLoggingBaseClass): if self.masks_failure_log and self._claim_failure_log_mask(): self._redact_failure_log_request() - def _report_failure_log_mask_error(self, guardrail: CustomGuardrail) -> None: + def _report_failure_log_mask_error(self, guardrail: CustomGuardrail, error: Exception) -> None: verbose_logger.error( - "LiteLLM.LoggingError: [Non-Blocking] Exception occurred masking the failure log with %s, so its request " - "is redacted %s", + "LiteLLM.LoggingError: [Non-Blocking] %s occurred masking the failure log with %s, so its request is redacted", + type(error).__name__, guardrail.guardrail_name, - traceback.format_exc(), ) self._handle_callback_failure(callback=guardrail) @@ -4467,8 +4466,8 @@ class Logging(LiteLLMLoggingBaseClass): ) )[0] ) - except Exception: # noqa: BLE001 # a failing mask redacts the log instead of stopping failure logging - self._report_failure_log_mask_error(guardrail) + except Exception as error: # noqa: BLE001 # a failing mask redacts the log instead of stopping failure logging + self._report_failure_log_mask_error(guardrail, error) return None return hooked_details @@ -4542,8 +4541,8 @@ class Logging(LiteLLMLoggingBaseClass): hooked: Final = guardrail.logging_hook( kwargs=_failure_log_hook_kwargs(details), result=None, call_type=self._logging_hook_call_type ) - except Exception: # noqa: BLE001 # a failing mask redacts the log instead of stopping failure logging - self._report_failure_log_mask_error(guardrail) + except Exception as error: # noqa: BLE001 # a failing mask redacts the log instead of stopping failure logging + self._report_failure_log_mask_error(guardrail, error) return None return hooked[0] diff --git a/tests/unit/litellm_core_utils/test_litellm_logging.py b/tests/unit/litellm_core_utils/test_litellm_logging.py index c39aed0faee..9ba819f2ada 100644 --- a/tests/unit/litellm_core_utils/test_litellm_logging.py +++ b/tests/unit/litellm_core_utils/test_litellm_logging.py @@ -8717,7 +8717,7 @@ class _UnflaggedNameMaskingGuardrail(_NameMaskingGuardrail): class _RaisingMaskingGuardrail(_NameMaskingGuardrail): def _mask(self, kwargs: dict[str, object]) -> dict[str, object]: - raise RuntimeError("masking failed") + raise RuntimeError(f"masking failed on {kwargs['messages']}") class _BlockingNameMaskingGuardrail(_NameMaskingGuardrail): @@ -8907,14 +8907,18 @@ async def test_failure_log_keeps_the_prompt_when_no_logging_only_guardrail_masks @pytest.mark.asyncio @pytest.mark.parametrize("call", ["completion", "acompletion"]) async def test_failure_is_logged_redacted_when_the_masking_guardrail_raises( - monkeypatch: pytest.MonkeyPatch, call: Literal["completion", "acompletion"] + monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, call: Literal["completion", "acompletion"] ) -> None: + caplog.set_level(logging.ERROR, logger="LiteLLM") recorder, sync_logged = _register_failure_logging(monkeypatch, _RaisingMaskingGuardrail()) await _fail_chat_call(call, copy.deepcopy(_RAW_LOG)) + error_logs: Final = [record.getMessage() for record in caplog.records if record.levelno >= logging.ERROR] assert recorder.logged == [(_REDACTED_LOG, _REDACTED_LOG)] assert sync_logged == [_REDACTED_LOG] + assert any("RuntimeError occurred masking the failure log" in log for log in error_logs) + assert not any(_RAW_PROMPT in log for log in error_logs) @pytest.mark.asyncio