Skip to content

dspy.Example

dspy.Example(base=None, **kwargs)

Source code in dspy/primitives/example.py
def __init__(self, base=None, **kwargs):
    # Internal storage and other attributes
    self._store = {}
    self._demos = []
    self._input_keys = None

    # Initialize from a base Example if provided
    if base and isinstance(base, type(self)):
        self._store = base._store.copy()

    # Initialize from a dict if provided
    elif base and isinstance(base, dict):
        self._store = base.copy()

    # Update with provided kwargs
    self._store.update(kwargs)

Functions

copy(**kwargs)

Source code in dspy/primitives/example.py
def copy(self, **kwargs):
    return type(self)(base=self, **kwargs)

get(key, default=None)

Source code in dspy/primitives/example.py
def get(self, key, default=None):
    return self._store.get(key, default)

inputs()

Source code in dspy/primitives/example.py
def inputs(self):
    if self._input_keys is None:
        raise ValueError("Inputs have not been set for this example. Use `example.with_inputs()` to set them.")

    # return items that are in input_keys
    d = {key: self._store[key] for key in self._store if key in self._input_keys}
    # return type(self)(d)
    new_instance = type(self)(base=d)
    new_instance._input_keys = self._input_keys  # Preserve input_keys in new instance
    return new_instance

items(include_dspy=False)

Source code in dspy/primitives/example.py
def items(self, include_dspy=False):
    return [(k, v) for k, v in self._store.items() if not k.startswith("dspy_") or include_dspy]

keys(include_dspy=False)

Source code in dspy/primitives/example.py
def keys(self, include_dspy=False):
    return [k for k in self._store.keys() if not k.startswith("dspy_") or include_dspy]

labels()

Source code in dspy/primitives/example.py
def labels(self):
    # return items that are NOT in input_keys
    input_keys = self.inputs().keys()
    d = {key: self._store[key] for key in self._store if key not in input_keys}
    return type(self)(d)

toDict()

Source code in dspy/primitives/example.py
def toDict(self):
    return self._store.copy()

values(include_dspy=False)

Source code in dspy/primitives/example.py
def values(self, include_dspy=False):
    return [v for k, v in self._store.items() if not k.startswith("dspy_") or include_dspy]

with_inputs(*keys)

Source code in dspy/primitives/example.py
def with_inputs(self, *keys):
    copied = self.copy()
    copied._input_keys = set(keys)
    return copied

without(*keys)

Source code in dspy/primitives/example.py
def without(self, *keys):
    copied = self.copy()
    for key in keys:
        del copied[key]
    return copied