Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] save() is broken when predict has lm instance #7852

Open
chenmoneygithub opened this issue Feb 26, 2025 · 0 comments
Open

[Bug] save() is broken when predict has lm instance #7852

chenmoneygithub opened this issue Feb 26, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@chenmoneygithub
Copy link
Collaborator

What happened?

save() is broken when predict has lm assigned. See code below for reproducing.

To fix it, we can serialize the model name, then at loading time, reconstruct the lm instance by calling dspy.LM(saved_model_name)

Steps to reproduce

>>> import dspy
>>> predict = dspy.Predict("q->a")
>>> predict.lm = dspy.LM("openai/gpt-4o-mini")
>>> predict.save("test_lm_saving.json", save_program=False)

Raises error:

>>> predict.save("test_lm_saving.json", save_program=False)
Traceback (most recent call last):
  File "/Users/chen.qian/Documents/mlflow_team/dspy/dspy/primitives/module.py", line 218, in save
    f.write(ujson.dumps(state, indent=2))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: <dspy.clients.lm.LM object at 0x10563c2c0> is not JSON serializable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/chen.qian/Documents/mlflow_team/dspy/dspy/primitives/module.py", line 220, in save
    raise RuntimeError(
RuntimeError: Failed to save state to test_lm_saving.json with error: <dspy.clients.lm.LM object at 0x10563c2c0> is not JSON serializable. 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`.

DSPy version

2.6.6

@chenmoneygithub chenmoneygithub added the bug Something isn't working label Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant