Most LLM call sites start as a hand-built string with the input variables spliced in and a sentence at the end describing the output format. It works until someone edits the string. Change a word, reorder a sentence or add an example, and the output format can shift in ways nothing in the codebase catches. What the function is supposed to produce is described only in prose, so nothing checks it until a downstream parser throws or silently reads the wrong field.
Declaring the contract
DSPy uses a signature: a class that declares typed input and output fields, with the framework responsible for turning that declaration into an actual prompt at call time. Here’s a real one, from a research agent’s planning step (src/agents/planner.py):
class PlannerSignature(dspy.Signature):
"""Break down the user's technical research topic into 3 specific web search queries.
Target documentation, architecture patterns, and tutorials."""
topic: str = dspy.InputField()
queries: list[str] = dspy.OutputField(
desc="Exactly 3 highly specific web search queries"
)
There’s no prompt here, only a task description (the docstring), a typed input, and a typed output with a description of what should be in it. The module that runs it is two lines:
_planner_module = dspy.ChainOfThought(PlannerSignature)
result = planner(topic=topic)
ChainOfThought is the prompting strategy: it takes the signature and constructs a prompt that asks the model to reason step by step before producing queries. dspy.Predict(PlannerSignature) would build a plainer, non-reasoning prompt from the same signature. Neither the caller (plan_node) nor the signature itself changes when you swap between them; result.queries is still a list[str] either way.
Typed outputs help more when the output isn’t just text, as in the same project’s critic step:
class CriticSignature(dspy.Signature):
"""Evaluate the draft report against the raw sources.
Check for hallucinations and comprehensiveness."""
topic: str = dspy.InputField()
raw_sources: str = dspy.InputField()
draft_report: str = dspy.InputField()
score: int = dspy.OutputField(desc="Score from 1 to 10")
is_hallucinated: bool = dspy.OutputField(
desc="True if the report contains facts not in the sources"
)
feedback: str = dspy.OutputField(
desc="Specific revision feedback, empty string if score >= 7"
)
The caller doesn’t regex a score out of a sentence or hope the model formatted a JSON blob correctly. It reads result.score as an int and result.is_hallucinated as a bool, and routes on them directly:
if result.score < 7 or result.is_hallucinated:
return {"critic_feedback": result.feedback, "revision_count": revision_count + 1, ...}
What it guarantees
A typed signature guarantees the shape of the output. It says nothing about whether the content is correct. result.score will be an int; DSPy’s output parsing enforces that before your code ever sees it. Whether that int is a good score, whether the critic actually caught the hallucination it was supposed to catch, is a separate question a type system has no opinion on. Swapping dspy.Predict for dspy.ChainOfThought, or later swapping in an optimizer that rewrites the underlying prompt to improve accuracy on a training set, changes how the answer gets produced without changing the contract the rest of the graph depends on. So the prompting strategy is a decision you can revisit in one place, instead of a string to hunt down and re-edit at every call site that builds a similar prompt by hand.
It doesn’t replace the work of writing a good task description, choosing informative field names, or providing the field-level desc hints that steer the model toward what you actually mean by “score” or “hallucinated.” That is still prompt engineering, written as structured metadata instead of prose. A badly specified signature performs as badly as a badly written prompt, but you can point at the underspecified field instead of rereading a whole prompt string for the ambiguous sentence.
What it does rule out is a prompt edit that quietly changes the output format until a downstream parser breaks in production. A typed output field either parses to the declared type or the call fails with an error. It’s a small guarantee, and raw prompt strings don’t give it to you.