# `Dspy.Example`
[🔗](https://github.com/nshkrdotcom/dspex/blob/v0.11.0/lib/snakebridge_generated/dspy/example.ex#L7)

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

# `t`

```elixir
@opaque t()
```

# `copy`

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

Python method `Example.copy`.

## Parameters

- `kwargs` (term())

## Returns

- `term()`

# `get`

```elixir
@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`

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

Python method `Example.inputs`.

## Returns

- `term()`

# `items`

```elixir
@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`

```elixir
@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`

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

Python method `Example.labels`.

## Returns

- `term()`

# `new`

```elixir
@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`

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

Python method `Example.toDict`.

## Returns

- `term()`

# `values`

```elixir
@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`

```elixir
@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`

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

Python method `Example.without`.

## Parameters

- `keys` (term())

## Returns

- `term()`

---

*Consult [api-reference.md](api-reference.md) for complete listing*
