DSPy pipeline explained
Quick answer
A
DSPy pipeline is a declarative way to compose AI tasks by defining Signatures and chaining them with dspy.Predict or dspy.ChainOfThought. It enables structured, reusable AI workflows with simple Python classes and method calls.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install dspy openai>=1.0
Setup
Install the dspy package and set your OpenAI API key as an environment variable.
- Run
pip install dspy openaito install dependencies. - Set your API key in your shell:
export OPENAI_API_KEY='your_api_key'(Linux/macOS) orsetx OPENAI_API_KEY "your_api_key"(Windows).
pip install dspy openai Step by step
Define a dspy.Signature class to specify input and output fields, then create a dspy.Predict instance to run the AI model. This example extracts a summary from a given text.
import os
import dspy
from openai import OpenAI
# Configure DSPy with OpenAI LLM
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
llm = dspy.LM("openai/gpt-4o-mini", client=client)
dspy.configure(lm=llm)
# Define a Signature for the pipeline
class Summarize(dspy.Signature):
text: str = dspy.InputField()
summary: str = dspy.OutputField()
# Create a Predict instance
summarize = dspy.Predict(Summarize)
# Run the pipeline
result = summarize(text="DSPy is a declarative Python library for AI pipelines.")
print("Summary:", result.summary) output
Summary: DSPy is a Python library that simplifies building AI pipelines declaratively.
Common variations
You can use dspy.ChainOfThought for step-by-step reasoning pipelines or switch models by changing the dspy.LM initialization. Async usage is not currently supported directly.
import dspy
# Chain of Thought example
class QA(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField()
qa = dspy.ChainOfThought(QA)
result = qa(question="What is DSPy?")
print("Answer:", result.answer) output
Answer: DSPy is a declarative Python library that simplifies AI programming by defining structured tasks.
Troubleshooting
- If you see
API key missing, ensureOPENAI_API_KEYis set in your environment. - If the model call fails, verify your internet connection and API quota.
- For unexpected output, check your
Signaturefields match the prompt requirements.
Key Takeaways
- Use
dspy.Signatureto define structured AI tasks with typed inputs and outputs. - Create pipelines with
dspy.Predictfor simple calls ordspy.ChainOfThoughtfor reasoning. - Configure DSPy with an OpenAI client using your API key from environment variables.
- DSPy pipelines enable clean, reusable, and declarative AI workflows in Python.
- Check environment variables and model names if you encounter API errors.