How to configure DSPy LM
Quick answer
To configure a
dspy.LM, import dspy and instantiate dspy.LM with the model name and your API key from os.environ. Then call dspy.configure(lm=lm) to set it as the default language model for DSPy workflows.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install dspy openai>=1.0
Setup
Install the dspy package and ensure your OpenAI API key is set in the environment variable OPENAI_API_KEY. This key is required to authenticate requests to the OpenAI API.
pip install dspy openai>=1.0 Step by step
Import dspy and os. Create an instance of dspy.LM with your model and API key from os.environ. Then configure DSPy to use this LM instance. Finally, define a signature and run a prediction.
import os
import dspy
# Instantiate the DSPy LM with model and API key
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
# Configure DSPy to use this LM
dspy.configure(lm=lm)
# Define a signature for a simple QA task
class QA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
# Create a prediction callable
qa = dspy.Predict(QA)
# Run a prediction
result = qa(question="What is DSPy?")
print(result.answer) output
DSPy is a declarative Python library for building AI applications with clean, type-safe signatures.
Common variations
- Use different OpenAI models by changing the model string, e.g.,
"openai/gpt-4o". - Configure DSPy with other LMs by instantiating
dspy.LMwith different providers. - For async usage, DSPy supports async signatures and calls.
Troubleshooting
- If you get authentication errors, verify your
OPENAI_API_KEYis correctly set in your environment. - If the model is not found, confirm the model name is valid and supported by your API key.
- For import errors, ensure
dspyandopenaipackages are installed and up to date.
Key Takeaways
- Always instantiate
dspy.LMwith your API key fromos.environfor security. - Call
dspy.configure(lm=lm)to set the default LM for DSPy workflows. - You can switch models easily by changing the model string in
dspy.LM. - DSPy supports both synchronous and asynchronous usage patterns.
- Check environment variables and package versions to resolve common errors.