DSPyDSPy 3.3 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
24 small wins to finish your pathNext lesson

Saving and loading an optimized program

save writes a program's learned state, its demos and instructions, to a JSON file. load reads it into a fresh program built from the same code.

Example
compiled = dspy.LabeledFewShot(k=12).compile(dspy.Predict(Triage), trainset=trainset)
compiled.save("triage.json")

state = json.load(open("triage.json"))
print(list(state))
print(len(state["demos"]), state["demos"][0])
print(state["signature"]["instructions"])

The file holds the demos, the signature's instructions and field prefixes, and the DSPy and Python versions it was saved with. It does not hold the code: Triage, the module's structure and the model setting stay in your program.

Example
fresh = dspy.Predict(Triage)
fresh.load("triage.json")
print(len(fresh.demos))
print(dspy.Evaluate(devset=devset, metric=exact, num_threads=1, display_progress=False)(fresh).score)

The reloaded program has the same twelve demos and the same 87.5 as when it was compiled. That is the working pattern: compile once, which can be slow and expensive, commit the JSON, and load it in the app.

Saving the whole program

save("folder", save_program=True) uses cloudpickle to save the module's code as well, and dspy.load("folder") restores it without the class definitions. Loading a pickle can run code, so only load files you created yourself. The JSON form is the safer default.

Try it yourself
  • Load the JSON into a dspy.ChainOfThought(Triage) and read the error.
  • Edit a demo's label in the JSON by hand and evaluate again.
  • Save the Desk module and look at the keys.

Every expert started right here.