A DSPy signature declares what a language-model operation receives and returns. Instead of writing a full prompt, specify the task’s input and output fields; DSPy uses the signature to construct a prompt. section 2.5
Choose the amount of detail the task needs:
- Use `question -> answer` for a simple operation. Put multiple fields on either side of the arrow, as in `question, context -> reasoning, answer`. Choose clear field names; join words with underscores. section 2.5
- Add types when they clarify inputs or the desired output, for example `message: str, labels: List[str] -> intent_label: str`. section 3.3.1
- For more guidance, subclass `dspy.Signature` and declare `dspy.InputField()` and `dspy.OutputField()` fields. A docstring or field description can supply instructions that DSPy incorporates into the prompt. section 3.3.1
Use a signature in a program
With an LM configured, `prog = dspy.Predict('question -> answer')` creates a program. Call `prog(question='What is the capital of France?')` to receive a Prediction with the specified output field. To change the prompting technique without changing the task specification, initialize `dspy.ChainOfThought` with the same signature. section 2.6 section 2.9
prog = dspy.Predict("poem, new_author -> rewritten_poem, explanation")
prediction = prog(poem=rime, new_author="Allen Ginsberg")
Style transfer shows why a signature can have multiple inputs and outputs: the program receives a poem and target author, then returns a rewritten poem and an explanation. section 2.5.5
A signature specifies *what* to produce; a module specifies *how* to ask the LM. That separation lets you change the LM, signature, or prompting technique independently. Class and string signatures are nearly equivalent, but a class docstring changes the generated prompt. section 2.9 section 3.3.1
