Match each DSPy optimizer to its core search strategy
DSPy optimizers vary by what they edit (demos vs instructions vs weights) and how they search (greedy bootstrap, random, Bayesian, coordinate ascent, gradient).
Picture a chef trying to nail a dish for a critic. BootstrapFewShot watches a master chef make it five times and copies the bites the critic liked. RandomSearch tries random combinations of plates and keeps the best. COPRO tweaks one ingredient at a time, keeping the change if the critic smiles. MIPROv2 is the data-driven chef who runs a smart trial and error plan over both the recipe and the plating. BootstrapFinetune sends the apprentice back to culinary school to actually learn new techniques.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
5 minutes: walk the two axes (what edited, how searched), map each optimizer onto the axes, justify the escalation order from BootstrapFewShot to MIPROv2, and call out BootstrapFinetune as the weight-touching outlier.
import dspy
from dspy.teleprompt import BootstrapFewShot, BootstrapFewShotWithRandomSearch, COPRO, MIPROv2
class QA(dspy.Signature):
"""Answer the question concisely."""
question: str = dspy.InputField()
answer: str = dspy.OutputField()
program = dspy.ChainOfThought(QA)
def metric(example, pred, trace=None):
return example.answer.lower() in pred.answer.lower()
# Cheap baseline: bootstrap a few demos from a teacher
compiled = BootstrapFewShot(metric=metric, max_bootstrapped_demos=4).compile(
program, trainset=trainset
)
# Broader sweep: random search over candidate demo sets
compiled = BootstrapFewShotWithRandomSearch(metric=metric, num_candidate_programs=10).compile(
program, trainset=trainset, valset=valset
)
# Instruction tuning: coordinate ascent
compiled = COPRO(metric=metric).compile(program, trainset=trainset, eval_kwargs={})
# Joint search: Bayesian over instructions and demos
compiled = MIPROv2(metric=metric, auto='medium').compile(
program, trainset=trainset, valset=valset
)| Optimizer | What it edits | Search strategy | Cost |
|---|---|---|---|
| BootstrapFewShot | Few-shot demos | Greedy bootstrap from teacher | Low |
| BootstrapFewShotWithRandomSearch | Few-shot demos | Random search over candidate sets | Low to medium |
| COPRO | Instruction string | Coordinate ascent | Medium |
| MIPROv2 | Instructions plus demos | Bayesian optimization | Medium to high |
| BootstrapFinetune | Model weights | Gradient updates on bootstrapped traces | High plus GPU time |
Real products, models, and research that use this idea.
- Stanford's STORM Wikipedia-generation system uses DSPy with MIPROv2 to tune its multi-stage pipeline
- The DSPy documentation's benchmark notebooks consistently rank MIPROv2 above plain BootstrapFewShot on HotpotQA and MATH
- Teams using Claude Opus 4.7 as the teacher and Sonnet 4.6 as the student see meaningful BootstrapFewShot gains
- BootstrapFinetune is most commonly applied with open-weights models like Llama 4 Maverick where fine-tuning infra is in-house
What an interviewer would ask next. Try answering before peeking at the approach.
QWhen does using a stronger teacher than student matter most in BootstrapFewShot?
QHow does MIPROv2's Bayesian search differ from random search in practice?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Conflating 'optimizer' in DSPy with 'optimizer' in deep learning. DSPy optimizers search over prompts and demos, not gradient updates (except BootstrapFinetune).
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.