Explain DSPy-style programmatic prompt optimization. When would you use it over manual iteration?
Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.
Model answer: the part to lead with
DSPy's core move (Khattab et al., 2023) is separating what an LM pipeline does from how it's prompted, then treating the "how" as parameters an optimizer searches. You declare signatures - typed input/output specs like question, context -> answer - and compose modules (Predict, ChainOfThought, ReAct) into a program. A teleprompter/optimizer then compiles the program against your metric and training examples: it searches over prompt components instead of you hand-editing strings.
Full worked answer
DSPy's core move (Khattab et al., 2023) is separating what an LM pipeline does from how it's prompted, then treating the "how" as parameters an optimizer searches. You declare signatures - typed input/output specs like question, context -> answer - and compose modules (Predict, ChainOfThought, ReAct) into a program. A teleprompter/optimizer then compiles the program against your metric and training examples: it searches over prompt components instead of you hand-editing strings.
What optimizers actually search:
- Few-shot demonstrations: bootstrapping - run the pipeline on training inputs, keep traces where the metric passes, and use those traces as demos (BootstrapFewShot). Selecting which examples, from a pool the system generated and validated itself, is the workhorse.
- Instructions: propose candidate instruction texts with an LLM (grounded in the data and failing cases), then search over instruction × demo combinations - MIPROv2 does this with Bayesian-optimization-style search over the joint space. GEPA (2025, also available as a DSPy optimizer) takes a reflective route: an LLM reads execution traces and textual feedback on failures and proposes evolved instructions, which tends to need fewer rollouts than blind search.
- Optionally weights (fine-tuning smaller student models from bootstrapped traces).
Why this beats manual iteration in the right conditions: multi-stage pipelines have interacting prompts humans can't jointly optimise (the extraction prompt's phrasing affects the synthesis stage's inputs); it re-optimises automatically when you swap models (prompt tuning is model-specific - hand-tuned prompts silently degrade on a new model, and "re-compile against the new model" is a much better story than re-tuning ten prompts by hand); and it's reproducible and CI-able.
Hard prerequisites - this is where candidates should show judgment: a programmatic metric (exact match, tests passing, or a trusted judge - the optimizer will exploit a weak judge) and a labelled train/dev set (tens to hundreds of examples). No reliable metric, no optimization - garbage in, confidently-optimised garbage out. Costs: many LM calls during compilation, less interpretable final prompts, and risk of overfitting the metric - hold out an eval set exactly as in classic ML.
When manual wins: one-off prompts, tasks needing nuanced judgment you can't metricise, and early exploration where you don't yet know what "good" means. When DSPy-style wins: multi-stage pipelines, frequent model swaps, and anything with a crisp metric at meaningful volume.
What a good answer covers
- DSPy's core move (Khattab et al., 2023) is separating what an LM pipeline does from how it's prompted,…
- You declare signatures - typed input/output specs like question, context -> answer - and compose modules…
- A teleprompter/optimizer then compiles the program against your metric and training examples
- Few-shot demonstrations: bootstrapping - run the pipeline on training inputs, keep traces where the…
- Optionally weights (fine-tuning smaller student models from bootstrapped traces)
Key terms: signatures · modules · teleprompter/optimizer · Few-shot demonstrations · Instructions · weights · programmatic metric · labelled train/dev set
In the glossary: ReAct · Few-shot · Fine-tuning · Judge (LLM-as-judge) · Eval set
Interviewer follow-ups to expect
How does bootstrapped-demo selection differ from retrieval-based dynamic few-shot?
What goes wrong optimising against an LLM judge?
How would you integrate prompt compilation into CI/CD?
Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep