liveBook · Manning

Signature

As explained in Building LLM Applications with DSPy

A declarative specification of the inputs and outputs of a DSPy language-model operation.

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

How Building LLM Applications with DSPy covers it

How deep it goes
Explains string syntax, typed fields, class declarations, and docstrings; defers prompt optimization.
Examples
Uses question answering, intent classification with candidate labels, and poem style transfer with an explanation.
What it assumes
Assumes basic Python declarations while introducing signature notation and DSPy input and output fields.
What you can do after
Build it from scratch as a DSPy string or dspy.Signature subclass, then pass it to a module.
How it connects
Connects signatures to language models, prompts, modules, Predict, ChainOfThought, and Predictions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.