DSPy: The framework for programming—not prompting—language models
Python
add pickle security to settings (#9339)
Isaac Millere5cf186e2090
diff --git a/dspy/dsp/utils/settings.py b/dspy/dsp/utils/settings.pyindex 24522b955..91d25c145 100644--- a/dspy/dsp/utils/settings.py+++ b/dspy/dsp/utils/settings.py@@ -226,16 +226,24 @@ class Settings:)@classmethod- def load(cls, path: str) -> dict[str, Any]:+ def load(cls, path: str, allow_pickle: bool = False) -> dict[str, Any]:"""Load the settings from a file using cloudpickle.Args:path: The file path to load the settings from.+ allow_pickle: Whether to allow loading with pickle. Loading untrusted .pkl files+ can run arbitrary code. Set to True only if you trust the source of the file.Returns:A dict that stores the loaded settings."""+ if not allow_pickle:+ raise ValueError(+ "Loading .pkl files can run arbitrary code, which may be dangerous. "+ "Set `allow_pickle=True` if you trust the source of the file."+ )+with open(path, "rb") as f:configs = cloudpickle.load(f)diff --git a/tests/utils/test_settings.py b/tests/utils/test_settings.pyindex 30b41e965..98d4c2622 100644--- a/tests/utils/test_settings.py+++ b/tests/utils/test_settings.py@@ -200,7 +200,7 @@ def test_dspy_settings_save_load(tmp_path):dspy.settings.save(tmp_path / "settings.pkl")dspy.configure(lm=None, adapter=None, callbacks=None)- loaded_settings = dspy.load_settings(tmp_path / "settings.pkl")+ loaded_settings = dspy.load_settings(tmp_path / "settings.pkl", allow_pickle=True)dspy.configure(**loaded_settings)assert dspy.settings.lm.model == "openai/gpt-4o"assert isinstance(dspy.settings.adapter, dspy.JSONAdapter)@@ -213,7 +213,7 @@ def test_dspy_settings_save_exclude_keys(tmp_path):dspy.settings.save(tmp_path / "settings.pkl", exclude_keys=["adapter", "track_usage"])dspy.configure(lm=None, adapter=None, track_usage=False)- loaded_settings = dspy.load_settings(tmp_path / "settings.pkl")+ loaded_settings = dspy.load_settings(tmp_path / "settings.pkl", allow_pickle=True)dspy.configure(**loaded_settings)assert dspy.settings.lm.model == "openai/gpt-4o"assert dspy.settings.adapter is None@@ -253,7 +253,7 @@ def callback(x):dspy.configure(callbacks=None)# Loading should now succeed and preserve the adapter instance- loaded_settings = dspy.load_settings(settings_path)+ loaded_settings = dspy.load_settings(settings_path, allow_pickle=True)dspy.settings.configure(**loaded_settings)assert dspy.settings.callbacks[0](3) == 4