From 785ae73276db275878a798dd086021601ac7b280 Mon Sep 17 00:00:00 2001 From: linhongyu510 Date: Fri, 11 Sep 2026 12:28:34 +0800 Subject: [PATCH] fix(cache): preserve non-JSON container entries --- litellm/caching/in_memory_cache.py | 5 ++++- tests/test_litellm/caching/test_in_memory_cache.py | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/litellm/caching/in_memory_cache.py b/litellm/caching/in_memory_cache.py index 494ee03b47f..4da4f7c364b 100644 --- a/litellm/caching/in_memory_cache.py +++ b/litellm/caching/in_memory_cache.py @@ -73,7 +73,10 @@ class InMemoryCache(BaseCache): # Only convert to JSON if absolutely necessary if not isinstance(value, (str, bytes)): - value = json.dumps(value, default=str) + try: + value = json.dumps(value, default=str) + except (TypeError, ValueError): + value = repr(value) return sys.getsizeof(value) / 1024 <= self.max_size_per_item diff --git a/tests/test_litellm/caching/test_in_memory_cache.py b/tests/test_litellm/caching/test_in_memory_cache.py index 96e2e38b2be..bf715218f00 100644 --- a/tests/test_litellm/caching/test_in_memory_cache.py +++ b/tests/test_litellm/caching/test_in_memory_cache.py @@ -84,6 +84,15 @@ def test_in_memory_cache_max_size_per_item_measures_container_contents(): assert in_memory_cache.get_cache(key="small") == small_value +def test_in_memory_cache_accepts_small_container_with_non_json_keys(): + in_memory_cache = InMemoryCache(max_size_per_item=1) + value = {("key", "key-hash"): ("job-1",)} + + in_memory_cache.set_cache(key="active-jobs", value=value) + + assert in_memory_cache.get_cache(key="active-jobs") == value + + def test_in_memory_cache_ttl(): """ Check that