Defining Signatures and Modules
Signature syntax, ChainOfThought, ReAct, and custom DSPy modules.
Signatures Are Typed Contracts
A DSPy signature is a Python class that declares the inputs and outputs of a reasoning step. Think of it as a typed function contract for your LLM call.
The docstring becomes the task description. Field annotations tell DSPy what to produce. You never write the actual prompt text — DSPy derives it from this declaration.
Defining a Basic Signature
A minimal signature subclasses dspy.Signature and annotates fields as InputField or OutputField. The class docstring provides the task instruction.
import dspy
class QASignature(dspy.Signature):
"""Answer the question based on the given context."""
context: str = dspy.InputField(desc='Relevant background text')
question: str = dspy.InputField(desc='The question to answer')
answer: str = dspy.OutputField(desc='A concise answer')
# Inspect what DSPy sees
print(QASignature.instructions) # The docstring
print(list(QASignature.input_fields.keys())) # ['context', 'question']
print(list(QASignature.output_fields.keys())) # ['answer']All lessons in this course
- Introduction to DSPy Framework
- Defining Signatures and Modules
- Compiling and Optimizing Prompts
- Evaluating DSPy Pipelines