I cannot save the model , pickle error
I'm using this class
class ClassifyEmergencyCall (dspy.Signature): """Classifies emergency calls into multiple categories from a predefined list.""" llamada = dspy.InputField(desc="The transcription of the emergency call") ubicacion = dspy.InputField(desc="The location mentioned in the call") categorias = dspy.OutputField(desc=f"List of categories that apply to this emergency call, ONLY from the list of valid categories provided ({VALID_CATEGORIES_STR}, if you find another category that is not on the list, do not return it)", format=list)
and when I try to save the model, I got this:
Can't pickle StringSignature(llamada, ubicacion -> rationale, categorias .....rest of error... ): attribute lookup StringSignature on dspy.signatures.signature failed
Regards.
I verified that the code below works well with dspy 2.5.41:
import pickle
import dspy
VALID_CATEGORIES_STR = ["foo", "bar"]
class ClassifyEmergencyCall(dspy.Signature):
"""Classifies emergency calls into multiple categories from a predefined list."""
llamada = dspy.InputField(desc="The transcription of the emergency call")
ubicacion = dspy.InputField(desc="The location mentioned in the call")
categorias = dspy.OutputField(
desc=f"List of categories that apply to this emergency call, ONLY from the list of valid categories provided ({VALID_CATEGORIES_STR}, if you find another category that is not on the list, do not return it)",
format=list,
)
predict = dspy.Predict(ClassifyEmergencyCall)
pickle.dump(predict, open("predict.pkl", "wb"))
geez = pickle.load(open("predict.pkl", "rb"))
print(geez)
Closing this issue.
@chenmoneygithub At least some modules cannot be saved.
- predict = dspy.Predict(ClassifyEmergencyCall)
+ predict = dspy.ChainOfThought(ClassifyEmergencyCall)
Traceback (most recent call last):
File "/tmp/test_save.py", line 21, in <module>
pickle.dump(predict, open("predict.pkl", "wb"))
_pickle.PicklingError: Can't pickle StringSignature(llamada, ubicacion -> reasoning, categorias
instructions='Classifies emergency calls into multiple categories from a predefined list.'
llamada = Field(annotation=str required=True json_schema_extra={'desc': 'The transcription of the emergency call', '__dspy_field_type': 'input', 'prefix': 'Llamada:'})
ubicacion = Field(annotation=str required=True json_schema_extra={'desc': 'The location mentioned in the call', '__dspy_field_type': 'input', 'prefix': 'Ubicacion:'})
reasoning = Field(annotation=str required=True json_schema_extra={'prefix': "Reasoning: Let's think step by step in order to", 'desc': '${reasoning}', '__dspy_field_type': 'output'})
categorias = Field(annotation=str required=True json_schema_extra={'desc': "List of categories that apply to this emergency call, ONLY from the list of valid categories provided (['foo', 'bar'], if you find another category that is not on the list, do not return it)", 'format': <class 'list'>, '__dspy_field_type': 'output', 'prefix': 'Categorias:'})
): attribute lookup StringSignature on dspy.signatures.signature failed
cc @okhat can we re-open this issue?
As of dspy==3.0.3, modules that were initialized from a signature string are also not pickleable:
import pickle
import dspy
predict = dspy.Predict("question: str -> answer: str")
pickle.dump(predict, open("predict.pkl", "wb"))
geez = pickle.load(open("predict.pkl", "rb"))
print(geez)
Traceback (most recent call last):
File "<input>", line 6, in <module>
_pickle.PicklingError: Can't pickle StringSignature(question -> answer
instructions='Given the fields `question`, produce the fields `answer`.'
question = Field(annotation=str required=True json_schema_extra={'__dspy_field_type': 'input', 'prefix': 'Question:', 'desc': '${question}'})
answer = Field(annotation=str required=True json_schema_extra={'__dspy_field_type': 'output', 'prefix': 'Answer:', 'desc': '${answer}'})
): attribute lookup StringSignature on dspy.signatures.signature failed
@okhat can you upgrade gepa in dspy, so the fix gets in?
https://github.com/gepa-ai/gepa/pull/94
update: upgraded dspy to 3.1.0b1, which solves it