fix(logging): keep exception text out of the error log when masking a failure log fails

This commit is contained in:
mateo-berri 2026-10-31 04:39:07 -07:00
parent b0f9a0fa6d
commit adf967ed73
2 changed files with 13 additions and 10 deletions

View file

@ -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]

View file

@ -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