Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions dspy/primitives/example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,90 @@
class Example:
"""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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This section reads a bit strange to me, I suggest just deleting that. "standard data format" should be enough IMO

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have trimmed it down! let me know if i should remove it fully or not?

- Dictionary-like access patterns (item access, iteration, etc.)
- Flexible initialization from dictionaries, other `Example` instances, or keyword arguments
- Input/output field separation for training data
- Serialization support for saving/loading `Example` instances
- Immutable operations that return new `Example` instances

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"
```
"""

def __init__(self, base=None, **kwargs):
"""Initialize an Example instance.

Args:
base: Optional base data source. Can be:
- Another Example instance (copies its data)
- A dictionary (copies its key-value pairs)
- None (creates empty Example)
**kwargs: Additional key-value pairs to store in the Example.
"""
# Internal storage and other attributes
self._store = {}
self._demos = []
Expand Down