Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

Drop diskcache changes from PR

user9a2aeb0a411d
diff --git a/litellm/caching/disk_cache.py b/litellm/caching/disk_cache.py
index a02327f7784..e32c29b3bc6 100644
--- a/litellm/caching/disk_cache.py
+++ b/litellm/caching/disk_cache.py
@@ -17,14 +17,14 @@ class DiskCache(BaseCache):
import diskcache as dc
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
- "DiskCache requires the `diskcache` package. Install it with "
- "`pip install diskcache`."
+ "Please install litellm with `litellm[caching]` to use disk caching."
) from e
- # JSONDisk avoids pickle deserialization (CVE-2025-69872). Values
- # passed to set_cache must be JSON-serializable.
- cache_dir = disk_cache_dir if disk_cache_dir is not None else ".litellm_cache"
- self.disk_cache = dc.Cache(cache_dir, disk=dc.JSONDisk)
+ # if users don't provider one, use the default litellm cache
+ if disk_cache_dir is None:
+ self.disk_cache = dc.Cache(".litellm_cache")
+ else:
+ self.disk_cache = dc.Cache(disk_cache_dir)
def set_cache(self, key, value, **kwargs):
if "ttl" in kwargs:
@@ -43,15 +43,14 @@ class DiskCache(BaseCache):
self.set_cache(key=cache_key, value=cache_value)
def get_cache(self, key, **kwargs):
- cached = self.disk_cache.get(key)
- if cached is None:
- return None
- if isinstance(cached, str):
+ original_cached_response = self.disk_cache.get(key)
+ if original_cached_response:
try:
- return json.loads(cached)
+ cached_response = json.loads(original_cached_response) # type: ignore
except Exception:
- return cached
- return cached
+ cached_response = original_cached_response
+ return cached_response
+ return None
def batch_get_cache(self, keys: list, **kwargs):
return_val = []
diff --git a/tests/test_litellm/caching/test_disk_cache.py b/tests/test_litellm/caching/test_disk_cache.py
deleted file mode 100644
index 470e2064c38..00000000000
--- a/tests/test_litellm/caching/test_disk_cache.py
+++ /dev/null
@@ -1,72 +0,0 @@
-import os
-import sys
-import tempfile
-
-import pytest
-
-pytest.importorskip("diskcache")
-
-sys.path.insert(0, os.path.abspath("../../.."))
-
-from litellm.caching.disk_cache import DiskCache
-
-
-def tmp_cache():
- with tempfile.TemporaryDirectory() as d:
- yield DiskCache(disk_cache_dir=d)
-
-
-def test_disk_cache_uses_jsondisk(tmp_cache):
- """Confirm DiskCache constructs the underlying cache with JSONDisk to
- avoid pickle deserialization (CVE-2025-69872)."""
- import diskcache as dc
-
- assert tmp_cache.disk_cache.disk.__class__ is dc.JSONDisk
-
-
-def test_disk_cache_dict_round_trip(tmp_cache):
- payload = {"response": "hello", "timestamp": 1.0}
- tmp_cache.set_cache("k", payload)
- assert tmp_cache.get_cache("k") == payload
-
-
-def test_disk_cache_string_round_trip_decodes_json(tmp_cache):
- """get_cache decodes JSON strings transparently for callers that stored
- pre-serialized payloads."""
- tmp_cache.set_cache("k", '{"hello": "world"}')
- assert tmp_cache.get_cache("k") == {"hello": "world"}
-
-
-def test_disk_cache_string_round_trip_returns_raw_when_not_json(tmp_cache):
- tmp_cache.set_cache("k", "not-json")
- assert tmp_cache.get_cache("k") == "not-json"
-
-
-def test_disk_cache_increment(tmp_cache):
- tmp_cache.set_cache("counter", 42)
- assert tmp_cache.increment_cache("counter", 8) == 50
- assert tmp_cache.get_cache("counter") == 50
-
-
-def test_disk_cache_ttl_expiry(tmp_cache):
- import time
-
- tmp_cache.set_cache("k", "v", ttl=1)
- assert tmp_cache.get_cache("k") == "v"
- time.sleep(1.1)
- assert tmp_cache.get_cache("k") is None
-
-
-def test_disk_cache_delete_and_flush(tmp_cache):
- tmp_cache.set_cache("a", 1)
- tmp_cache.set_cache("b", 2)
- tmp_cache.delete_cache("a")
- assert tmp_cache.get_cache("a") is None
- assert tmp_cache.get_cache("b") == 2
- tmp_cache.flush_cache()
- assert tmp_cache.get_cache("b") is None
-
-
-def test_disk_cache_missing_key_returns_none(tmp_cache):
- assert tmp_cache.get_cache("never-set") is None