dspy.Signature¶
dspy.Signature
¶
Bases: BaseModel
Functions¶
append(name, field, type_=None) -> type[Signature]
classmethod
¶
Insert a field at the end of the inputs or outputs section.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Field name to add. |
required |
field
|
|
required | |
type_
|
type | None
|
Optional explicit type annotation. If |
None
|
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new Signature class with the field appended. |
Example
Source code in dspy/signatures/signature.py
delete(name) -> type[Signature]
classmethod
¶
Return a new Signature class without the given field.
If name is not present, the fields are unchanged (no error raised).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Field name to remove. |
required |
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new Signature class with the field removed (or unchanged if the field was absent). |
Example
import dspy
class MySig(dspy.Signature):
input_text: str = dspy.InputField(desc="Input sentence")
temp_field: str = dspy.InputField(desc="Temporary debug field")
output_text: str = dspy.OutputField(desc="Translated sentence")
NewSig = MySig.delete("temp_field")
print(list(NewSig.fields.keys()))
# No error is raised if the field is not present
Unchanged = NewSig.delete("nonexistent")
print(list(Unchanged.fields.keys()))
Source code in dspy/signatures/signature.py
dump_state()
classmethod
¶
Source code in dspy/signatures/signature.py
equals(other) -> bool
classmethod
¶
Compare the JSON schema of two Signature classes.
Source code in dspy/signatures/signature.py
insert(index: int, name: str, field, type_: type | None = None) -> type[Signature]
classmethod
¶
Insert a field at a specific position among inputs or outputs.
Negative indices are supported (e.g., -1 appends). If type_ is omitted, the field's
existing annotation is used; if that is missing, str is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Insertion position within the chosen section; negatives append. |
required |
name
|
str
|
Field name to add. |
required |
field
|
InputField or OutputField instance to insert. |
required | |
type_
|
type | None
|
Optional explicit type annotation. |
None
|
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new Signature class with the field inserted. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import dspy
class MySig(dspy.Signature):
input_text: str = dspy.InputField(desc="Input sentence")
output_text: str = dspy.OutputField(desc="Translated sentence")
NewSig = MySig.insert(0, "context", dspy.InputField(desc="Context for translation"))
print(list(NewSig.fields.keys()))
NewSig2 = NewSig.insert(-1, "confidence", dspy.OutputField(desc="Translation confidence"))
print(list(NewSig2.fields.keys()))
Source code in dspy/signatures/signature.py
load_state(state)
classmethod
¶
Source code in dspy/signatures/signature.py
prepend(name, field, type_=None) -> type[Signature]
classmethod
¶
Insert a field at index 0 of the inputs or outputs section.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Field name to add. |
required |
field
|
|
required | |
type_
|
type | None
|
Optional explicit type annotation. If |
None
|
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new |
Example
Source code in dspy/signatures/signature.py
with_instructions(instructions: str) -> type[Signature]
classmethod
¶
Return a new Signature class with identical fields and new instructions.
This method does not mutate cls. It constructs a fresh Signature
class using the current fields and the provided instructions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instructions
|
str
|
Instruction text to attach to the new signature. |
required |
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new Signature class whose fields match |
type[Signature]
|
and whose instructions equal |
Example
Source code in dspy/signatures/signature.py
with_updated_fields(name: str, type_: type | None = None, **kwargs: dict[str, Any]) -> type[Signature]
classmethod
¶
Create a new Signature class with the updated field information.
Returns a new Signature class with the field, name, updated with fields[name].json_schema_extra[key] = value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the field to update. |
required |
type_
|
type | None
|
The new type of the field. |
None
|
kwargs
|
dict[str, Any]
|
The new values for the field. |
{}
|
Returns:
| Type | Description |
|---|---|
type[Signature]
|
A new Signature class (not an instance) with the updated field information. |
Source code in dspy/signatures/signature.py
:::