diff --git a/litellm/caching/in_memory_cache.py b/litellm/caching/in_memory_cache.py index 9a970e64039..2aa2b868600 100644 --- a/litellm/caching/in_memory_cache.py +++ b/litellm/caching/in_memory_cache.py @@ -210,7 +210,7 @@ class InMemoryCache(BaseCache): return False @staticmethod - def _copy_cached_value(value: Any) -> Any: + def _copy_cached_value(value: object) -> object: """Return a read-isolated value without making the cache brittle. In-memory cache entries can contain Pydantic models and mutable @@ -227,7 +227,7 @@ class InMemoryCache(BaseCache): except Exception: # noqa: BLE001 - cache reads must tolerate non-copyable values return value - def _get_cache_value(self, key, *, copy_value: bool) -> Any: + def _get_cache_value(self, key, *, copy_value: bool) -> object: if key in self.cache_dict: if self.evict_element_if_expired(key): return None diff --git a/tests/unit/caching/test_in_memory_cache.py b/tests/unit/caching/test_in_memory_cache.py index c9135825e59..3ef302fa889 100644 --- a/tests/unit/caching/test_in_memory_cache.py +++ b/tests/unit/caching/test_in_memory_cache.py @@ -79,6 +79,19 @@ def test_in_memory_cache_returns_read_isolated_pydantic_models(): assert stored.metadata == {"region": "us"} +def test_in_memory_cache_preserves_non_copyable_mutable_values(): + cache = InMemoryCache() + cache.set_cache("payload", ["unchanged"]) + + with patch( + "litellm.caching.in_memory_cache.copy.deepcopy", + side_effect=RuntimeError("not copyable"), + ): + cached = cache.get_cache("payload") + + assert cached is cache.cache_dict["payload"] + + def test_in_memory_openai_obj_cache(): from openai import OpenAI