Skip to content

Commit bbcfe08

Browse files
[docs]Add Google-Style comprehensive docstring to dspy/primitives/example Example class (#8949)
* Add comprehensive docstring to Example class - Add detailed class-level docstring following Google Python style guide - Include comprehensive usage examples showing various initialization patterns - Document key features: dictionary-like access, input/output separation, serialization - Cover all major use cases: basic usage, dictionary initialization, copying, input/output handling - Provide practical examples for common operations like iteration, key checking, and field access This improves DSPy's docstring coverage and makes the Example class more accessible to users. * Fixes made according to the comments and suggestions * lint --------- Co-authored-by: chenmoneygithub <[email protected]>
1 parent d7a4c7c commit bbcfe08

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

dspy/primitives/example.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,90 @@
11
class Example:
2+
"""A flexible data container for DSPy examples and training data.
3+
4+
The `Example` class is the standard data format used in DSPy evaluation and optimization.
5+
6+
Key features:
7+
- Dictionary-like access patterns (item access, iteration, etc.)
8+
- Flexible initialization from dictionaries, other `Example` instances, or keyword arguments
9+
- Input/output field separation for training data
10+
- Serialization support for saving/loading `Example` instances
11+
- Immutable operations that return new `Example` instances
12+
13+
Examples:
14+
15+
Basic usage with keyword arguments:
16+
17+
```python
18+
import dspy
19+
20+
# Create an example with input and output fields
21+
example = dspy.Example(
22+
question="What is the capital of France?",
23+
answer="Paris",
24+
)
25+
print(example.question) # "What is the capital of France?"
26+
print(example.answer) # "Paris"
27+
```
28+
29+
Initialize from a dictionary:
30+
31+
```python
32+
data = {"question": "What is 2+2?", "answer": "4"}
33+
example = dspy.Example(data)
34+
print(example["question"]) # "What is 2+2?"
35+
```
36+
37+
Copy from another Example:
38+
39+
```python
40+
original = dspy.Example(question="Hello", answer="World")
41+
copy = dspy.Example(original)
42+
print(copy.question) # "Hello"
43+
```
44+
45+
Working with input/output separation:
46+
47+
```python
48+
# Mark which fields are inputs for training
49+
example = dspy.Example(
50+
question="What is the weather?",
51+
answer="It's sunny",
52+
).with_inputs("question")
53+
54+
# Get only input fields
55+
inputs = example.inputs()
56+
print(inputs.question) # "What is the weather?"
57+
58+
# Get only output fields (labels)
59+
labels = example.labels()
60+
print(labels.answer) # "It's sunny"
61+
```
62+
63+
Dictionary-like operations:
64+
65+
```python
66+
example = dspy.Example(name="Alice", age=30)
67+
68+
# Check if key exists
69+
if "name" in example:
70+
print("Name field exists")
71+
72+
# Get with default value
73+
city = example.get("city", "Unknown")
74+
print(city) # "Unknown"
75+
```
76+
"""
77+
278
def __init__(self, base=None, **kwargs):
79+
"""Initialize an Example instance.
80+
81+
Args:
82+
base: Optional base data source. Can be:
83+
- Another Example instance (copies its data)
84+
- A dictionary (copies its key-value pairs)
85+
- None (creates empty Example)
86+
**kwargs: Additional key-value pairs to store in the Example.
87+
"""
388
# Internal storage and other attributes
489
self._store = {}
590
self._demos = []

0 commit comments

Comments
 (0)