Skip to content

Commit a7107c7

Browse files
committed
simplify docs
1 parent 49961b9 commit a7107c7

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

docs/docs/learn/programming/signatures.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ Your signatures can also have multiple input/output fields with types:
3232

3333
**Tip:** For fields, any valid variable names work! Field names should be semantically meaningful, but start simple and don't prematurely optimize keywords! Leave that kind of hacking to the DSPy compiler. For example, for summarization, it's probably fine to say `"document -> summary"`, `"text -> gist"`, or `"long_context -> tldr"`.
3434

35+
You can also add instructions to your inline signature, which can use variables at runtime. Use the `instructions` keyword argument to add instructions to your signature.
36+
37+
```python
38+
toxicity = dspy.Predict(
39+
'comment -> toxic: bool',
40+
instructions="Mark as 'toxic' if the comment includes insults, harassment, or sarcastic derogatory remarks.")
41+
```
3542

3643
### Example A: Sentiment Classification
3744

@@ -162,10 +169,10 @@ Prediction(
162169
DSPy signatures support various annotation types:
163170

164171
1. **Basic types** like `str`, `int`, `bool`
165-
2. **Typing module types** like `List[str]`, `Dict[str, int]`, `Optional[float]`
172+
2. **Typing module types** like `List[str]`, `Dict[str, int]`, `Optional[float]`. `Union[str, int]`
166173
3. **Custom types** defined in your code
167174
4. **Dot notation** for nested types with proper configuration
168-
5. **Special data types** like `dspy.Image`
175+
5. **Special data types** like `dspy.Image, dspy.History`
169176

170177
### Working with Custom Types
171178

@@ -175,26 +182,15 @@ class QueryResult(pydantic.BaseModel):
175182
text: str
176183
score: float
177184

178-
# Automatic resolution - works in the same scope
179185
signature = dspy.Signature("query: str -> result: QueryResult")
180186

181-
# Nested types with dot notation
182187
class Container:
183-
class NestedType(pydantic.BaseModel):
184-
value: str
185-
186-
# Approach 1: Type aliases (recommended)
187-
NestedResult = Container.NestedType
188-
signature = dspy.Signature("query: str -> result: NestedResult")
189-
190-
# Approach 2: Explicit custom_types parameter
191-
signature = dspy.Signature(
192-
"query: str -> result: Container.NestedType",
193-
custom_types={
194-
"Container": Container,
195-
"Container.NestedType": Container.NestedType
196-
}
197-
)
188+
class Query(pydantic.BaseModel):
189+
text: str
190+
class Score(pydantic.BaseModel):
191+
score: float
192+
193+
signature = dspy.Signature("query: Container.Query -> score: Container.Score")
198194
```
199195

200196
## Using signatures to build modules & compiling them

tests/signatures/test_custom_types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ class NestedType(pydantic.BaseModel):
3434
alias_sig = Signature("input: str -> output: NestedType")
3535
assert alias_sig.output_fields["output"].annotation == Container.NestedType
3636

37+
class Container2:
38+
class Query(pydantic.BaseModel):
39+
text: str
40+
class Score(pydantic.BaseModel):
41+
score: float
42+
43+
signature = dspy.Signature("query: Container2.Query -> score: Container2.Score")
44+
assert signature.output_fields["score"].annotation == Container2.Score
45+
3746

3847
class GlobalCustomType(pydantic.BaseModel):
3948
"""A type defined at module level for testing module-level resolution."""
@@ -83,6 +92,7 @@ class LocalType(pydantic.BaseModel):
8392
assert sig5.output_fields["output"].annotation == OuterContainer.InnerType
8493

8594
def test_expected_failure():
95+
# InnerType DNE when not OuterContainer.InnerTypes, so this type shouldnt be resolved
8696
with pytest.raises(ValueError):
8797
sig4 = Signature("input: str -> output: InnerType")
8898
assert sig4.output_fields["output"].annotation == InnerType

0 commit comments

Comments
 (0)