API Reference: Signatures
Signatures define the inputs and outputs for modules. They provide type safety and clear contracts for LLM interactions.
Creating Signatures
Class-based Signatures
The traditional way to define signatures using Python classes:
from udspy import Signature, InputField, OutputField
class QA(Signature):
"""Answer questions concisely."""
question: str = InputField()
answer: str = OutputField()
String Signatures (DSPy-style)
For quick prototyping, use the string format "inputs -> outputs":
from udspy import Signature
# Simple signature
QA = Signature.from_string("question -> answer")
# Multiple inputs and outputs
Analyze = Signature.from_string(
"context, question -> summary, answer",
"Analyze text and answer questions"
)
Format: "input1, input2 -> output1, output2"
- Inputs and outputs are comma-separated
- Whitespace is trimmed automatically
- All fields default to
strtype - Optional second argument for instructions
Direct Module Usage
All modules automatically recognize string signatures:
from udspy import Predict, ChainOfThought
# These are equivalent
predictor1 = Predict("question -> answer")
predictor2 = Predict(Signature.from_string("question -> answer"))
When to Use Each Format
Use string signatures (from_string) when:
- Prototyping quickly
- All fields are strings
- You don't need field descriptions
- The signature is simple
Use class-based signatures when: - You need custom types (int, list, custom Pydantic models) - You want field descriptions for better LLM guidance - The signature is complex - You want IDE autocomplete and type checking
Examples
Basic String Signature
from udspy import Predict
predictor = Predict("question -> answer")
result = predictor(question="What is Python?")
print(result.answer)
Multiple Fields
from udspy import ChainOfThought
cot = ChainOfThought("context, question -> summary, answer")
result = cot(
context="Python is a programming language",
question="What is Python?"
)
print(result.reasoning)
print(result.summary)
print(result.answer)
With Instructions
QA = Signature.from_string(
"question -> answer",
"Answer questions concisely and accurately"
)
predictor = Predict(QA)
Comparison
# String format - quick and simple
QA_String = Signature.from_string("question -> answer")
# Class format - more control
class QA_Class(Signature):
"""Answer questions."""
question: str = InputField(description="Question to answer")
answer: str = OutputField(description="Concise answer")
API Reference
udspy.signature
Signature definitions for structured LLM inputs and outputs.
Classes
Signature
Bases: BaseModel
Base class for defining LLM task signatures.
A Signature specifies the input and output fields for an LLM task, along with an optional instruction describing the task.
Example
Source code in src/udspy/signature.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
Functions
from_string(spec, instructions='')
classmethod
Create a Signature from DSPy-style string format.
This is a convenience method for creating simple signatures using
the DSPy string format "input1, input2 -> output1, output2".
All fields default to type str.
For more control over field types, descriptions, and defaults,
use the class-based Signature definition or make_signature().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
str
|
Signature specification in format "inputs -> outputs" Examples: "question -> answer" "context, question -> answer" "text -> summary, keywords" |
required |
instructions
|
str
|
Optional task instructions (docstring) |
''
|
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new Signature class with all fields as type |
Raises:
| Type | Description |
|---|---|
ValueError
|
If spec is not in valid format |
Example
Note
This is equivalent to DSPy's string-based signature creation.
All fields default to str type. For custom types, use the
class-based approach with InputField() and OutputField().
Source code in src/udspy/signature.py
get_input_fields()
classmethod
Get all input fields defined in this signature.
Source code in src/udspy/signature.py
get_instructions()
classmethod
get_output_fields()
classmethod
Get all output fields defined in this signature.
Source code in src/udspy/signature.py
SignatureMeta
Bases: type(BaseModel)
Metaclass for Signature that validates field types.
Source code in src/udspy/signature.py
Functions
InputField(default=..., *, description=None, **kwargs)
Define an input field for a Signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value for the field |
...
|
description
|
str | None
|
Human-readable description of the field's purpose |
None
|
**kwargs
|
Any
|
Additional Pydantic field arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
A Pydantic FieldInfo with input metadata |
Source code in src/udspy/signature.py
OutputField(default=..., *, description=None, **kwargs)
Define an output field for a Signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default
|
Any
|
Default value for the field |
...
|
description
|
str | None
|
Human-readable description of the field's purpose |
None
|
**kwargs
|
Any
|
Additional Pydantic field arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
A Pydantic FieldInfo with output metadata |
Source code in src/udspy/signature.py
make_signature(input_fields, output_fields, instructions='')
Dynamically create a Signature class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_fields
|
dict[str, type]
|
Dictionary mapping field names to types for inputs |
required |
output_fields
|
dict[str, type]
|
Dictionary mapping field names to types for outputs |
required |
instructions
|
str
|
Task instructions |
''
|
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new Signature class |