Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions dspy/primitives/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,15 @@ def save(self, path, save_program=False):
state = self.dump_state()
state["metadata"] = metadata
if path.suffix == ".json":
with open(path, "w") as f:
f.write(ujson.dumps(state, indent=2))
try:
with open(path, "w") as f:
f.write(ujson.dumps(state, indent=2))
except Exception as e:
raise RuntimeError(
f"Failed to save state to {path} with error: {e}. Your DSPy program may contain non "
"json-serializable objects, please consider saving the state in .pkl by using `path` ending "
"with `.pkl`, or saving the whole program by setting `save_program=True`."
)
elif path.suffix == ".pkl":
with open(path, "wb") as f:
cloudpickle.dump(state, f)
Expand Down
Loading