How does a DSPy signature differ from a prompt template?
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.
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.
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.
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.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
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)
| Aspect | DSPy signature | Prompt template |
|---|---|---|
| Who writes the prompt | DSPy compiler | Developer |
| Form | Typed input/output fields with descriptions | Literal string with placeholders |
| Optimisation | Search over demonstrations and rewrites via metric | Hand-tuning by re-running and editing |
| Model-switch cost | Recompile against new model | Re-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
What an interviewer would ask next. Try answering before peeking at the approach.
QHow do BootstrapFewShot and MIPROv2 differ as optimisers?
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.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
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.
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)
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.