Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Improve DSPy saving (#1889)

Chen Qian4be87efb2056
diff --git a/dspy/__init__.py b/dspy/__init__.py
index 9e3e85fd2..1715bfb06 100644
--- a/dspy/__init__.py
+++ b/dspy/__init__.py
@@ -15,6 +15,7 @@ from dspy.clients import * # isort: skip
from dspy.adapters import * # isort: skip
from dspy.utils.logging_utils import configure_dspy_loggers, disable_logging, enable_logging
from dspy.utils.asyncify import asyncify
+from dspy.utils.saving import load
settings = dsp.settings
diff --git a/dspy/predict/predict.py b/dspy/predict/predict.py
index 0f93fd278..798030f27 100644
--- a/dspy/predict/predict.py
+++ b/dspy/predict/predict.py
@@ -5,17 +5,19 @@ from functools import lru_cache
from pydantic import BaseModel
import dsp
+from dspy.adapters.image_utils import Image
from dspy.predict.parameter import Parameter
from dspy.primitives.prediction import Prediction
from dspy.primitives.program import Module
from dspy.signatures.signature import ensure_signature, signature_to_template
from dspy.utils.callback import with_callbacks
-from dspy.adapters.image_utils import Image
+
@lru_cache(maxsize=None)
def warn_once(msg: str):
logging.warning(msg)
+
class Predict(Module, Parameter):
def __init__(self, signature, _parse_values=True, callbacks=None, **config):
self.stage = random.randbytes(8).hex()
@@ -31,13 +33,7 @@ class Predict(Module, Parameter):
self.train = []
self.demos = []
- def dump_state(self, save_verbose=None):
- if save_verbose:
- logging.warning(
- "`save_verbose` is deprecated and will be removed in DSPy 2.6.0 release. Currently `save_verbose` "
- "does nothing."
- )
-
+ def dump_state(self):
state_keys = ["lm", "traces", "train"]
state = {k: getattr(self, k) for k in state_keys}
@@ -61,33 +57,21 @@ class Predict(Module, Parameter):
state["extended_signature"] = self.extended_signature.dump_state()
return state
- def load_state(self, state, use_legacy_loading=False):
+ def load_state(self, state):
"""Load the saved state of a `Predict` object.
Args:
state (dict): The saved state of a `Predict` object.
- use_legacy_loading (bool): Whether to use the legacy loading method. Only use it when you are loading a
- saved state from a version of DSPy prior to v2.5.3.
+
Returns:
self: Returns self to allow method chaining
"""
- if use_legacy_loading:
- self._load_state_legacy(state)
- return self
-
- if "signature" not in state:
- # Check if the state is from a version of DSPy prior to v2.5.3.
- raise ValueError(
- "The saved state is from a version of DSPy prior to v2.5.3. Please use `use_legacy_loading=True` to "
- "load the state."
- )
-
excluded_keys = ["signature", "extended_signature"]
for name, value in state.items():
# `excluded_keys` are fields that go through special handling.
if name not in excluded_keys:
setattr(self, name, value)
-
+
# FIXME: Images are getting special treatment, but all basemodels initialized from json should be converted back to objects
for demo in self.demos:
for field in demo:
@@ -96,7 +80,7 @@ class Predict(Module, Parameter):
if not isinstance(url, str):
raise ValueError(f"Image URL must be a string, got {type(url)}")
demo[field] = Image(url=url)
-
+
self.signature = self.signature.load_state(state["signature"])
if "extended_signature" in state:
@@ -104,51 +88,6 @@ class Predict(Module, Parameter):
return self
- def _load_state_legacy(self, state):
- """Legacy state loading for backwards compatibility.
-
- This method is used to load the saved state of a `Predict` object from a version of DSPy prior to v2.5.3.
- Returns:
- self: Returns self to allow method chaining
- """
- for name, value in state.items():
- setattr(self, name, value)
-
- # Reconstruct the signature.
- if "signature_instructions" in state:
- instructions = state["signature_instructions"]
- self.signature = self.signature.with_instructions(instructions)
-
- if "signature_prefix" in state:
- prefix = state["signature_prefix"]
- *_, last_key = self.signature.fields.keys()
- self.signature = self.signature.with_updated_fields(last_key, prefix=prefix)
-
- # Some special stuff for CoT.
- if "extended_signature_instructions" in state:
- instructions = state["extended_signature_instructions"]
- self.extended_signature = self.extended_signature.with_instructions(instructions)
-
- if "extended_signature_prefix" in state:
- prefix = state["extended_signature_prefix"]
- *_, last_key = self.extended_signature.fields.keys()
- self.extended_signature = self.extended_signature.with_updated_fields(last_key, prefix=prefix)
-
- return self
-
- def load(self, path, return_self=False):
- """Load a saved state from a file.
-
- Args:
- path (str): Path to the saved state file
- return_self (bool): If True, returns self to allow method chaining. Default is False for backwards compatibility.
-
- Returns:
- Union[None, Predict]: Returns None if return_self is False (default), returns self if return_self is True
- """
- super().load(path)
- return self if return_self else None
-
@with_callbacks
def __call__(self, **kwargs):
return self.forward(**kwargs)
@@ -296,6 +235,7 @@ def v2_5_generate(lm, lm_kwargs, signature, demos, inputs, _parse_values=True):
lm, lm_kwargs=lm_kwargs, signature=signature, demos=demos, inputs=inputs, _parse_values=_parse_values
)
+
# TODO: get some defaults during init from the context window?
# # TODO: FIXME: Hmm, I guess expected behavior is that contexts can
# affect execution. Well, we need to determine whether context dominates, __init__ demoninates, or forward dominates.
diff --git a/dspy/primitives/module.py b/dspy/primitives/module.py
index 762387ef7..c2146301c 100644
--- a/dspy/primitives/module.py
+++ b/dspy/primitives/module.py
@@ -2,13 +2,20 @@ import copy
import logging
from collections import deque
from collections.abc import Generator
+from pathlib import Path
+import cloudpickle
import ujson
+from dspy.utils.saving import get_dependency_versions
+
# NOTE: Note: It's important (temporary decision) to maintain named_parameters that's different in behavior from
# named_sub_modules for the time being.
+logger = logging.getLogger(__name__)
+
+
class BaseModule:
def __init__(self):
pass
@@ -149,24 +156,103 @@ class BaseModule:
return new_instance
- def dump_state(self, save_verbose):
- return {name: param.dump_state(save_verbose) for name, param in self.named_parameters()}
+ def dump_state(self):
+ return {name: param.dump_state() for name, param in self.named_parameters()}
- def load_state(self, state, use_legacy_loading=False):
+ def load_state(self, state):
for name, param in self.named_parameters():
- if isinstance(param, BaseModule):
- param.load_state(state[name], use_legacy_loading=use_legacy_loading)
- else:
- # `use_legacy_loading` is only applicable for BaseModule instances.
- param.load_state(state[name])
+ param.load_state(state[name])
- def save(self, path, save_field_meta=False):
- with open(path, "w") as f:
- f.write(ujson.dumps(self.dump_state(save_field_meta), indent=2))
+ def save(self, path, save_program=False):
+ """Save the module.
- def load(self, path, use_legacy_loading=False):
- with open(path) as f:
- self.load_state(ujson.loads(f.read()), use_legacy_loading=use_legacy_loading)
+ Save the module to a directory or a file. There are two modes:
+ - `save_program=False`: Save only the state of the module to a json or pickle file, based on the value of
+ the file extension.
+ - `save_program=True`: Save the whole module to a directory via cloudpickle, which contains both the state and
+ architecture of the model.
+
+ We also save the dependency versions, so that the loaded model can check if there is a version mismatch on
+ critical dependencies or DSPy version.
+
+ Args:
+ path (str): Path to the saved state file, which should be a .json or .pkl file when `save_program=False`,
+ and a directory when `save_program=True`.
+ save_program (bool): If True, save the whole module to a directory via cloudpickle, otherwise only save
+ the state.
+ """
+ metadata = {}
+ metadata["dependency_versions"] = get_dependency_versions()
+ path = Path(path)
+
+ if save_program:
+ if path.suffix:
+ raise ValueError(
+ f"`path` must point to a directory without a suffix when `save_program=True`, but received: {path}"
+ )
+ if path.exists() and not path.is_dir():
+ raise NotADirectoryError(f"The path '{path}' exists but is not a directory.")
+
+ if not path.exists():
+ # Create the directory (and any parent directories)
+ path.mkdir(parents=True)
+
+ try:
+ with open(path / "program.pkl", "wb") as f:
+ cloudpickle.dump(self, f)
+ except Exception as e:
+ raise RuntimeError(
+ f"Saving failed with error: {e}. Please remove the non-picklable attributes from your DSPy program, "
+ "or consider using state-only saving by setting `save_program=False`."
+ )
+ with open(path / "metadata.json", "w") as f:
+ ujson.dump(metadata, f, indent=2)
+
+ return
+
+ state = self.dump_state()
+ state["metadata"] = metadata
+ if path.suffix == ".json":
+ with open(path, "w") as f:
+ f.write(ujson.dumps(state, indent=2))
+ elif path.suffix == ".pkl":
+ with open(path, "wb") as f:
+ cloudpickle.dump(state, f)
+ else:
+ raise ValueError(
+ f"`path` must end with `.json` or `.pkl` when `save_program=False`, but received: {path}"
+ )
+
+ def load(self, path):
+ """Load the saved module. You may also want to check out dspy.load, if you want to
+ load an entire program, not just the state for an existing program.
+
+ Args:
+ path (str): Path to the saved state file, which should be a .json or a .pkl file
+ """
+ path = Path(path)
+
+ if path.suffix == ".json":
+ with open(path, "r") as f:
+ state = ujson.loads(f.read())
+ elif path.suffix == ".pkl":
+ with open(path, "rb") as f:
+ state = cloudpickle.load(f)
+ else:
+ raise ValueError(f"`path` must end with `.json` or `.pkl`, but received: {path}")
+
+ dependency_versions = get_dependency_versions()
+ saved_dependency_versions = state["metadata"]["dependency_versions"]
+ for key, saved_version in saved_dependency_versions.items():
+ if dependency_versions[key] != saved_version:
+ logger.warning(
+ f"There is a mismatch of {key} version between saved model and current environment. "
+ f"You saved with `{key}=={saved_version}`, but now you have "
+ f"`{key}=={dependency_versions[key]}`. This might cause errors or performance downgrade "
+ "on the loaded model, please consider loading the model in the same environment as the "
+ "saving environment."
+ )
+ self.load_state(state)
def postprocess_parameter_name(name, value):
diff --git a/dspy/retrieve/retrieve.py b/dspy/retrieve/retrieve.py
index 37ac0390d..5080f63b9 100644
--- a/dspy/retrieve/retrieve.py
+++ b/dspy/retrieve/retrieve.py
@@ -30,8 +30,7 @@ class Retrieve(Parameter):
def reset(self):
pass
- def dump_state(self, save_verbose=False):
- """save_verbose is set as a default argument to support the inherited Parameter interface for dump_state"""
+ def dump_state(self):
state_keys = ["k"]
return {k: getattr(self, k) for k in state_keys}
@@ -116,8 +115,7 @@ class RetrieveThenRerank(Parameter):
def reset(self):
pass
- def dump_state(self, save_verbose=False):
- """save_verbose is set as a default argument to support the inherited Parameter interface for dump_state"""
+ def dump_state(self):
state_keys = ["k"]
return {k: getattr(self, k) for k in state_keys}
diff --git a/dspy/utils/saving.py b/dspy/utils/saving.py
new file mode 100644
index 000000000..95659a72d
--- /dev/null
+++ b/dspy/utils/saving.py
@@ -0,0 +1,50 @@
+import logging
+import sys
+from pathlib import Path
+
+import cloudpickle
+import importlib_metadata
+import ujson
+
+logger = logging.getLogger(__name__)
+
+
+def get_dependency_versions():
+ return {
+ "python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}",
+ "dspy": importlib_metadata.version("dspy"),
+ "cloudpickle": cloudpickle.__version__,
+ }
+
+
+def load(path):
+ """Load saved DSPy model.
+
+ This method is used to load a saved DSPy model with `save_program=True`, i.e., the model is saved with cloudpickle.
+
+ Args:
+ path (str): Path to the saved model.
+
+ Returns:
+ The loaded model, a `dspy.Module` instance.
+ """
+ path = Path(path)
+ if not path.exists():
+ raise FileNotFoundError(f"The path '{path}' does not exist.")
+
+ with open(path / "metadata.json", "r") as f:
+ metadata = ujson.load(f)
+
+ dependency_versions = get_dependency_versions()
+ saved_dependency_versions = metadata["dependency_versions"]
+ for key, saved_version in saved_dependency_versions.items():
+ if dependency_versions[key] != saved_version:
+ logger.warning(
+ f"There is a mismatch of {key} version between saved model and current environment. You saved with "
+ f"`{key}=={saved_version}`, but now you have `{key}=={dependency_versions[key]}`. This might cause "
+ "errors or performance downgrade on the loaded model, please consider loading the model in the same "
+ "environment as the saving environment."
+ )
+
+ with open(path / "program.pkl", "rb") as f:
+ return cloudpickle.load(f)
diff --git a/poetry.lock b/poetry.lock
index 2e13c532b..b1c8106ac 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -1,4 +1,4 @@
-# This file is automatically @generated by Poetry 1.8.3 and should not be changed by hand.
+# This file is automatically @generated by Poetry 1.8.4 and should not be changed by hand.
[[package]]
name = "aiohappyeyeballs"
@@ -266,17 +266,6 @@ typing-extensions = {version = ">=4", markers = "python_version < \"3.11\""}
[package.extras]
tests = ["mypy (>=0.800)", "pytest", "pytest-asyncio"]
-[[package]]
-name = "asn1crypto"
-version = "1.5.1"
-description = "Fast ASN.1 parser and serializer with definitions for private keys, public keys, certificates, CRL, OCSP, CMS, PKCS#3, PKCS#7, PKCS#8, PKCS#12, PKCS#5, X.509 and TSP"
-optional = true
-python-versions = "*"
-files = [
- {file = "asn1crypto-1.5.1-py2.py3-none-any.whl", hash = "sha256:db4e40728b728508912cbb3d44f19ce188f218e9eba635821bb4b68564f8fd67"},
- {file = "asn1crypto-1.5.1.tar.gz", hash = "sha256:13ae38502be632115abf8a24cbe5f4da52e3b5231990aff31123c805306ccb9c"},
-]
-
[[package]]
name = "asttokens"
version = "2.4.1"
@@ -895,13 +884,13 @@ colorama = {version = "*", markers = "platform_system == \"Windows\""}
[[package]]
name = "cloudpickle"
-version = "2.2.1"
-description = "Extended pickling support for Python objects"
-optional = true
-python-versions = ">=3.6"
+version = "3.1.0"
+description = "Pickler class to extend the standard pickle.Pickler functionality"
+optional = false
+python-versions = ">=3.8"
files = [
- {file = "cloudpickle-2.2.1-py3-none-any.whl", hash = "sha256:61f594d1f4c295fa5cd9014ceb3a1fc4a70b0de1164b94fbc2d854ccba056f9f"},
- {file = "cloudpickle-2.2.1.tar.gz", hash = "sha256:d89684b8de9e34a2a43b3460fbca07d09d6e25ce858df4d5a44240403b6178f5"},
+ {file = "cloudpickle-3.1.0-py3-none-any.whl", hash = "sha256:fe11acda67f61aaaec473e3afe030feb131d78a43461b718185363384f1ba12e"},
+ {file = "cloudpickle-3.1.0.tar.gz", hash = "sha256:81a929b6e3c7335c863c771d673d105f02efdb89dfaba0c90495d1c64796601b"},
]
[[package]]
@@ -5576,24 +5565,6 @@ cffi = ">=1.4.1"
docs = ["sphinx (>=1.6.5)", "sphinx-rtd-theme"]
tests = ["hypothesis (>=3.27.0)", "pytest (>=3.2.1,!=3.3.0)"]
-[[package]]
-name = "pyopenssl"
-version = "24.2.1"
-description = "Python wrapper module around the OpenSSL library"
-optional = true
-python-versions = ">=3.7"
-files = [
- {file = "pyOpenSSL-24.2.1-py3-none-any.whl", hash = "sha256:967d5719b12b243588573f39b0c677637145c7a1ffedcd495a487e58177fbb8d"},
- {file = "pyopenssl-24.2.1.tar.gz", hash = "sha256:4247f0dbe3748d560dcbb2ff3ea01af0f9a1a001ef5f7c4c647956ed8cbf0e95"},
-]
-
-[package.dependencies]
-cryptography = ">=41.0.5,<44"
-
-[package.extras]
-docs = ["sphinx (!=5.2.0,!=5.2.0.post0,!=7.2.5)", "sphinx-rtd-theme"]
-test = ["pretend", "pytest (>=3.0.1)", "pytest-rerunfailures"]
-
[[package]]
name = "pyparsing"
version = "3.2.0"
@@ -6661,109 +6632,6 @@ files = [
{file = "snowballstemmer-2.2.0.tar.gz", hash = "sha256:09b16deb8547d3412ad7b590689584cd0fe25ec8db3be37788be3810cbf19cb1"},
]
-[[package]]
-name = "snowflake-connector-python"
-version = "3.12.3"
-description = "Snowflake Connector for Python"
-optional = true
-python-versions = ">=3.8"
-files = [
- {file = "snowflake_connector_python-3.12.3-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:497a096fc379ef0846b2f1cf11a8d7620f0d090f08a77d9e93473845014d57d1"},
- {file = "snowflake_connector_python-3.12.3-cp310-cp310-macosx_11_0_x86_64.whl", hash = "sha256:055c5808d524497213e4cc9ae91ec3e46cb8342b314e78bc3e139d733dc16741"},
- {file = "snowflake_connector_python-3.12.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:4a5dc512d62ef693041ed2ad82931231caddc16e14ffc2842da3e3dd4240b83d"},
- {file = "snowflake_connector_python-3.12.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a46448f7279d444084eb84a9cddea67662e80ccfaddf41713b9e9aab2b1242e9"},
- {file = "snowflake_connector_python-3.12.3-cp310-cp310-win_amd64.whl", hash = "sha256:821b774b77129ce9f03729456ac1f21d69fedb50e5ce957178131c7bb3d8279f"},
- {file = "snowflake_connector_python-3.12.3-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:82290134978d11628026b447052219ce8d880e36937204f1f0332dfc3f2e92e9"},
- {file = "snowflake_connector_python-3.12.3-cp311-cp311-macosx_11_0_x86_64.whl", hash = "sha256:20b5c8000ee9cee11b0f9a6ae26640f0d498ce77f7e2ec649a2f0d306523792d"},
- {file = "snowflake_connector_python-3.12.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ca6500d16bdbd37da88e589cc3e82b90272471d3aabfe4a79ec1cf4696675acf"},
- {file = "snowflake_connector_python-3.12.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:b455ba117a68da436e253899674fae1a93669eaefdde8a903c03eb65b7e87c86"},
- {file = "snowflake_connector_python-3.12.3-cp311-cp311-win_amd64.whl", hash = "sha256:205219fcaeee2d33db5d0d023d60518e3bd8272ce1679be2199d7f362d255054"},
- {file = "snowflake_connector_python-3.12.3-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:3d830ca32c864b730cba5d92900d850752199635c4fb0ae0a70ee677f62aee70"},
- {file = "snowflake_connector_python-3.12.3-cp312-cp312-macosx_11_0_x86_64.whl", hash = "sha256:597b0c74ec57ba693191ae2de8db9536e349ee32cab152df657473e498b6fd87"},
- {file = "snowflake_connector_python-3.12.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2215d8a4c5e25ea0d2183fe693c3fdf058cd6035e5c84710d532dc04ab4ffd31"},
- {file = "snowflake_connector_python-3.12.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f8ba9c261904c1ba7cae6035c7881224cf979da39c8b7c7cb10236fdfc57e505"},
- {file = "snowflake_connector_python-3.12.3-cp312-cp312-win_amd64.whl", hash = "sha256:f0d0fcb948ef0812ab162ec9767622f345554043a07439c0c1a9474c86772320"},
- {file = "snowflake_connector_python-3.12.3-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:fe742a0b2fb1c79a21e95b97c49a05783bc00314d1184d227c5fe5b57688af12"},
- {file = "snowflake_connector_python-3.12.3-cp38-cp38-macosx_11_0_x86_64.whl", hash = "sha256:a8584a44a6bb41d2056cf1b833e629c76e28c5303d2c875c1a23bda46a1cd43a"},
- {file = "snowflake_connector_python-3.12.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:dd990db8e4886c32ba5c63758e8dc4814e2e75f5fd3fe79d43f7e5ee0fc46793"},
- {file = "snowflake_connector_python-3.12.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e4fe7f91f6e44bda877e77403a586d7487ca2c52dc1a32a705b2fea33f9c763a"},
- {file = "snowflake_connector_python-3.12.3-cp38-cp38-win_amd64.whl", hash = "sha256:4994e95eff593dc44c28243ef0ae8d27b8b1aeb96dd64cbcea5bcf0e4dfb77fb"},
- {file = "snowflake_connector_python-3.12.3-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:ac33a7dd54b35f94c4b91369971dbd6467a914dff4b01c46e77e7e6901d7eca4"},
- {file = "snowflake_connector_python-3.12.3-cp39-cp39-macosx_11_0_x86_64.whl", hash = "sha256:a26876322811fe2b93f6d814dcfe016f1df680a12624026ecf57a6bcdf20f969"},
- {file = "snowflake_connector_python-3.12.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:6c0bb390be2e15b6b7cccab7fbe1ef94e1e9ab13790c974aa44761298cdc2641"},
- {file = "snowflake_connector_python-3.12.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e7340f73af4ae72e6af8fe28a1b8e196a0c99943071afc96ce419efb4da80035"},
- {file = "snowflake_connector_python-3.12.3-cp39-cp39-win_amd64.whl", hash = "sha256:c314749bd0151218b654a7d4646a39067ab650bdc86dfebb1884b056b0bdb4b4"},
- {file = "snowflake_connector_python-3.12.3.tar.gz", hash = "sha256:02873c7f7a3b10322e28dddc2be6907f8ab8ecad93d6d6af14c77c2f53091b88"},
-]
-
-[package.dependencies]
-asn1crypto = ">0.24.0,<2.0.0"
-certifi = ">=2017.4.17"
-cffi = ">=1.9,<2.0.0"
-charset-normalizer = ">=2,<4"
-cryptography = ">=3.1.0"
-filelock = ">=3.5,<4"
-idna = ">=2.5,<4"
-packaging = "*"
-platformdirs = ">=2.6.0,<5.0.0"
-pyjwt = "<3.0.0"
-pyOpenSSL = ">=16.2.0,<25.0.0"
-pytz = "*"
-requests = "<3.0.0"
-sortedcontainers = ">=2.4.0"
-tomlkit = "*"
-typing-extensions = ">=4.3,<5"
-urllib3 = {version = ">=1.21.1,<2.0.0", markers = "python_version < \"3.10\""}
-
-[package.extras]
-development = ["Cython", "coverage", "more-itertools", "numpy (<1.27.0)", "pendulum (!=2.1.1)", "pexpect", "pytest (<7.5.0)", "pytest-cov", "pytest-rerunfailures", "pytest-timeout", "pytest-xdist", "pytzdata"]
-pandas = ["pandas (>=1.0.0,<3.0.0)", "pyarrow"]
-secure-local-storage = ["keyring (>=23.1.0,<26.0.0)"]
-
-[[package]]
-name = "snowflake-snowpark-python"
-version = "1.25.0"
-description = "Snowflake Snowpark for Python"
-optional = true
-python-versions = "<3.12,>=3.8"
-files = [
- {file = "snowflake_snowpark_python-1.25.0-py3-none-any.whl", hash = "sha256:0b965308eb88568cbe6865164fb5441a336d9cf55bc603291f64ebfe799018d1"},
- {file = "snowflake_snowpark_python-1.25.0.tar.gz", hash = "sha256:184b9f6873b4c318af2727c707000998c9f6e24542036e536d86c2e314549e47"},
-]
-
-[package.dependencies]
-cloudpickle = [
- {version = ">=1.6.0,<2.1.0 || >2.1.0,<2.2.0 || >2.2.0,<=2.2.1", markers = "python_version < \"3.11\""},
- {version = "2.2.1", markers = "python_version ~= \"3.11\""},
-]
-protobuf = ">=3.20,<6"
-pyyaml = "*"
-setuptools = ">=40.6.0"
-snowflake-connector-python = ">=3.12.0,<4.0.0"
-typing-extensions = ">=4.1.0,<5.0.0"
-tzlocal = "*"
-wheel = "*"
-
-[package.extras]
-development = ["cachetools", "coverage", "decorator", "graphviz", "lxml", "matplotlib", "mypy-protobuf", "openpyxl", "pre-commit", "protoc-wheel-0 (==21.1)", "pytest (<8.0.0)", "pytest-assume", "pytest-cov", "pytest-timeout", "pytest-xdist", "sphinx (==5.0.2)"]
-localtest = ["pandas", "requests"]
-modin = ["modin (==0.30.1)", "snowflake-connector-python[pandas] (>=3.12.0,<4.0.0)"]
-modin-development = ["cachetools", "coverage", "decorator", "graphviz", "lxml", "matplotlib", "modin (==0.30.1)", "mypy-protobuf", "openpyxl", "pre-commit", "protoc-wheel-0 (==21.1)", "pytest (<8.0.0)", "pytest-assume", "pytest-cov", "pytest-timeout", "pytest-xdist", "scikit-learn (==1.5.2)", "scipy", "snowflake-connector-python[pandas] (>=3.12.0,<4.0.0)", "sphinx (==5.0.2)", "statsmodels"]
-opentelemetry = ["opentelemetry-api (>=1.0.0,<2.0.0)", "opentelemetry-sdk (>=1.0.0,<2.0.0)"]
-pandas = ["snowflake-connector-python[pandas] (>=3.12.0,<4.0.0)"]
-secure-local-storage = ["snowflake-connector-python[secure-local-storage] (>=3.12.0,<4.0.0)"]
-
-[[package]]
-name = "sortedcontainers"
-version = "2.4.0"
-description = "Sorted Containers -- Sorted List, Sorted Dict, Sorted Set"
-optional = true
-python-versions = "*"
-files = [
- {file = "sortedcontainers-2.4.0-py2.py3-none-any.whl", hash = "sha256:a163dcaede0f1c021485e957a39245190e74249897e2ae4b2aa38595db237ee0"},
- {file = "sortedcontainers-2.4.0.tar.gz", hash = "sha256:25caa5a06cc30b6b83d11423433f65d1f9d76c4c6a0c90e3379eaa43b9bfdb88"},
-]
-
[[package]]
name = "soupsieve"
version = "2.6"
@@ -7381,17 +7249,6 @@ files = [
{file = "tomli-2.1.0.tar.gz", hash = "sha256:3f646cae2aec94e17d04973e4249548320197cfabdf130015d023de4b74d8ab8"},
]
-[[package]]
-name = "tomlkit"
-version = "0.13.2"
-description = "Style preserving TOML library"
-optional = true
-python-versions = ">=3.8"
-files = [
- {file = "tomlkit-0.13.2-py3-none-any.whl", hash = "sha256:7a974427f6e119197f670fbbbeae7bef749a6c14e793db934baefc1b5f03efde"},
- {file = "tomlkit-0.13.2.tar.gz", hash = "sha256:fff5fe59a87295b278abd31bec92c15d9bc4a06885ab12bcea52c71119392e79"},
-]
-
[[package]]
name = "torch"
version = "2.5.1"
@@ -8152,20 +8009,6 @@ files = [
{file = "websockets-14.1.tar.gz", hash = "sha256:398b10c77d471c0aab20a845e7a60076b6390bfdaac7a6d2edb0d2c59d75e8d8"},
]
-[[package]]
-name = "wheel"
-version = "0.45.0"
-description = "A built-package format for Python"
-optional = true
-python-versions = ">=3.8"
-files = [
- {file = "wheel-0.45.0-py3-none-any.whl", hash = "sha256:52f0baa5e6522155090a09c6bd95718cc46956d1b51d537ea5454249edb671c7"},
- {file = "wheel-0.45.0.tar.gz", hash = "sha256:a57353941a3183b3d5365346b567a260a0602a0f8a635926a7dede41b94c674a"},
-]
-
-[package.extras]
-test = ["pytest (>=6.0.0)", "setuptools (>=65)"]
-
[[package]]
name = "win32-setctime"
version = "1.1.0"
@@ -8523,4 +8366,4 @@ weaviate = ["weaviate-client"]
[metadata]
lock-version = "2.0"
python-versions = ">=3.9,<3.13"
-content-hash = "f54a12cf2b3bc3d2811e8875561b1262c8b7a546d0fb333f075a1127bc2ffcb0"
+content-hash = "c9dd68c3260fa4acd03862d0d0bfa83b94ed096891d3a008eb61e0380f97e1c6"
diff --git a/pyproject.toml b/pyproject.toml
index c0fe14f19..d43c1f281 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -45,6 +45,7 @@ dependencies = [
"anyio",
"asyncer==0.0.8",
"cachetools",
+ "cloudpickle",
]
[project.optional-dependencies]
@@ -129,7 +130,6 @@ rich = "^13.7.1"
psycopg2 = { version = "^2.9.9", optional = true }
pgvector = { version = "^0.2.5", optional = true }
llama-index = { version = "^0.10.30", optional = true }
-snowflake-snowpark-python = { version = "*", optional = true, python = ">=3.9,<3.12" }
jinja2 = "^3.1.3"
magicattr = "^0.1.6"
litellm = "^1.51.0"
@@ -140,6 +140,7 @@ json-repair = "^0.30.0"
tenacity = ">=8.2.3"
asyncer = "0.0.8"
cachetools = "^5.5.0"
+cloudpickle = "^3.0.0"
[tool.poetry.group.dev.dependencies]
pytest = "^8.3.3"
diff --git a/requirements.txt b/requirements.txt
index 218ca9ed7..70eb34727 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -18,3 +18,4 @@ requests
tenacity>=8.2.3
tqdm
ujson
+cloudpickle
diff --git a/tests/predict/test_predict.py b/tests/predict/test_predict.py
index 97544043e..094269c5b 100644
--- a/tests/predict/test_predict.py
+++ b/tests/predict/test_predict.py
@@ -335,29 +335,6 @@ def test_output_only():
assert predictor().output == "short answer"
-def test_chainable_load(tmp_path):
- """Test both traditional and chainable load methods."""
-
- file_path = tmp_path / "test_chainable.json"
-
- original = Predict("question -> answer")
- original.demos = [{"question": "test", "answer": "response"}]
- original.save(file_path)
-
- traditional = Predict("question -> answer")
- traditional.load(file_path)
- assert traditional.demos == original.demos
-
- chainable = Predict("question -> answer").load(file_path, return_self=True)
- assert chainable is not None
- assert chainable.demos == original.demos
-
- assert chainable.signature.dump_state() == original.signature.dump_state()
-
- result = Predict("question -> answer").load(file_path)
- assert result is None
-
-
def test_load_state_chaining():
"""Test that load_state returns self for chaining."""
original = Predict("question -> answer")
@@ -367,7 +344,3 @@ def test_load_state_chaining():
new_instance = Predict("question -> answer").load_state(state)
assert new_instance is not None
assert new_instance.demos == original.demos
-
- legacy_instance = Predict("question -> answer").load_state(state, use_legacy_loading=True)
- assert legacy_instance is not None
- assert legacy_instance.demos == original.demos
diff --git a/tests/primitives/test_module.py b/tests/primitives/test_module.py
index a16905735..5e6790d8e 100644
--- a/tests/primitives/test_module.py
+++ b/tests/primitives/test_module.py
@@ -1,5 +1,6 @@
import dspy
import threading
+from dspy.utils.dummies import DummyLM
def test_deepcopy_basic():
@@ -46,3 +47,62 @@ def test_deepcopy_with_nested_modules():
# Parameters should be different objects with the same values.
assert id(model.parameters()[0]) != id(model_copy.parameters()[0])
assert model.parameters()[0].__dict__ == model_copy.parameters()[0].__dict__
+
+
+def test_save_and_load_with_json(tmp_path):
+ model = dspy.ChainOfThought(dspy.Signature("q -> a"))
+ model._predict.signature = model._predict.signature.with_instructions("You are a helpful assistant.")
+ model._predict.demos = [
+ dspy.Example(q="What is the capital of France?", a="Paris", reasoning="n/a").with_inputs("q", "a")
+ ]
+ save_path = tmp_path / "model.json"
+ model.save(save_path)
+ new_model = dspy.ChainOfThought(dspy.Signature("q -> a"))
+ new_model.load(save_path)
+
+ assert str(new_model.signature) == str(model.signature)
+ assert new_model.demos[0] == model.demos[0].toDict()
+
+
+def test_save_and_load_with_pkl(tmp_path):
+ import datetime
+
+ # `datetime.date` is not json serializable, so we need to save with pickle.
+ class MySignature(dspy.Signature):
+ """Just a custom signature."""
+
+ current_date: datetime.date = dspy.InputField()
+ target_date: datetime.date = dspy.InputField()
+ date_diff: int = dspy.OutputField(desc="The difference in days between the current_date and the target_date")
+
+ trainset = [
+ {"current_date": datetime.date(2024, 1, 1), "target_date": datetime.date(2024, 1, 2), "date_diff": 1},
+ {"current_date": datetime.date(2024, 1, 1), "target_date": datetime.date(2024, 1, 3), "date_diff": 2},
+ {"current_date": datetime.date(2024, 1, 1), "target_date": datetime.date(2024, 1, 4), "date_diff": 3},
+ {"current_date": datetime.date(2024, 1, 1), "target_date": datetime.date(2024, 1, 5), "date_diff": 4},
+ {"current_date": datetime.date(2024, 1, 1), "target_date": datetime.date(2024, 1, 6), "date_diff": 5},
+ ]
+ trainset = [dspy.Example(**example).with_inputs("current_date", "target_date") for example in trainset]
+
+ dspy.settings.configure(
+ lm=DummyLM([{"date_diff": "1", "reasoning": "n/a"}, {"date_diff": "2", "reasoning": "n/a"}] * 10)
+ )
+
+ cot = dspy.ChainOfThought(MySignature)
+ cot(current_date=datetime.date(2024, 1, 1), target_date=datetime.date(2024, 1, 2))
+
+ def dummy_metric(example, pred, trace=None):
+ return True
+
+ optimizer = dspy.BootstrapFewShot(max_bootstrapped_demos=4, max_labeled_demos=4, max_rounds=5, metric=dummy_metric)
+ compiled_cot = optimizer.compile(cot, trainset=trainset)
+ compiled_cot._predict.signature = compiled_cot._predict.signature.with_instructions("You are a helpful assistant.")
+
+ save_path = tmp_path / "program.pkl"
+ compiled_cot.save(save_path)
+
+ new_cot = dspy.ChainOfThought(MySignature)
+ new_cot.load(save_path)
+
+ assert str(new_cot.signature) == str(compiled_cot.signature)
+ assert new_cot.demos == compiled_cot.demos
diff --git a/tests/signatures/test_adapter_image.py b/tests/signatures/test_adapter_image.py
index f1f60502a..8c34f3c41 100644
--- a/tests/signatures/test_adapter_image.py
+++ b/tests/signatures/test_adapter_image.py
@@ -220,7 +220,7 @@ def test_invalid_image_input(sample_url):
assert not messages_contain_image_url_pattern(lm.history[-1]["messages"])
-def test_predictor_save_load(sample_url, sample_pil_image):
+def test_predictor_save_load(tmp_path, sample_url, sample_pil_image):
signature = "image: dspy.Image -> caption: str"
examples = [
dspy.Example(image=dspy.Image.from_url(sample_url)),
@@ -231,10 +231,11 @@ def test_predictor_save_load(sample_url, sample_pil_image):
predictor = dspy.Predict(signature)
optimizer = dspy.teleprompt.LabeledFewShot()
compiled_predictor = optimizer.compile(student=predictor, trainset=examples[:1])
- # Test dump state with save verbose = True and False
- with tempfile.NamedTemporaryFile(mode='w+', delete=True) as temp_file:
- compiled_predictor.save(temp_file.name)
- loaded_predictor = dspy.Predict(signature)
- loaded_predictor.load(temp_file.name)
-
+
+ path = tmp_path / "model.json"
+
+ compiled_predictor.save(path)
+ loaded_predictor = dspy.Predict(signature)
+ loaded_predictor.load(path)
+
assert isinstance(loaded_predictor.demos[0]["image"], dspy.Image)
\ No newline at end of file
diff --git a/tests/utils/test_saving.py b/tests/utils/test_saving.py
new file mode 100644
index 000000000..6a0089946
--- /dev/null
+++ b/tests/utils/test_saving.py
@@ -0,0 +1,76 @@
+import dspy
+from dspy.utils import DummyLM
+
+
+def test_save_predict(tmp_path):
+ predict = dspy.Predict("question->answer")
+ predict.save(tmp_path, save_program=True)
+
+ assert (tmp_path / "metadata.json").exists()
+ assert (tmp_path / "program.pkl").exists()
+
+ loaded_predict = dspy.load(tmp_path)
+ assert isinstance(loaded_predict, dspy.Predict)
+
+ assert predict.signature == loaded_predict.signature
+
+
+def test_save_custom_model(tmp_path):
+ class CustomModel(dspy.Module):
+ def __init__(self):
+ self.cot1 = dspy.ChainOfThought("question->refined_question")
+ self.cot2 = dspy.ChainOfThought("refined_question->answer")
+
+ model = CustomModel()
+ model.save(tmp_path, save_program=True)
+
+ loaded_model = dspy.load(tmp_path)
+ assert isinstance(loaded_model, CustomModel)
+
+ assert len(model.predictors()) == len(loaded_model.predictors())
+ for predictor, loaded_predictor in zip(model.predictors(), loaded_model.predictors()):
+ assert predictor.signature == loaded_predictor.signature
+
+
+def test_save_model_with_custom_signature(tmp_path):
+ import datetime
+
+ class MySignature(dspy.Signature):
+ """Just a custom signature."""
+
+ current_date: datetime.date = dspy.InputField()
+ target_date: datetime.date = dspy.InputField()
+ date_diff: int = dspy.OutputField(desc="The difference in days between the current_date and the target_date")
+
+ predict = dspy.Predict(MySignature)
+ predict.signature = predict.signature.with_instructions("You are a helpful assistant.")
+ predict.save(tmp_path, save_program=True)
+
+ loaded_predict = dspy.load(tmp_path)
+ assert isinstance(loaded_predict, dspy.Predict)
+
+ assert predict.signature == loaded_predict.signature
+
+
+def test_save_compiled_model(tmp_path):
+ predict = dspy.Predict("question->answer")
+ dspy.settings.configure(lm=DummyLM([{"answer": "blue"}, {"answer": "white"}] * 10))
+
+ trainset = [
+ {"question": "What is the color of the sky?", "answer": "blue"},
+ {"question": "What is the color of the ocean?", "answer": "blue"},
+ {"question": "What is the color of the milk?", "answer": "white"},
+ {"question": "What is the color of the coffee?", "answer": "black"},
+ ]
+ trainset = [dspy.Example(**example).with_inputs("question") for example in trainset]
+
+ def dummy_metric(example, pred, trace=None):
+ return True
+
+ optimizer = dspy.BootstrapFewShot(max_bootstrapped_demos=4, max_labeled_demos=4, max_rounds=5, metric=dummy_metric)
+ compiled_predict = optimizer.compile(predict, trainset=trainset)
+ compiled_predict.save(tmp_path, save_program=True)
+
+ loaded_predict = dspy.load(tmp_path)
+ assert compiled_predict.demos == loaded_predict.demos
+ assert compiled_predict.signature == loaded_predict.signature