Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Add param to load_memory_cache to stop pkl files without explicit loading (#9055)

Isaac Millerb82f06b04ec6
diff --git a/dspy/clients/cache.py b/dspy/clients/cache.py
index 72291d845..4773306cc 100644
--- a/dspy/clients/cache.py
+++ b/dspy/clients/cache.py
@@ -170,7 +170,11 @@ class Cache:
with open(filepath, "wb") as f:
cloudpickle.dump(self.memory_cache, f)
- def load_memory_cache(self, filepath: str) -> None:
+ def load_memory_cache(self, filepath: str, allow_pickle: bool = False) -> None:
+ if not allow_pickle:
+ raise ValueError("Loading untrusted .pkl files can run arbitrary code, which may be dangerous. \
+ Set `allow_pickle=True` to load if you are running in a trusted environment and the file is from a trusted source.")
+
if not self.enable_memory_cache:
return
diff --git a/tests/clients/test_cache.py b/tests/clients/test_cache.py
index fb0ccf33e..20e0af323 100644
--- a/tests/clients/test_cache.py
+++ b/tests/clients/test_cache.py
@@ -189,8 +189,12 @@ def test_save_and_load_memory_cache(cache, tmp_path):
memory_max_entries=100,
)
- # Load the memory cache
- new_cache.load_memory_cache(str(temp_cache_file))
+ # Load the memory cache without allowing pickle (default)
+ with pytest.raises(ValueError):
+ new_cache.load_memory_cache(str(temp_cache_file))
+
+ # Load the memory cache with allow_pickle=True
+ new_cache.load_memory_cache(str(temp_cache_file), allow_pickle=True)
# Verify items are in the new memory cache
for req in requests: