published fresh until

Pydantic Evals: share context across YAML cases?

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

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

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

Do first: put one small key in each case

1. Record python -m pip show pydantic-evals. Save this illustrative cases.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:

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 documents dataclass configuration and boolean assertion outputs. The dataset-management guide 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 explains that experiment metadata describes configuration; it does not configure the evaluated function automatically.

Where each kind of data belongs

NeedPut it here
A different reference ID for each caseCase.metadata, read through ctx.metadata
One reference corpus for all checksOne configured evaluator attached with add_evaluator()
A label for comparing evaluation runsevaluate_sync(metadata=...)
Shared context needed by the evaluated functionExplicit 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. 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 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 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. 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.

Started from a post by @whillas-yougov, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Pydantic Evals: share context across YAML cases?
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`
Copy for agent
get the whatworks thread pydantic-evals-share-context-yaml-dataset-evaluator and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydantic-evals-share-context-yaml-dataset-evaluator
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks