Dspy.Example (DSPex v0.11.0)

Copy Markdown View Source

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

Summary

Functions

Python method Example.copy.

Python method Example.get.

Python method Example.inputs.

Python method Example.items.

Python method Example.keys.

Python method Example.labels.

Initialize an Example instance.

Python method Example.toDict.

Python method Example.values.

Python method Example.with_inputs.

Python method Example.without.

Types

t()

@opaque t()

Functions

copy(ref, opts \\ [])

@spec copy(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.copy.

Parameters

  • kwargs (term())

Returns

  • term()

get(ref, key, args, opts \\ [])

@spec get(SnakeBridge.Ref.t(), term(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.get.

Parameters

  • key (term())
  • default (term() default: None)

Returns

  • term()

inputs(ref, opts \\ [])

@spec inputs(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.inputs.

Returns

  • term()

items(ref, args, opts \\ [])

@spec items(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.items.

Parameters

  • include_dspy (term() default: False)

Returns

  • term()

keys(ref, args, opts \\ [])

@spec keys(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.keys.

Parameters

  • include_dspy (term() default: False)

Returns

  • term()

labels(ref, opts \\ [])

@spec labels(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.labels.

Returns

  • term()

new(args, opts \\ [])

@spec new(
  [term()],
  keyword()
) :: {:ok, SnakeBridge.Ref.t()} | {:error, Snakepit.Error.t()}

Initialize an Example instance.

Parameters

  • 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.

to_dict(ref, opts \\ [])

@spec to_dict(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.toDict.

Returns

  • term()

values(ref, args, opts \\ [])

@spec values(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.values.

Parameters

  • include_dspy (term() default: False)

Returns

  • term()

with_inputs(ref, args, opts \\ [])

@spec with_inputs(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.with_inputs.

Parameters

  • keys (term())

Returns

  • term()

without(ref, args, opts \\ [])

@spec without(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method Example.without.

Parameters

  • keys (term())

Returns

  • term()