diff --git a/litellm/_logging.py b/litellm/_logging.py index c00eaa0e51c..8bd7a86ecf5 100644 --- a/litellm/_logging.py +++ b/litellm/_logging.py @@ -470,7 +470,7 @@ def _process_record(record: logging.LogRecord, *, base64_limit: int, text_limit: else _python_process_diagnostic(message, exception, stack, leaves, redact, base64_limit, text_limit) ) raw_template_changed: Final = raw_template is not None and processed_leaves[len(extra_leaves)] != raw_template - safe_message: Final = "REDACTED" if raw_template_changed else processed_message + safe_message: Final = "REDACTED" if raw_template_changed and processed_message == message else processed_message if redact or safe_message != message: record.msg = safe_message # rebind-ok: the Filter interface mutates the record record.args = None # rebind-ok: the rendered message replaces interpolation inputs @@ -486,7 +486,7 @@ def _process_record(record: logging.LogRecord, *, base64_limit: int, text_limit: raw_color_changed: Final = ( raw_color is not None and processed_leaves[len(extra_leaves) + int(raw_template is not None)] != raw_color ) - if raw_color_changed: + if raw_color_changed and getattr(record, "color_message", None) == substituted_color: setattr(record, "color_message", "REDACTED") setattr(record, _REDACTED_RECORD_ATTR, _REDACTED_STAMP) return True diff --git a/tests/test_litellm/test_logging.py b/tests/test_litellm/test_logging.py index 59b023a1dc1..d9cfe88d52f 100644 --- a/tests/test_litellm/test_logging.py +++ b/tests/test_litellm/test_logging.py @@ -868,6 +868,21 @@ def test_malformed_interpolation_still_scrubs_a_record(monkeypatch, native): assert record.color_message == "REDACTED" +@pytest.mark.parametrize("native", (False, True), ids=("python", "rust")) +def test_key_pattern_template_keeps_the_rendered_redacted_line(monkeypatch, native): + if native: + pytest.importorskip("litellm.rust_bridge._native") + monkeypatch.setenv("LITELLM_RUST", "1" if native else "0") + monkeypatch.setattr("litellm._logging._ENABLE_SECRET_REDACTION", True) + record = _make_record(logging.INFO, "password=%s ok", ("hunter2",)) + record.color_message = "password=%s ok" + + assert DiagnosticProcessingFilter().filter(record) is True + + assert record.getMessage() == "REDACTED ok" + assert record.color_message == "REDACTED ok" + + def test_disabled_diagnostic_call_does_not_render_arguments(caplog): class Unrenderable: def __str__(self):