From 1aae75ef3e945448dd3830c1f7596ba21a2ec10d Mon Sep 17 00:00:00 2001 From: Alan Mak <108719967+meaqua9420@users.noreply.github.com> Date: Mon, 1 Jun 2026 22:37:38 +0800 Subject: [PATCH] fix cache key raw payload logging --- litellm/caching/caching.py | 4 +- .../caching/test_cache_key_logging.py | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/test_litellm/caching/test_cache_key_logging.py diff --git a/litellm/caching/caching.py b/litellm/caching/caching.py index 11733ce4cee..1a1d0b84216 100644 --- a/litellm/caching/caching.py +++ b/litellm/caching/caching.py @@ -309,7 +309,9 @@ class Cache: param_value = kwargs[param] cache_key += f"{str(param)}: {str(param_value)}" - verbose_logger.debug("\nCreated cache key: %s", cache_key) + verbose_logger.debug( + "\nCreated cache key payload length before hashing: %s", len(cache_key) + ) hashed_cache_key = Cache._get_hashed_cache_key(cache_key) hashed_cache_key = self._add_namespace_to_cache_key(hashed_cache_key, **kwargs) # Remove preset_cache_key from kwargs to avoid "got multiple values" TypeError diff --git a/tests/test_litellm/caching/test_cache_key_logging.py b/tests/test_litellm/caching/test_cache_key_logging.py new file mode 100644 index 00000000000..50b77948892 --- /dev/null +++ b/tests/test_litellm/caching/test_cache_key_logging.py @@ -0,0 +1,53 @@ +import logging + +from litellm.caching.caching import Cache + + +def test_cache_key_logging_does_not_emit_raw_request_payload(caplog): + cache = Cache() + payload_marker = "RAW_PAYLOAD_MARKER_29414" + tool_marker = "RAW_TOOL_MARKER_29414" + schema_marker = "RAW_SCHEMA_MARKER_29414" + + with caplog.at_level(logging.DEBUG, logger="LiteLLM"): + cache_key = cache.get_cache_key( + model="gpt-4.1", + messages=[ + { + "role": "user", + "content": f"Please summarize {payload_marker}.", + } + ], + tools=[ + { + "type": "function", + "function": { + "name": "lookup_customer", + "description": tool_marker, + "parameters": { + "type": "object", + "properties": {"customer_id": {"type": "string"}}, + }, + }, + } + ], + response_format={ + "type": "json_schema", + "json_schema": { + "name": "summary", + "schema": { + "type": "object", + "description": schema_marker, + }, + }, + }, + ) + + log_output = "\n".join( + record.getMessage() for record in caplog.records if record.name == "LiteLLM" + ) + + assert payload_marker not in log_output + assert tool_marker not in log_output + assert schema_marker not in log_output + assert cache_key in log_output