Features
- Automatic prompt optimization via compilation
- Declarative modules replace hand-written prompts
- Built-in evaluation and optimization metrics
- Support for multiple LLM backends
Pros
- Eliminates manual prompt engineering
- Systematic approach to LLM pipeline optimization
- Academic rigor from Stanford NLP research
Cons
- Steep learning curve with unique paradigm
- Python-only with no JavaScript support
- Compilation can be slow and expensive
Overview
DSPy is a framework from Stanford NLP that takes a fundamentally different approach to working with LLMs. Instead of writing prompts by hand, you define declarative modules (signatures) that describe what you want, and DSPy’s compiler automatically optimizes the prompts, few-shot examples, and pipeline configurations to maximize a metric you define.
The framework treats LLM calls as differentiable operations that can be optimized. You write your pipeline as a series of modules (like ChainOfThought, ReAct, or custom modules), provide a training set and evaluation metric, and DSPy compiles the pipeline by finding the best prompts and examples.
This systematic approach replaces the trial-and-error of manual prompt engineering with reproducible, measurable optimization.
When to Use
Choose DSPy when you have clear evaluation metrics and want to systematically optimize LLM pipeline performance. It is ideal for research teams and production systems where prompt quality directly impacts outcomes.
Getting Started
pip install dspy
import dspy
lm = dspy.LM('anthropic/claude-sonnet-4-5-20250929')
dspy.configure(lm=lm)
classify = dspy.ChainOfThought('sentence -> sentiment: str')
result = classify(sentence="This movie was amazing!")