How to compile DSPy programs
Quick answer
To compile
DSPy programs, define your dspy.Signature classes and use dspy.Predict to create callable modules. Then call the module with input arguments to get compiled outputs. Use dspy.configure() with an LM instance for OpenAI or other LLM providers.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
Define a dspy.Signature class for your program, configure the LLM, create a dspy.Predict instance, and call it with inputs to compile and run your DSPy program.
import os
import dspy
from openai import OpenAI
# Configure the LLM with OpenAI API key
lm = dspy.LM("openai/gpt-4o-mini", api_key=os.environ["OPENAI_API_KEY"])
dspy.configure(lm=lm)
# Define a DSPy signature for a simple QA task
class QA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
# Create a DSPy Predict module
qa = dspy.Predict(QA)
# Compile and run the DSPy program
result = qa(question="What is DSPy?")
print("Answer:", result.answer) output
Answer: DSPy is a declarative Python framework for AI programming that compiles structured prompts to LLMs.
Common variations
You can use different LLM models by changing the dspy.LM model string, or switch to async calls by using await qa(question="...") inside an async function. DSPy also supports chain-of-thought with dspy.ChainOfThought.
import asyncio
async def async_example():
result = await qa(question="Explain RAG.")
print("Async answer:", result.answer)
asyncio.run(async_example()) output
Async answer: RAG stands for Retrieval-Augmented Generation, combining retrieval of documents with LLM generation.
Troubleshooting
If you get errors like API key missing, ensure OPENAI_API_KEY is set in your environment. For AttributeError, verify you installed the latest dspy and openai packages. If responses are empty, check your prompt and model usage.
Key Takeaways
- Use
dspy.LMwith your OpenAI API key to configure DSPy compilation. - Define structured input/output with
dspy.Signatureclasses for clean program interfaces. - Call
dspy.Predictinstances with inputs to compile and run DSPy programs. - DSPy supports async calls and chain-of-thought for advanced workflows.
- Always verify environment variables and package versions to avoid common errors.