@@ -7,7 +7,7 @@ This guide demonstrates how to save and load your DSPy program. At a high level,
7
7
8
8
## State-only Saving
9
9
10
- State represents the DSPy program's internal state, including the signature, demos (few-shot examples), and other informaiton like
10
+ State represents the DSPy program's internal state, including the signature, demos (few-shot examples), and other information like
11
11
the ` lm ` to use for each ` dspy.Predict ` in the program. It also includes configurable attributes of other DSPy modules like
12
12
` k ` for ` dspy.retrievers.Retriever ` . To save the state of a program, use the ` save ` method and set ` save_program=False ` . You can
13
13
choose to save the state to a JSON file or a pickle file. We recommend saving the state to a JSON file because it is safer and readable.
@@ -94,7 +94,39 @@ assert str(compiled_dspy_program.signature) == str(loaded_dspy_program.signature
94
94
```
95
95
96
96
With whole program saving, you don't need to recreate the program, but can directly load the architecture along with the state.
97
- You can pick the suitable saviing approach based on your needs.
97
+ You can pick the suitable saving approach based on your needs.
98
+
99
+ ### Serializing Imported Modules
100
+
101
+ When saving a program with ` save_program=True ` , you might need to include custom modules that your program depends on.
102
+
103
+ You can specify which custom modules should be serialized with your program by passing them to the ` modules_to_serialize `
104
+ parameter when calling ` save ` . This ensures that any dependencies your program relies on are included during serialization and
105
+ available when loading the program later.
106
+
107
+ This uses cloudpickle's ` cloudpickle.register_pickle_by_value ` function in order to register a module as picklable by value. When
108
+ a module is registered this way, cloudpickle will serialize the module by value rather than by reference, ensuring that the
109
+ module contents are preserved with the saved program.
110
+
111
+ For example, if your program uses custom modules:
112
+
113
+ ``` python
114
+ import dspy
115
+ import my_custom_module
116
+
117
+ compiled_dspy_program = dspy.ChainOfThought(my_custom_module.custom_signature)
118
+
119
+ # Save the program with the custom module
120
+ compiled_dspy_program.save(
121
+ " ./dspy_program/" ,
122
+ save_program = True ,
123
+ modules_to_serialize = [my_custom_module]
124
+ )
125
+ ```
126
+
127
+ This ensures that the required modules are properly serialized and available when loading the program later. Any number of
128
+ modules can be passed to ` modules_to_serialize ` . If you don't specify ` modules_to_serialize ` , no additional modules will be
129
+ registered for serialization.
98
130
99
131
## Backward Compatibility
100
132
@@ -104,4 +136,4 @@ are that loading a saved file in a different version of DSPy will not raise an e
104
136
the program was saved.
105
137
106
138
Starting from ` dspy>=2.7 ` , we will guarantee the backward compatibility of the saved program in major releases, i.e., programs saved in ` dspy==2.7.0 `
107
- should be loadeable in ` dspy==2.7.10 ` .
139
+ should be loadable in ` dspy==2.7.10 ` .
0 commit comments