DSPy
Type: full-code · Vendor: Stanford NLP · Language: Python · License: MIT · Status: active · Status in practice: mature · First released: 2023-01-09
Replace hand-tuned prompts with a declarative Python programming model in which you specify input/output behaviour as Signatures, compose Modules (Predict, ChainOfThought, ReAct, ProgramOfThought), and let Optimizers (BootstrapFewShot, MIPROv2, BootstrapFinetune) tune prompts and weights against a metric.
Description. DSPy is the MIT-licensed framework for 'programming—rather than prompting—language models'. DSPy stands for Declarative Self-improving Python. It exposes three layers: Signatures declare what an LM should do (e.g. 'question -> answer'); Modules implement reasoning shapes around those signatures (dspy.Predict, dspy.ChainOfThought, dspy.ReAct, dspy.ProgramOfThought); Optimizers consume a metric and training data to 'tune the prompts and weights of your AI modules' — synthesising few-shot demos, proposing better natural-language instructions, or fine-tuning small LMs. Most often used for classifiers, RAG pipelines, and agent loops where empirically tuned prompts beat hand-crafted strings.
Agent loop shape. DSPy compiles to whatever loop the module declares. A dspy.ReAct agent runs the classic thought→action→observation cycle against a signature plus a tools list; dspy.ProgramOfThought emits and executes code; dspy.ChainOfThought adds a reasoning step before the signature output. At authoring time you only write the signature and pick the module. At compile time, an Optimizer (e.g. MIPROv2) iterates with a teacher LM, generates instructions and few-shot examples per step, and uses Bayesian Optimization to search the space of instructions/demonstrations.
Primary use cases
- declarative programming of LM behaviour via Signatures
- compiler-driven prompt + weight optimisation against a metric
- ReAct-style agents whose prompts are co-tuned with the loop
- RAG pipelines whose intermediate prompts get optimised
Key concepts
- Signature (docs) — Declarative I/O spec ('question -> answer').
- Module → react (docs) — Predict / ChainOfThought / ReAct / ProgramOfThought wrappers around a signature.
- Optimizer → evaluator-optimizer (docs) — BootstrapFewShot / MIPROv2 / BootstrapFinetune tune prompts and weights against a metric.
- Compiler / teacher LM — Optimizer uses a teacher LM (often the program itself) to bootstrap demos.
- dspy.ReAct (docs) — Tool-using agent module over a signature and a tools list.
Patterns this full-code implements —
- ·Automatic Workflow Search
Optimizers use Bayesian Optimization to search instruction + demonstration space.
- ★Best-of-N Sampling
Optimizer paradigm is synthesise-many, score, keep-best; demos are bootstrapped from teacher.
- ★★Chain of Thought
dspy.ChainOfThought is a named module.
- ★DSPy Signatures
Signatures are the declarative core.
- ★★Evaluator-Optimizer
Optimizers tune prompts and weights against a metric — the canonical evaluator-optimizer loop.
- ★★ReAct
dspy.ReAct is a named tool-using agent module.
- ★★Structured Output
Signatures enforce typed I/O; compiler builds optimised prompts/finetunes for the signature.
- ★★Tool Use
Tool list passed to dspy.ReAct.
- ★★Self-Refine
Self-refine emerges through optimizer iteration; no single canonical Self-Refine module.
Neighbourhood
Click any neighbour to follow the lineage. Scroll to zoom, drag to pan.