Zenaique

How does DSPy program plus optimizer plus metric replace hand written prompts?

Flashcard·Easy·4.0 · 0·~30s·Asked atDataikuMckinseyPromptlayer
Attempt it
TL;DR

DSPy is a compiler for LLM programs: you declare a Signature plus Module plus Metric, and an Optimizer produces the prompt and few-shot demos, you never write the final prompt by hand.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Imagine you want to teach a recipe to a friend. The old way is to write the recipe out perfectly yourself and hope it lands. DSPy is the other way around: you describe what the dish should taste like (the metric), give your friend 100 examples of good and bad dishes (the trainset), and let them iterate until their recipe consistently scores well. You never wrote the recipe, you wrote the goal and provided the feedback. That is exactly how DSPy treats prompts, the framework writes them for you against a metric, instead of asking you to hand-craft each one.

Key concepts

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

DSPy is the most misunderstood framework in the 2026 LLM stack, and the misunderstanding is almost always the same: people read the docs and pattern-match it to LangChain because both are 'Python libraries that call LLMs.' The interview signal is whether you can articulate why DSPy is doing something fundamentally different.

The one-line frame: DSPy is a compiler for LLM programs. You write the function spec and the loss, an Optimizer writes the prompt. Every other detail flows from that.

Mental model: Signature is the model spec, Module is the nn.Module, Optimizer is the optimizer, Metric is the loss, trainset is the dataset, compile() is the training loop, the compiled program is the checkpoint.

The four primitives and the PyTorch analogy

Signature

A Signature declares the input and output fields of an LLM function, with short semantic descriptions:

python
class QA(dspy.Signature):
    """Answer questions from the given context."""
    context: str = dspy.InputField()
    question: str = dspy.InputField()
    answer: str = dspy.OutputField()

The Signature is what the prompt will eventually be compiled against. You commit to the function shape; the optimizer commits to the prompt text.

Module

A Module wraps the Signature into a callable LLM step. The common ones in 2026:

  • dspy.Predict(Signature). Direct call, no intermediate reasoning.
  • dspy.ChainOfThought(Signature). Adds a rationale field the LLM fills in before the output.
  • dspy.ReAct(Signature, tools=[...]). Tool-using agent loop.

Modules are composable, so a multi-step program is just Modules calling Modules.

Optimizer and Metric

The Optimizer is the analogue of torch.optim. Built-in options include BootstrapFewShot, MIPROv2, and COPRO. The metric is any Python callable returning a scalar (or boolean) that takes a prediction and a ground-truth example. The trainset is a list of dspy.Example objects.

compile()

compile() is the training loop. It runs the program on the trainset, scores each output with the metric, and searches the prompt and demonstration space until it finds a configuration whose metric score on the trainset (or a held-out split) is good. The result is a compiled program with frozen instructions and demonstrations.

Optimizers in 2026: what each one actually does
Serving a compiled program in production
Where DSPy fits in the 2026 framework landscape
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • Stanford STORM uses DSPy to compile multi-hop research agents from a draft-quality metric, the prompts are not hand-written.
  • DSPy's HotPotQA examples compile retrieval-augmented multi-hop QA programs to exact-match scores higher than hand-tuned baselines.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWalk me through how BootstrapFewShot actually generates demonstrations.
A

It runs the program over the trainset using a teacher Module (same LM, higher temperature, or a stronger LM), captures (input, intermediate, output) traces, and scores each output with your metric. Only traces that pass the metric become candidate demonstrations, then it selects a subset that maximizes coverage and accuracy on a held-out slice.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Treating DSPy as 'LangChain with pipes.' The compile loop is the whole point, without a metric and trainset DSPy is just an awkward way to call an LLM.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • The four DSPy primitives (Signature, Module, Optimizer, Metric) and the PyTorch analogy

  • What compile() actually does end to end

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Defend the call to…
Short answer·Hard