Preserve keyword-only evaluator arguments in `Dataset` files

Closed
DA
23d ago
+195-15

Closes #11268

A standard keyword-only dataclass evaluator can evaluate successfully and save to JSON/YAML, then fail to load because its configuration was shortened to a positional argument. Preserve its argument name and make the generated schema accept the resulting keyword mapping.

New public surface: none.

User-visible behavior: MinimumLength(minimum=4) previously saved as {'MinimumLength': 4} and failed on load; it now saves as {'MinimumLength': {'minimum': 4}} and round-trips successfully.

Verification: 42 targeted tests passed, including the existing complete built-in capability schema snapshot. Regression tests cover evaluator JSON/YAML persistence, report evaluators, keyword-only capability schemas, and arbitrary keyword metadata compatibility. Targeted Pyright, Ruff check/format, and git diff --check passed. No full test suite or repository-wide type check was run.

What changes for existing users: keyword-only evaluator configurations can be saved and loaded with matching schemas; valid positional shorthand and existing arbitrary-**kwargs schemas retain their behavior.

Maintainer setup: Please apply pkg:evals and request the fork review with douwebot; these labels require repository permissions.

Implementation, compatibility, and verification details

The issue's script was run against published pydantic-evals==2.61.0, source main at 715651ef7fd3994395e85070ba3d6096c5b211c9, and this implementation at e058052fc760a9469fb27306ed900561dfb955a9. Both affected versions saved JSON/YAML successfully and then raised an evaluator-loading ExceptionGroup whose nested error was:

ValueError: Failed to instantiate evaluator 'MinimumLength' for dataset: MinimumLength.__init__() takes 1 positional argument but 2 were given

With this implementation, the same script loads both files and checks the restored evaluators against the original objects. It makes no provider calls.

BaseEvaluator.build_serialization_arguments() omits default-valued fields. as_spec() then considers a single remaining first field eligible for positional shorthand. The change checks that field's kw_only flag before selecting this shorthand; otherwise it retains the argument dictionary. This also covers ReportEvaluator, which inherits the same base.

Schema generation uses the shared build_schema_types() helper. For explicit signatures without **kwargs, it records keyword-only parameters, omits scalar shorthand when its argument would have to be keyword-only, and includes a keyword mapping for a single keyword-only parameter. It still permits a required positional parameter's compact form when other optional keyword-only parameters exist. No new spec form or loader fallback is introduced.

The shared helper's other public caller was checked before changing it: a custom capability declaring from_spec(cls, *, minimum: int) accepts a keyword mapping through Agent.from_spec(), but its generated AgentSpec schema previously rejected that mapping and accepted the unusable scalar form. A public load/schema regression covers this path.

Variadic keyword signatures retain their existing schema. SetToolMetadata accepts arbitrary metadata such as code_mode=True, so restricting that schema to the named tools parameter would reject valid existing configurations. The implementation excludes VAR_KEYWORD signatures from the keyword-only adjustment and adds a regression for arbitrary metadata. The complete built-in capability schema remains identical to its existing snapshot; no snapshot update is needed. Broader changes to arbitrary-**kwargs schemas remain outside this repair.

The new persistence tests cover class-level @dataclass(kw_only=True), field-level field(kw_only=True), required and default-valued parameters, mixed field kinds, dataset and case evaluators, report evaluators, JSON and YAML, generated-schema validation, and evaluation after loading. Positional dataclasses are successful controls.

The final targeted command passed all 42 selected tests:

pytest tests/test_capability_spec.py::test_model_json_schema_with_capabilities tests/test_spec.py tests/evals/test_keyword_only_serialization.py -q --tb=short

Earlier scoped validation also passed the evaluator base/spec tests and the existing dataset/capability schema, serialization, and round-trip selections. The final command includes the complete built-in capability schema test with MCP available.

pyright pydantic_evals/pydantic_evals/evaluators/_base.py pydantic_ai_slim/pydantic_ai/_spec.py tests/test_spec.py tests/evals/test_keyword_only_serialization.py
ruff check pydantic_evals/pydantic_evals/evaluators/_base.py pydantic_ai_slim/pydantic_ai/_spec.py tests/test_spec.py tests/evals/test_keyword_only_serialization.py
ruff format --check pydantic_evals/pydantic_evals/evaluators/_base.py pydantic_ai_slim/pydantic_ai/_spec.py tests/test_spec.py tests/evals/test_keyword_only_serialization.py
git diff --check

Pyright reported zero errors and zero warnings with the test environment's interpreter explicitly selected. Import-blocking probes for missing jsonschema and missing pydantic_evals each collected and skipped all 12 persistence tests with exit code 0; test parameterization uses string names so absent optional imports cannot cause a collection-time NameError.

No runtime dependency or public signature changes are included. Separate init=False handling, arbitrary custom constructor overrides, and automatic recovery of previously exported invalid scalar configurations are outside the repair. Such old keyword-only configurations can be written using the existing named-argument mapping.

PR decisions

2026-10-26 · Keep keyword-only schemas consistent · iter -

2026-10-26 · Preserve variadic keyword schemas · iter -

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it. (Not applicable: no compatibility impact identified.)
  • PR title is fit for the release changelog.