DSPy: The framework for programming—not prompting—language models
Python
Update DSpy settings save / load methods (#9215)
WeichenXu786c431bc62b
diff --git a/dspy/__init__.py b/dspy/__init__.pyindex 3de46932b..b65ed557b 100644--- a/dspy/__init__.py+++ b/dspy/__init__.py@@ -23,6 +23,7 @@ configure_dspy_loggers(__name__)# Singleton definitions and aliasingconfigure = settings.configure+load_settings = settings.loadcontext = settings.contextBootstrapRS = BootstrapFewShotWithRandomSearchdiff --git a/dspy/dsp/utils/settings.py b/dspy/dsp/utils/settings.pyindex 4debc6ba9..24522b955 100644--- a/dspy/dsp/utils/settings.py+++ b/dspy/dsp/utils/settings.py@@ -4,6 +4,7 @@ import copyimport loggingimport threadingfrom contextlib import contextmanager+from typing import Anyimport cloudpickle@@ -190,14 +191,19 @@ class Settings:combined_config = {**main_thread_config, **overrides}return repr(combined_config)- def save(self, path, modules_to_serialize=None):+ def save(+ self, path: str,+ modules_to_serialize: list[str] | None = None,+ exclude_keys: list[str] | None = None,+ ):"""Save the settings to a file using cloudpickle.Args:path: The file path to save the settings to.- modules_to_serialize (list): A list of modules to serialize with cloudpickle's `register_pickle_by_value`.+ modules_to_serialize (list or None): A list of modules to serialize with cloudpickle's `register_pickle_by_value`.If None, then no modules will be registered for serialization.+ exclude_keys (list or None): A list of keys to exclude during saving."""logger.warning("`dspy.settings` are serialized using cloudpickle. Because cloudpickle allows for the "@@ -209,27 +215,31 @@ class Settings:for module in modules_to_serialize:cloudpickle.register_pickle_by_value(module)+ exclude_keys = exclude_keys or []+ data = {key: value for key, value in self.config.items() if key not in exclude_keys}with open(path, "wb") as f:- cloudpickle.dump(dict(self.config), f)+ cloudpickle.dump(data, f)except Exception as e:raise RuntimeError(f"Saving failed with error: {e}. Please remove the non-picklable attributes from the values ""in the `dspy.settings`.")- def load(self, path):+ @classmethod+ def load(cls, path: str) -> dict[str, Any]:"""Load the settings from a file using cloudpickle.- Note: The `load` method can only be called from the thread that first configured settings-Args:path: The file path to load the settings from.++ Returns:+ A dict that stores the loaded settings."""with open(path, "rb") as f:configs = cloudpickle.load(f)- self.configure(**configs)+ return configssettings = Settings()diff --git a/tests/utils/test_settings.py b/tests/utils/test_settings.pyindex b1dff242e..30b41e965 100644--- a/tests/utils/test_settings.py+++ b/tests/utils/test_settings.py@@ -200,12 +200,27 @@ def test_dspy_settings_save_load(tmp_path):dspy.settings.save(tmp_path / "settings.pkl")dspy.configure(lm=None, adapter=None, callbacks=None)- dspy.settings.load(tmp_path / "settings.pkl")+ loaded_settings = dspy.load_settings(tmp_path / "settings.pkl")+ dspy.configure(**loaded_settings)assert dspy.settings.lm.model == "openai/gpt-4o"assert isinstance(dspy.settings.adapter, dspy.JSONAdapter)assert len(dspy.settings.callbacks) == 1+def test_dspy_settings_save_exclude_keys(tmp_path):+ dspy.configure(lm=dspy.LM("openai/gpt-4o"), adapter=dspy.JSONAdapter(), track_usage=True)++ 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")+ dspy.configure(**loaded_settings)+ assert dspy.settings.lm.model == "openai/gpt-4o"+ assert dspy.settings.adapter is None+ assert not dspy.settings.track_usage+++def test_settings_save_with_extra_modules(tmp_path):# Create a temporary Python file with our custom modulecustom_module_path = tmp_path / "custom_module.py"@@ -236,8 +251,10 @@ def callback(x):del custom_moduledspy.configure(callbacks=None)+# Loading should now succeed and preserve the adapter instance- dspy.settings.load(settings_path)+ loaded_settings = dspy.load_settings(settings_path)+ dspy.settings.configure(**loaded_settings)assert dspy.settings.callbacks[0](3) == 4