Overview
DSPy (Declarative Self-improving Language Programs) is a framework from Stanford University that introduces a declarative paradigm for building LLM applications. Instead of writing prompts, you define modules and signatures, and DSPy automatically optimizes prompts and few-shot examples. It treats LLM applications as programs that can be compiled and optimized, enabling reproducible, high-quality results without manual prompt engineering.
Features
- ✓Declarative module-based programming model
- ✓Automatic prompt optimization and few-shot selection
- ✓Signature-based interface definitions
- ✓Support for multiple LLM providers
- ✓Built-in evaluation and testing framework
- ✓Compile-time optimization of prompts
Installation
pip install dspy-aiPros
- +Eliminates manual prompt engineering
- +Reproducible and versionable LLM applications
- +Strong academic backing from Stanford
- +Automatic optimization saves development time
- +Clean, modular architecture
Cons
- −Steep learning curve for the declarative paradigm
- −Requires understanding of DSPy abstractions
- −Optimization can be computationally expensive
- −Smaller community than LangChain
- −Documentation still evolving
Alternatives
Documentation
DSPy
Overview
DSPy (Declarative Self-improving Language Programs) is a revolutionary framework from Stanford University that introduces a declarative paradigm for building LLM applications. Instead of writing and tweaking prompts manually, you define modules and signatures, and DSPy automatically optimizes prompts and few-shot examples through a compilation process.
DSPy treats LLM applications as programs that can be compiled, optimized, and versioned. This approach eliminates the fragile, trial-and-error nature of prompt engineering and enables reproducible, high-quality results.
Installation
pip install dspy-ai
Quick Start
import dspy
# Define your signature (input/output contract)
class GenerateAnswer(dspy.Signature):
"""Answer questions with short factoid answers."""
context = dspy.InputField(desc="may contain relevant facts")
question = dspy.InputField()
answer = dspy.OutputField(desc="short factual answer")
# Define a module
class RAG(dspy.Module):
def __init__(self, num_passages=3):
super().__init__()
self.retrieve = dspy.Retrieve(k=num_passages)
self.generate_answer = dspy.ChainOfThought(GenerateAnswer)
def forward(self, question):
context = self.retrieve(question).passages
prediction = self.generate_answer(context=context, question=question)
return dspy.Prediction(context=context, answer=prediction.answer)
# Compile the program
rag_program = RAG()
compiled_rag = dspy.CompiledProgram(rag_program)
Core Concepts
Signatures
Signatures define the interface for your modules, specifying input and output fields with optional descriptions.
Modules
Modules are the building blocks of DSPy programs. They can be simple (like ChainOfThought) or custom.
Compilation
Compilation is the process of optimizing your program's prompts and few-shot examples for your specific task and data.
Advanced Features
Custom Optimizers
from dspy.teleprompt import BootstrapFewShot
optimizer = BootstrapFewShot(metric=validate_answer)
compiled_program = optimizer.compile(rag_program, trainset=trainset)
Multi-Step Reasoning
class MultiHopQA(dspy.Module):
def __init__(self):
super().__init__()
self.search = dspy.Retrieve()
self.generate_hypothesis = dspy.ChainOfThought("question -> hypothesis")
self.verify = dspy.ChainOfThought("hypothesis, context -> is_correct")
Examples
Question Answering
question = "Who wrote the novel '1984'?"
prediction = compiled_rag(question)
print(f"Answer: {prediction.answer}")
Classification
class ClassifySentiment(dspy.Signature):
"""Classify the sentiment of a text."""
text = dspy.InputField()
sentiment = dspy.OutputField(desc="positive, negative, or neutral")
Use Cases
DSPy excels in scenarios requiring reliable, production-grade LLM applications:
| Use Case | Why DSPy |
|---|---|
| Production RAG Systems | Automatic prompt optimization ensures consistent quality |
| Classification Pipelines | Declarative signatures make intent clear |
| Multi-Step Reasoning | Chain-of-thought modules compose cleanly |
| Research Projects | Reproducible, versionable experiments |
| Enterprise LLM Apps | Audit trails and evaluation built-in |
Pros & Cons
✅ Pros
- Eliminates manual prompt engineering — Automatic optimization saves hours of tweaking
- Reproducible and versionable — Programs can be compiled, saved, and versioned
- Strong academic backing — Created by Stanford NLP researchers
- Automatic optimization — Few-shot examples and prompts are auto-generated
- Clean, modular architecture — Easy to compose and extend
- Built-in evaluation — Test and iterate with automated metrics
- Multi-provider support — Works with OpenAI, Anthropic, Google, and local models
❌ Cons
- Steep learning curve — Declarative paradigm takes time to master
- Requires understanding DSPy abstractions — Signatures, modules, optimizers
- Optimization can be computationally expensive — Compilation takes time and API calls
- Smaller community than LangChain — Fewer tutorials and third-party resources
- Documentation still evolving — Some areas lack detailed examples
Comparison with Alternatives
| Feature | DSPy | LangChain | CrewAI |
|---|---|---|---|
| Paradigm | Declarative (compile) | Imperative (chain) | Role-based teams |
| Prompt Engineering | Automatic | Manual | Manual |
| Reproducibility | Excellent | Good | Good |
| Learning Curve | High | Medium | Low |
| Best for | Production LLM apps | Flexible integrations | Multi-agent workflows |
Best Practices
- Start with simple signatures — Define clear input/output contracts before adding complexity.
- Use BootstrapFewShot for quick wins — Auto-generate few-shot examples from a small training set.
- Define custom metrics — DSPy optimizes based on your metric function.
- Version your compiled programs — Save compiled programs for reproducibility.
- Use ChainOfThought for reasoning tasks — Built-in module for step-by-step thinking.
- Test with a validation set — Always evaluate on held-out data.
Troubleshooting
| Issue | Solution |
|---|---|
| Compilation takes too long | Reduce trainset size, use cheaper models for optimization |
| Poor output quality | Improve signature descriptions, add more training examples |
| Metric not working | Ensure metric returns a float between 0 and 1 |
| Module not executing | Check signature field names match exactly |
Resources
Last updated: June 2026
