How does a DSPy signature differ from a prompt template?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
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.
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.
7 min: signature versus template, the compiler step, modules, optimisers, metric-driven adaptation across model upgrades, when DSPy is wrong.
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.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes 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.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.