dspy.Prediction¶
dspy.Prediction(*args, **kwargs) ¶
Bases: Example
A prediction object that contains the output of a DSPy module.
Prediction inherits from Example.
To allow feedback-augmented scores, Prediction supports comparison operations (<, >, <=, >=) for Predictions with a score field. The comparison operations compare the ‘score’ values as floats. For equality comparison, Predictions are equal if their underlying data stores are equal (inherited from Example).
Arithmetic operations (+, /, etc.) are also supported for Predictions with a ‘score’ field, operating on the score value.
Source code in dspy/primitives/prediction.py
Methods:¶
copy(**kwargs) ¶
Return a shallow copy, optionally overriding fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs | Fields to add or override in the copy. | {} |
Examples:
>>> import dspy
>>> ex = dspy.Example(question="Why?", answer="Because.")
>>> ex.copy(answer="No reason.")
Example({'question': 'Why?', 'answer': 'No reason.'}) (input_keys=None)
Source code in dspy/primitives/example.py
from_completions(list_or_dict, signature=None) classmethod ¶
get(key, default=None) ¶
Return the value for key, or default if the field doesn’t exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key | Field name to look up. | required | |
default | Value to return when | None |
Examples:
>>> import dspy
>>> ex = dspy.Example(name="Alice")
>>> ex.get("name")
'Alice'
>>> ex.get("city", "Unknown")
'Unknown'
Source code in dspy/primitives/example.py
get_lm_usage() ¶
inputs() ¶
Return a new Example containing only the input fields.
Requires with_inputs to have been called first.
Raises:
| Type | Description |
|---|---|
ValueError | If |
Examples:
>>> import dspy
>>> ex = dspy.Example(question="Why?", answer="Because.").with_inputs("question")
>>> ex.inputs()
Example({'question': 'Why?'}) (input_keys={'question'})
Source code in dspy/primitives/example.py
items(include_dspy=False) ¶
Return (field_name, value) pairs, like dict.items().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_dspy | If | False |
Examples:
>>> import dspy
>>> dspy.Example(question="Why?", answer="Because.").items()
[('question', 'Why?'), ('answer', 'Because.')]
Source code in dspy/primitives/example.py
keys(include_dspy=False) ¶
Return field names, like dict.keys().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_dspy | If | False |
Examples:
Source code in dspy/primitives/example.py
labels() ¶
Return a new Example containing only the label (non-input) fields.
Requires with_inputs to have been called first, since labels are everything that is not an input.
Examples:
>>> import dspy
>>> ex = dspy.Example(question="Why?", answer="Because.").with_inputs("question")
>>> ex.labels()
Example({'answer': 'Because.'}) (input_keys=None)
Source code in dspy/primitives/example.py
set_lm_usage(value) ¶
toDict() ¶
Convert to a plain dictionary, recursively serializing nested objects.
Nested Example objects, Pydantic models, lists, and dicts are converted so the result is JSON-friendly.
Examples:
>>> import dspy
>>> dspy.Example(question="Why?", answer="Because.").toDict()
{'question': 'Why?', 'answer': 'Because.'}
Source code in dspy/primitives/example.py
values(include_dspy=False) ¶
Return field values, like dict.values().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_dspy | If | False |
Examples:
Source code in dspy/primitives/example.py
with_inputs(*keys) ¶
Mark which fields are inputs and return a new Example.
Fields not listed here are treated as labels (expected outputs). DSPy optimizers and evaluators use this split: they pass example.inputs() to your program and compare the output against example.labels().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys | Names of the input fields. | () |
Returns:
| Type | Description |
|---|---|
| A copy of this |
Examples:
>>> import dspy
>>> ex = dspy.Example(question="Why?", answer="Because.").with_inputs("question")
>>> ex.inputs().keys()
['question']
>>> ex.labels().keys()
['answer']
Source code in dspy/primitives/example.py
without(*keys) ¶
Return a copy with the specified fields removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*keys | Field names to drop. | () |
Examples:
>>> import dspy
>>> ex = dspy.Example(question="Why?", answer="Because.", source="web")
>>> ex.without("source")
Example({'question': 'Why?', 'answer': 'Because.'}) (input_keys=None)