From d1acdfaf9a0d488d5bd7bf5aff3c76914f986f29 Mon Sep 17 00:00:00 2001 From: Maximilian Rink Date: Tue, 13 Oct 2026 22:18:56 +0200 Subject: [PATCH] test(cache): isolate namespace regression test --- litellm/caching/caching.py | 6 +----- .../test_authenticated_namespace_cache_key.py | 20 +++++++++++++++++++ tests/unit/caching/test_caching.py | 15 -------------- 3 files changed, 21 insertions(+), 20 deletions(-) create mode 100644 tests/unit/caching/test_authenticated_namespace_cache_key.py diff --git a/litellm/caching/caching.py b/litellm/caching/caching.py index faf660dfe1f..4a1b9f3430c 100644 --- a/litellm/caching/caching.py +++ b/litellm/caching/caching.py @@ -657,11 +657,7 @@ class Cache: metadata_namespace, ) namespace: Final[str | None] = ( - ":".join( - value - for value in namespace_parts - if isinstance(value, str) and value - ) + ":".join(value for value in namespace_parts if isinstance(value, str) and value) if authenticated_namespace is not None else requested_namespace or metadata_namespace or self.namespace ) diff --git a/tests/unit/caching/test_authenticated_namespace_cache_key.py b/tests/unit/caching/test_authenticated_namespace_cache_key.py new file mode 100644 index 00000000000..4fcfd975d5f --- /dev/null +++ b/tests/unit/caching/test_authenticated_namespace_cache_key.py @@ -0,0 +1,20 @@ +from typing import Final + +from litellm.caching.caching import Cache +from litellm.proxy._types import UserAPIKeyAuth +from litellm.types.caching import LiteLLMCacheType + + +def test_authenticated_cache_keys_preserve_request_and_metadata_namespaces() -> None: + cache: Final = Cache(type=LiteLLMCacheType.LOCAL, namespace="instance") + caller: Final = UserAPIKeyAuth(api_key="fixture", token="hashed-key", team_id="team") + + key: Final = cache._add_namespace_to_cache_key( + "hashed-response", + cache={"namespace": "request"}, + metadata={"user_api_key_auth": caller, "redis_namespace": "metadata"}, + ) + + assert key is not None + assert key.startswith("instance:caller:") + assert key.endswith(":request:metadata:hashed-response") diff --git a/tests/unit/caching/test_caching.py b/tests/unit/caching/test_caching.py index 022a2c8c8c3..4b8e1c9f3d7 100644 --- a/tests/unit/caching/test_caching.py +++ b/tests/unit/caching/test_caching.py @@ -55,21 +55,6 @@ def test_authenticated_proxy_callers_get_distinct_exact_cache_keys() -> None: assert key_a != key_b -def test_authenticated_cache_keys_preserve_request_and_metadata_namespaces() -> None: - cache: Final = Cache(type=LiteLLMCacheType.LOCAL, namespace="instance") - caller: Final = UserAPIKeyAuth(api_key="fixture", token="hashed-key", team_id="team") - - key: Final = cache._add_namespace_to_cache_key( - "hashed-response", - cache={"namespace": "request"}, - metadata={"user_api_key_auth": caller, "redis_namespace": "metadata"}, - ) - - assert key is not None - assert key.startswith("instance:caller:") - assert key.endswith(":request:metadata:hashed-response") - - def test_authenticated_callers_without_server_token_fail_closed_for_all_key_forms() -> None: cache: Final = Cache(type=LiteLLMCacheType.LOCAL, namespace="qa") caller_a: Final = UserAPIKeyAuth(api_key="raw-a", team_id="team-a", user_id="user-a").model_copy(