Zenaique

How does a DSPy signature differ from a prompt template?

Flashcard·Medium·4.0 · 0·~30s·Asked atDoordashMercorStability Ai
Attempt it
TL;DR

A DSPy signature declares input and output fields with semantic descriptions; the compiler turns it into an optimised prompt, while a template is the literal prompt you wrote.

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

Imagine comparing a recipe to a meal order. A meal order at a great restaurant says 'I want a light starter and a hearty main, no dairy.' The kitchen translates that intent into actual dishes that change with the season and the cook's experiments. A recipe spells out 'mix 200g flour with 100ml milk, bake 30 minutes'. Change anything and the recipe is no longer the recipe. A DSPy signature is the meal order: you say what you want as input and output. A prompt template is the recipe: you spell out the exact words. The kitchen, the DSPy compiler, does the cooking, and it can try new dishes against your taste test.

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.

A prompt template is a string with placeholders. A DSPy signature is a class with fields and descriptions. Read in isolation, the two look like notation choices for the same idea. They are not.

The template is the prompt the model sees. The signature is a description the compiler consumes. Between the signature and the model sits the DSPy compile loop: a module that selects a reasoning structure, an optimiser that searches over demonstrations and prompt rewrites, and a metric that turns 'better' into a scalar. The developer never authors the final prompt.

This architectural difference is why DSPy is not 'another LangChain.' The mental model is closer to PyTorch. You declare a program, choose modules, and optimise against a loss.

Anatomy of a signature

A signature has a docstring, a set of InputFields, and a set of OutputFields. Each field has a name and an optional desc that the compiler uses as a hint.

code
class ExtractCompany(dspy.Signature):
    """Extract the company name from a news headline."""
    headline = dspy.InputField()
    company  = dspy.OutputField(desc="company name or 'none'")

The docstring becomes part of the compiled prompt's instruction. The field names become labels. The desc strings become parenthetical hints to the model. None of this is the final prompt. It is the contract from which the compiler synthesises one.

Modules. The reasoning structure
Optimisers. The compile step
Why this changes the model-upgrade story
Where DSPy is the wrong tool
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.
python
import dspy

# 1) Signature: declare the contract
class GenerateAnswer(dspy.Signature):
    """Answer a factual question concisely."""
    question = dspy.InputField()
    answer   = dspy.OutputField(desc="a short factual answer")

# 2) Module: choose the reasoning structure
qa = dspy.ChainOfThought(GenerateAnswer)

# 3) Metric + small trainset feed an optimiser
def em_metric(example, pred, trace=None):
    return example.answer.strip().lower() == pred.answer.strip().lower()

from dspy.teleprompt import BootstrapFewShot
compiled_qa = BootstrapFewShot(metric=em_metric).compile(qa, trainset=trainset)

print(compiled_qa(question="Capital of France?").answer)
AspectDSPy signaturePrompt template
Who writes the promptDSPy compilerDeveloper
FormTyped input/output fields with descriptionsLiteral string with placeholders
OptimisationSearch over demonstrations and rewrites via metricHand-tuning by re-running and editing
Model-switch costRecompile against new modelRe-engineer prompt manually

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

  • STORM (Stanford) uses DSPy signatures plus optimisers to compile multi-stage research-report generation
  • Several Anthropic and OpenAI internal eval pipelines adopt DSPy patterns to make extraction prompts portable across model versions
Sign in to see more production examples.

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

QHow do BootstrapFewShot and MIPROv2 differ as optimisers?
A

Contrast bootstrap demonstration synthesis from a teacher run with the Bayesian-style instruction plus demonstration search that MIPROv2 performs, and discuss when each is appropriate.

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

Reading a signature as 'just a typed prompt template' and missing that the compiler is the whole point. The developer never writes the final prompt that the model sees.

Sign in to see all red flags and common mistakes.

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

  • What an InputField and an OutputField each declare

  • Three DSPy modules and what each adds (Predict, ChainOfThought, ReAct)

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