Fix DSPy optimization not improving
Quick answer
To fix
dspy optimization not improving, ensure you configure the LM instance with the correct model and API key, use the dspy.configure() method properly, and run training or prediction with the right signature and input. Also, verify your training loop calls qa(question="...") instead of qa.predict(), as the latter is deprecated and can cause no optimization progress.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install dspy openai>=1.0
Setup
Install dspy and openai packages, and set your OpenAI API key as an environment variable.
- Run
pip install dspy openai - Set environment variable
OPENAI_API_KEYwith your API key
pip install dspy openai Step by step
Use the correct dspy pattern for optimization by configuring the LM, defining a signature, and invoking prediction properly.
import os
import dspy
from openai import OpenAI
# Initialize LM with OpenAI GPT-4o-mini
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)
# Define a signature for QA task
class QA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
# Create a prediction module
qa = dspy.Predict(QA)
# Run prediction with correct call (not qa.predict)
result = qa(question="What is retrieval-augmented generation?")
print("Answer:", result.answer) output
Answer: Retrieval-augmented generation (RAG) is a technique that combines retrieval of relevant documents with generative models to produce more accurate and context-aware responses.
Common variations
You can use different models by changing the LM initialization string, or run asynchronous calls with dspy if supported. Avoid deprecated methods like qa.predict() which do not trigger optimization.
import asyncio
async def async_example():
result = await qa.acall(question="Explain chain-of-thought prompting.")
print("Async answer:", result.answer)
asyncio.run(async_example()) output
Async answer: Chain-of-thought prompting is a technique where the model is guided to reason step-by-step, improving complex problem solving.
Troubleshooting
- If optimization does not improve, confirm you are using
qa(question="...")instead ofqa.predict(). - Check your API key is valid and environment variable is set.
- Ensure
dspy.configure(lm=lm)is called before using the signature. - Verify your model string matches a supported model like
openai/gpt-4o-mini.
Key Takeaways
- Always configure
dspywith a validLMinstance before use. - Use
qa(question="...")for prediction to enable optimization, notqa.predict(). - Check environment variables and model names to avoid silent failures.
- Async calls require
awaitandacallmethod usage. - Deprecated methods can cause no optimization progress; update your code accordingly.