Builders Spotlight — DSPy
The story and philosophy behind one open-source AI project: what drove it, what makes it different, and why it matters.
DSPy
One sentence: DSPy—“Declarative Self-improving Python”—is a framework from Stanford NLP for programming language models instead of prompting them, replacing hand-tuned prompt strings with composable code that an optimizer then compiles into high-quality prompts and weights.
The problem it set out to solve
Prompt engineering is brittle. You hand-write a wall of instructions, tweak it until it works on a few examples, and then it silently breaks when you swap models, change your data, or add a step to your pipeline. Every change means re-tuning strings by trial and error, and none of that effort transfers. For multi-stage LLM systems—retrieve, reason, verify, summarize—this gets exponentially worse: the prompts interact, and there’s no principled way to improve the whole pipeline at once. Omar Khattab and the Stanford NLP group saw teams pouring engineering hours into fragile prompt strings that couldn’t be tested, versioned, or optimized like real software.
The key insight
Treat prompting as a compilation target, not source code. You declare what you want—the inputs and outputs of each step—and let an optimizer figure out how to phrase it. In DSPy you write a signature (a typed input/output contract like question -> answer) and wrap it in a module (a strategy like chain-of-thought or ReAct). Your program is now ordinary, composable Python. Then, given a metric and a handful of examples, a DSPy optimizer searches for the best instructions and few-shot demonstrations automatically—on whatever model you point it at. The prompt becomes an artifact the machine produces, not one you babysit.
How it works (in plain terms)
Three pieces stack together. Signatures declare the task’s shape (context, question -> answer). Modules implement a prompting technique against that signature and compose into arbitrary pipelines—the same way you’d compose functions. Optimizers (formerly called “teleprompters”) take your pipeline, a training set, and a metric, then bootstrap better prompts and few-shot examples by actually running the program and keeping what works. Recent optimizers push this further: MIPRO jointly tunes instructions and demonstrations, SIMBA adds stochastic search, GEPA (Genetic-Pareto Prompt Evolution) reads full execution traces and reflects on why a run failed to propose targeted fixes rather than random mutations, and dspy.GRPO brings online reinforcement learning to optimize a pipeline’s weights—without changing a line of your program’s abstractions.
What it looks like in practice
import dspy
# Point DSPy at any backend model
dspy.configure(lm=dspy.LM("openai/gpt-4.1-nano"))
# A signature declares the task; a module picks the strategy
class GenerateAnswer(dspy.Signature):
"""Answer questions with a short factual response."""
context: str = dspy.InputField()
question: str = dspy.InputField()
answer: str = dspy.OutputField()
rag = dspy.ChainOfThought(GenerateAnswer)
# Compile: let an optimizer write the prompt for you
optimized = dspy.MIPROv2(metric=my_metric).compile(
rag, trainset=train_examples
)
# Swap the model later and just re-compile — no prompt rewriting
print(optimized(context=ctx, question="Who built DSPy?").answer)
Why it matters
- Prompts become testable software: Because the pipeline is code with metrics, you can version it, evaluate it, and regression-test it. “It got worse” becomes a number, not a vibe.
- Portability across models: The same program re-compiles against a new model. When the frontier shifts—and it shifts constantly—you re-run the optimizer instead of rewriting prompts by hand.
- Optimization beats intuition: DSPy’s optimizers routinely find few-shot and instruction combinations a human wouldn’t have tried, closing much of the gap between a quick prototype and a tuned system automatically.
- It shaped the field: The “programming, not prompting” framing and the compiler metaphor have influenced a wave of downstream tooling and research; GEPA’s reflective optimization landed as an ICLR 2026 Oral.
Where to go next
- GitHub: github.com/stanfordnlp/dspy — source, examples, and active development
- Docs: dspy.ai — tutorials on signatures, modules, and optimizers, plus the project roadmap
- The paper: DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines — the original formulation of the compile-your-prompts idea