diff --git a/litellm/caching/caching.py b/litellm/caching/caching.py index 5f5b8210096..357d78030d6 100644 --- a/litellm/caching/caching.py +++ b/litellm/caching/caching.py @@ -556,7 +556,7 @@ class Cache: Returns: str: The final hashed cache key with the redis namespace. """ - dynamic_cache_control: Final[DynamicCacheControl] = kwargs.get("cache", {}) + dynamic_cache_control: Final[DynamicCacheControl] = kwargs.get("cache") or {} metadata: Final = kwargs.get("metadata") or {} namespace: Final = dynamic_cache_control.get("namespace") or metadata.get("redis_namespace") or self.namespace if namespace: @@ -661,7 +661,7 @@ class Cache: else: cache_key = self.get_cache_key(**kwargs) if cache_key is not None: - cache_control_args: Final[DynamicCacheControl] = kwargs.get("cache", {}) + cache_control_args: Final[DynamicCacheControl] = kwargs.get("cache") or {} max_age = cache_control_args.get("s-maxage") or cache_control_args.get("s-max-age") or float("inf") cache_lookup_kwargs: Final = self._get_safe_cache_lookup_kwargs(kwargs) if dynamic_cache_object is not None: @@ -694,7 +694,7 @@ class Cache: else: cache_key = self.get_cache_key(**kwargs) if cache_key is not None: - cache_control_args: Final = kwargs.get("cache", {}) + cache_control_args: Final = kwargs.get("cache") or {} max_age: Final = cache_control_args.get( "s-max-age", cache_control_args.get("s-maxage", float("inf")) ) diff --git a/tests/unit/caching/test_cache_null_namespace.py b/tests/unit/caching/test_cache_null_namespace.py new file mode 100644 index 00000000000..be7ff885b65 --- /dev/null +++ b/tests/unit/caching/test_cache_null_namespace.py @@ -0,0 +1,35 @@ +from typing import Final + +from litellm.caching.caching import Cache +from litellm.types.caching import LiteLLMCacheType + + +def test_explicit_null_cache_control_uses_cache_namespace(): + cache: Final = Cache(type=LiteLLMCacheType.LOCAL, namespace="instance") + request: Final = {"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hello"}]} + + default_key: Final = cache.get_cache_key(**request, metadata=None) + null_cache_key: Final = cache.get_cache_key(**request, metadata=None, cache=None) + request_namespace_key: Final = cache.get_cache_key( + **request, + metadata=None, + cache={"namespace": "request"}, + ) + + assert null_cache_key == default_key + assert request_namespace_key.startswith("request:") + assert request_namespace_key != default_key + + +def test_explicit_null_cache_control_allows_sync_cache_lookup(): + cache: Final = Cache(type=LiteLLMCacheType.LOCAL) + request: Final = {"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hello"}]} + + assert cache.get_cache(**request, cache=None) is None + + +async def test_explicit_null_cache_control_allows_async_cache_lookup(): + cache: Final = Cache(type=LiteLLMCacheType.LOCAL) + request: Final = {"model": "gpt-4o-mini", "messages": [{"role": "user", "content": "hello"}]} + + assert await cache.async_get_cache(**request, cache=None) is None