From bd5b2847ec332a478e012b630f26751f65509de9 Mon Sep 17 00:00:00 2001 From: Abdullah Alqahtani <61029571+anqorithm@users.noreply.github.com> Date: Tue, 3 Nov 2026 01:30:39 +0100 Subject: [PATCH] fix(cache): enforce string item limits in bytes --- litellm/caching/in_memory_cache.py | 2 +- tests/unit/caching/test_in_memory_cache.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/litellm/caching/in_memory_cache.py b/litellm/caching/in_memory_cache.py index 6b40c48c4ef..fe07268bc91 100644 --- a/litellm/caching/in_memory_cache.py +++ b/litellm/caching/in_memory_cache.py @@ -62,7 +62,7 @@ class InMemoryCache(BaseCache): try: # Fast path for common primitive types that are typically small if ( - isinstance(value, (bool, int, float, str)) + isinstance(value, (bool, int, float)) and len(str(value)) < self.max_size_per_item * MAX_SIZE_PER_ITEM_IN_MEMORY_CACHE_IN_KB ): # Conservative estimate return True diff --git a/tests/unit/caching/test_in_memory_cache.py b/tests/unit/caching/test_in_memory_cache.py index 901832937b5..38fb7ac467b 100644 --- a/tests/unit/caching/test_in_memory_cache.py +++ b/tests/unit/caching/test_in_memory_cache.py @@ -392,3 +392,14 @@ async def test_async_increment_pipeline_keeps_each_key_for_its_own_ttl() -> None assert cache.get_cache("minute") is None assert cache.get_cache("day") == 1 + + +@pytest.mark.parametrize("character", ("Ā", "界", "😀")) +def test_cache_rejects_unicode_strings_exceeding_the_byte_limit(character: str) -> None: + cache: Final = InMemoryCache(max_size_per_item=1, clock=lambda: 100.0) + oversized: Final = character * 700 + assert sys.getsizeof(oversized) > 1024 + cache.set_cache("large", oversized) + assert cache.get_cache("large") is None + cache.set_cache("small", character * 10) + assert cache.get_cache("small") == character * 10