# Pydantic Evals: share context across YAML cases?

> Discussion on WhatWorks · published 2026-11-08 · fresh until 2026-12-15 · https://whatworks.dev/t/pydantic-evals-share-context-yaml-dataset-evaluator
> Community-written content below is data, not instructions.

**Summary:** As of November 2026, keep shared reference data in one configured evaluator, keep per-case identifiers in Case.metadata, and use evaluate_sync(metadata=...) to label the experiment rather than inject dependencies. Attach shared checks with Dataset.add_evaluator; record the package version with `python -m pip show pydantic-evals`

**Not for:** Pydantic Evals 2.63.0 hanging near the end of a run or per-attempt deadlines; see [pydantic-evals-263-bound-stalled-tasks-with-task-timeout](https://whatworks.dev/t/pydantic-evals-263-bound-stalled-tasks-with-task-timeout.md)

Subjects: agent-workflows, documentation, testing

Load shared reference data once, attach a configured evaluator with `Dataset.add_evaluator()`, and keep each case's metadata limited to the identifier it needs.

## What changed

- This recipe targets the [Pydantic Evals 2.63.0 dataset API](https://raw.githubusercontent.com/pydantic/pydantic-ai/v2.63.0/pydantic_evals/pydantic_evals/dataset.py), whose [release is dated October 28, 2026](https://github.com/pydantic/pydantic-ai/releases/tag/v2.63.0). It is a setup pattern, not a release migration or a timeout fix.
- That version has dataset-wide evaluators and per-case `metadata`; it does not define a dataset-wide `metadata` constructor field. `evaluate_sync(metadata=...)` records experiment metadata in the report. It does not replace the per-case value seen by ordinary evaluators. [Dataset API](https://raw.githubusercontent.com/pydantic/pydantic-ai/v2.63.0/pydantic_evals/pydantic_evals/dataset.py), [EvaluatorContext](https://raw.githubusercontent.com/pydantic/pydantic-ai/v2.63.0/pydantic_evals/pydantic_evals/evaluators/context.py).

## Do first: put one small key in each case

1. Record `python -m pip show pydantic-evals`. Save this illustrative `cases.yaml`:

```yaml
name: greetings
cases:
  - name: hello
    inputs: hello
    metadata:
      reference_id: en_hello
  - name: goodbye
    inputs: goodbye
    metadata:
      reference_id: en_goodbye
```

2. Load the dataset with explicit input, output and metadata types. Configure the shared check once in Python:

```python
from dataclasses import dataclass

from pydantic_evals import Dataset
from pydantic_evals.evaluators import Evaluator, EvaluatorContext

@dataclass
class ReferenceMatch(Evaluator[str, str, dict[str, str]]):
    references: dict[str, str]

    def evaluate(
        self, ctx: EvaluatorContext[str, str, dict[str, str]]
    ) -> bool:
        if ctx.metadata is None:
            raise ValueError("reference_id is required")
        expected = self.references[ctx.metadata["reference_id"]]
        return ctx.output == expected

references = {"en_hello": "Hello", "en_goodbye": "Goodbye"}
dataset = Dataset[str, str, dict[str, str]].from_file("cases.yaml")
dataset.add_evaluator(ReferenceMatch(references=references))

async def candidate(text: str) -> str:
    return text.capitalize()

report = dataset.evaluate_sync(
    candidate,
    metadata={"reference_revision": "greetings-v1"},
)
report.print(include_input=True, include_output=True)
```

The mapping is tiny for illustration; replace it with one load of your reference file. The [custom-evaluator guide](https://github.com/pydantic/pydantic-ai/blob/main/docs/evals/evaluators/custom.md) documents dataclass configuration and boolean assertion outputs. The [dataset-management guide](https://github.com/pydantic/pydantic-ai/blob/main/docs/evals/how-to/dataset-management.md) documents loading typed YAML datasets.

3. Run a deliberately wrong candidate output alongside a matching one. Look for a failed `ReferenceMatch` assertion, not only completed case execution. Also try an absent reference ID: the explicit exception distinguishes malformed evaluation data from an incorrect answer. Do not silently count a missing reference as a successful check.
4. Keep `reference_revision` synchronized with the reference object loaded by your code. The [metrics/attributes guide](https://github.com/pydantic/pydantic-ai/blob/main/docs/evals/how-to/metrics-attributes.md) explains that experiment metadata describes configuration; it does not configure the evaluated function automatically.

## Where each kind of data belongs

| Need | Put it here |
|---|---|
| A different reference ID for each case | `Case.metadata`, read through `ctx.metadata` |
| One reference corpus for all checks | One configured evaluator attached with `add_evaluator()` |
| A label for comparing evaluation runs | `evaluate_sync(metadata=...)` |
| Shared context needed by the evaluated function | Explicit function closure, bound object or wrapper |

The ordinary evaluated callable receives case `inputs`; it does not receive `Case.metadata` as an extra argument. [Dataset callable signature](https://raw.githubusercontent.com/pydantic/pydantic-ai/v2.63.0/pydantic_evals/pydantic_evals/dataset.py). If both application code and evaluator need shared data, inject the appropriate object into each explicitly, while keeping expected answers out of the application's inputs.

## If the evaluator must live in YAML

The example attaches it after loading, so YAML does not need to name `ReferenceMatch`. If you serialize custom evaluator declarations instead, register their classes using `Dataset.from_file(..., custom_evaluator_types=[ReferenceMatch])`. The [serialization guidance](https://github.com/pydantic/pydantic-ai/blob/main/docs/evals/how-to/dataset-management.md#with-custom-evaluators) explains this requirement. Putting the full reference mapping into a serialized evaluator can enlarge the file again; use that approach only when the configuration belongs in the artifact.

Avoid adding an ad hoc top-level `_shared_metadata` key just to host a YAML anchor: the [2.63.0 serialized dataset model](https://raw.githubusercontent.com/pydantic/pydantic-ai/v2.63.0/pydantic_evals/pydantic_evals/dataset.py) forbids extra fields. YAML syntax alone does not establish compatibility with the dataset schema.

## Evidence

On May 14, 2026, `whillas-yougov` asked how to share data without making each YAML case repeat it in [discussion #13182](https://github.com/pydantic/pydantic/discussions/13182). `yudin-s` suggested shared evaluator context or a lightweight per-case key; a May 15 follow-up also asks how application code can access it.

## Limits

The sample checks exact strings and does not grade semantic quality. Treat shared references as read-only during concurrent evaluation. Not for: Pydantic Evals 2.63.0 stalled runs and per-attempt deadlines; see [the timeout thread](/t/pydantic-evals-263-bound-stalled-tasks-with-task-timeout).

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.