dspy.Example¶
dspy.Example(base=None, **kwargs)
¶
A flexible data container for DSPy examples and training data.
The Example class is the standard data format used in DSPy evaluation and optimization.
Key features
- Dictionary-like access patterns (item access, iteration, etc.)
- Flexible initialization from dictionaries, other
Exampleinstances, or keyword arguments - Input/output field separation for training data
- Serialization support for saving/loading
Exampleinstances - Immutable operations that return new
Exampleinstances
Examples:
Basic usage with keyword arguments:
```python
import dspy
# Create an example with input and output fields
example = dspy.Example(
question="What is the capital of France?",
answer="Paris",
)
print(example.question) # "What is the capital of France?"
print(example.answer) # "Paris"
```
Initialize from a dictionary:
```python
data = {"question": "What is 2+2?", "answer": "4"}
example = dspy.Example(data)
print(example["question"]) # "What is 2+2?"
```
Copy from another Example:
```python
original = dspy.Example(question="Hello", answer="World")
copy = dspy.Example(original)
print(copy.question) # "Hello"
```
Working with input/output separation:
```python
# Mark which fields are inputs for training
example = dspy.Example(
question="What is the weather?",
answer="It's sunny",
).with_inputs("question")
# Get only input fields
inputs = example.inputs()
print(inputs.question) # "What is the weather?"
# Get only output fields (labels)
labels = example.labels()
print(labels.answer) # "It's sunny"
```
Dictionary-like operations:
```python
example = dspy.Example(name="Alice", age=30)
# Check if key exists
if "name" in example:
print("Name field exists")
# Get with default value
city = example.get("city", "Unknown")
print(city) # "Unknown"
```
Initialize an Example instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
Optional base data source. Can be: - Another Example instance (copies its data) - A dictionary (copies its key-value pairs) - None (creates empty Example) |
None
|
|
**kwargs
|
Additional key-value pairs to store in the Example. |
{}
|
Source code in dspy/primitives/example.py
Functions¶
copy(**kwargs)
¶
get(key, default=None)
¶
inputs()
¶
Source code in dspy/primitives/example.py
items(include_dspy=False)
¶
keys(include_dspy=False)
¶
labels()
¶
toDict()
¶
Source code in dspy/primitives/example.py
values(include_dspy=False)
¶
with_inputs(*keys)
¶
:::