Save keyword-only evaluator arguments by name in `Dataset` files, and drop the positional spec schema form for keyword-only parameters
This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.
Closes #11268
Why we make these changes
Dataset.to_file() wrote a keyword-only evaluator field in the positional short form, so the file failed to load or loaded the value into another field. The generated JSON schema had the same blind spot for keyword-only parameters, for evaluators and capabilities alike, so fixing the writer alone would produce files the saved schema rejects.
This builds on @dafyy321-pixel's #11270: their commit is cherry-picked with authorship kept, and its _spec.py part is ported onto #12554's refactor. Later commits add a regression test for the wrong-field case and a docs note on the dict form. Thank you, @dafyy321-pixel!
New public surface: none.
User-visible behavior:
| Saved evaluator | Before | After |
|---|---|---|
@dataclass(kw_only=True) with minimum: int = 3, saved as MinimumLength(minimum=4) | MinimumLength: 4; loading fails with takes 1 positional argument but 2 were given | MinimumLength: {minimum: 4}; round-trips |
minimum: int = field(default=0, kw_only=True) declared before evaluation_name: str = 'length', saved with minimum=4 | Name: 4; reloads silently as minimum=0, evaluation_name=4 | Name: {minimum: 4}; round-trips |
Schema for a keyword-only parameter (evaluator, or capability from_spec(cls, *, minimum)) | accepts {Name: 4}, rejects {Name: {minimum: 4}} | the reverse, matching the loader |
- Files already saved in the bad form are not repaired:
MinimumLength: 4still fails to load, and a wrong-field file still loads into the wrong field. Re-save them. - For keyword-only evaluators,
as_spec()also feedsEvaluationResult.source, the report'ssource, and the OTel attributegen_ai.evaluation.evaluator.source. These change fromarguments: [4]to{minimum: 4}even when nothing is saved. This is a correction. - The default-registry schemas from
AgentSpec.model_json_schema_with_capabilities()andDataset.model_json_schema_with_evaluators()are byte-identical, and configurations that round-trip today save byte-identically. Targets that accept**kwargskeep their schema, so{SetToolMetadata: {code_mode: True}}still validates. Outside the defaults, the scalar form disappears only from the harnessAskUserand clai2SessionTracingschemas, where it never loaded.
Verification:
- Evaluator JSON/YAML round trip over class- and field-level keyword-only, mixed and positional fields, including the wrong-field case, each checking the saved form, the saved schema and the reloaded value.
- Report evaluator round trip.
- Keyword-only capability schema and
**kwargscapability schema. - Against
main's source, every keyword-only case above fails and the positional and**kwargscases pass.
What changes for existing users: keyword-only evaluators save by name and reload correctly, spec schemas offer the positional form only where the loader accepts it, and files saved in the old form must be re-saved.
Split out: #13333 (SetToolMetadata schema short forms the loader rejects) and #13334 (a single-field evaluator with a dict value fails its own schema, also on main).
PR decisions
2026-11-13 · Validator findings: writer and schema both wrong · iter -
- Decision: Writer: as_spec writes the positional short form for a kw_only first field; when a positional field follows, the file loads silently with the value in the other field (FirstFieldKwOnly(a=5) reloads as a=1, b=5). Schema: build_schema_types emits the scalar short form for a single keyword-only parameter and rejects the keyword mapping; same for capabilities. Loader is correct.
- Why: Reproduced on upstream/main 75ef1a9bd and fixed on this branch by local-notes/readiness repro.py and schema_check.py; with only the writer fixed, to_file writes a file its own schema rejects, so both halves are needed
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Ship as our own PR on top of contributor PR 11270 · iter -
- Decision: Cherry-pick https://github.com/pydantic/pydantic-ai/pull/11270 so its author keeps git authorship, open our own PR crediting them in the body, then close 11270 with a short credit comment linking ours
- Why: David chose our own PR; 11270 conflicts with main in _spec.py and the validator derived the same fix blind
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Port 11270's schema logic onto main's _spec_param_type_hints refactor · iter -
- Decision: Keep their behavior: a keyword-only parameter suppresses the scalar short form, a single keyword-only parameter gets the keyword mapping, and no parameter is marked keyword-only on a **kwargs target. Keyword-only detection lives in _keyword_only_spec_params next to build_schema_types, and the nested short-form if is merged
- Why: #12554 moved the parameter loop into _spec_param_type_hints; inlining their logic took build_schema_types 11->16 and _spec_param_type_hints 13->17, over the cognitive complexity limit of 15 (agent_docs/complexity.md crossed-the-limit: extract one block)
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Must not move · iter -
- Decision: AgentSpec.model_json_schema_with_capabilities() and Dataset.model_json_schema_with_evaluators() stay byte-identical; every case that round-trips today saves byte-identically; {SetToolMetadata: {code_mode: True}} must still validate
- Why: Fix is scoped to keyword-only signatures; verified with local-notes/readiness/schema_diff.py (identical) and repro.py (only kw-only lines change)
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Behavior section names unrepaired files and the source change · iter -
- Decision: PR body states that the loader does not repair files already saved in the bad form ({KwOnly: 4} still fails, FirstFieldKwOnly(a=5) files still load as b=5; re-save them), and that for keyword-only evaluators as_spec also feeds EvaluationResult.source, the report's source and the OTel attribute gen_ai.evaluation.evaluator.source, which change from arguments: [4] to {minimum: 4} even when nothing is saved; this is a correction
- Why: Settled by the manager for David
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Split SetToolMetadata schema and positional dict-value schema to follow-ups · iter -
- Decision: After the PR opens, file through /file-followup-issue with no labels and no assignee: (1) SetToolMetadata schema accepts {SetToolMetadata: ['a']} and 'all', which the loader rejects; (2) a single-parameter positional evaluator whose value is a string-keyed dict is written in the keyword form and rejected by its own schema, on main and with the fix
- Why: Both reproduce on main independently of this fix (schema_check.py, to_file_schema_positional_dict.py); the manager routes them
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Package label pkg:evals · iter -
- Decision: Apply pkg:evals only, though the diff also touches pydantic_ai_slim/pydantic_ai/_spec.py
- Why: pushing-commits-to-the-repo: for changes spanning packages, choose the package whose users the change serves; the reported defect is Dataset files written by pydantic-evals, and the _spec.py half exists so to_file writes a file its own schema accepts
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Keep the new tests in 11270's feature file · iter -
- Decision: Keep tests/evals/test_keyword_only_serialization.py and add the silent wrong-value case (KeywordOnlyFirstWithDefault) as a row of its round-trip parametrize, instead of moving cases into test_evaluator_base.py or test_report_evaluators.py
- Why: tests/AGENTS.md prefers feature-centric parametrized test files; the defect shows on reload, and that test already asserts the saved form, the schema and the reloaded value for each field shape
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · Non-built-in schemas that move are corrections · iter -
- Decision: Schemas move only for AskUser (harness), SessionTracing (clai2), TaskLink and OnlineEvaluation: the scalar short form is dropped because their one required parameter is keyword-only. Accept it
- Why: On AskUser and SessionTracing the dropped scalar form already fails to load (takes 1 positional argument but 2 were given); TaskLink is not a dataclass and OnlineEvaluation has no serialization name, so neither is spec-loadable. Checked over 191 classes by local-notes/readiness/changed_forms.py and changed_forms_load.py
- Source: https://github.com/pydantic/pydantic-ai/issues/11268
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · pre-push review 1 of 3 at 959ef844dbc215b55bedb97792db1aa47f6d334b · iter -
- Decision: reserved; dispatching (haiku, claude -p from detached policy-base checkout at 75ef1a9bd, read-only tools)
- Why: tree 4a3ef802593a75656777317bb8c6fe0f0e17fe29; scope 75ef1a9bd9b6a841efc320adc9a27668bd689bb1..959ef844dbc215b55bedb97792db1aa47f6d334b (full branch); first review
- Source: https://github.com/pydantic/pydantic-ai/tree/kw-only-evaluators
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · pre-push review 1 of 3 at 959ef844dbc215b55bedb97792db1aa47f6d334b · iter -
- Decision: remediated: docs/evals/how-to/dataset-serialization.md Single Parameter (Short Form) now says the short form passes the first positional argument and a keyword-only parameter uses the dict form, with an example
- Why: reviewed tree 4a3ef802593a75656777317bb8c6fe0f0e17fe29; scope 75ef1a9bd..959ef844d (full branch); first review; one finding, valid
- Source: https://github.com/pydantic/pydantic-ai/tree/kw-only-evaluators
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · pre-push review 2 of 3 at 9010fba7c1845e0b9cdeb32b1daf39705621ebcf · iter -
- Decision: reserved; dispatching (haiku, claude -p from detached policy-base checkout at 75ef1a9bd, read-only tools)
- Why: tree 08e285ea4c69a687fc213e1722fbdbb6b157e2b1; scope 959ef844dbc215b55bedb97792db1aa47f6d334b..9010fba7c1845e0b9cdeb32b1daf39705621ebcf (follow-up: docs remediation of review 1's finding)
- Source: https://github.com/pydantic/pydantic-ai/tree/kw-only-evaluators
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · pre-push review 2 of 3 at 9010fba7c1845e0b9cdeb32b1daf39705621ebcf · iter -
- Decision: remediated: the dict-form example's MinimumLength evaluator is now declared in the sentence (kw_only dataclass, single field minimum: int) instead of a new executable block; review 1's finding confirmed fixed
- Why: reviewed tree 08e285ea4c69a687fc213e1722fbdbb6b157e2b1; scope 959ef844d..9010fba7c (follow-up); one finding, partly valid: an undeclared evaluator name, fixed in prose because a YAML-only format section needs no runnable class
- Source: https://github.com/pydantic/pydantic-ai/tree/kw-only-evaluators
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · pre-push review 3 of 3 at 20e0241f32524c9dad504e44a45adac3c36053a6 · iter -
- Decision: reserved; dispatching (haiku, claude -p from detached policy-base checkout at 75ef1a9bd, read-only tools)
- Why: tree 8a7b027202fdc01a5b50b40cbeae0b699d8d9616; scope 9010fba7c1845e0b9cdeb32b1daf39705621ebcf..20e0241f32524c9dad504e44a45adac3c36053a6 (follow-up: docs remediation of review 2's finding)
- Source: https://github.com/pydantic/pydantic-ai/tree/kw-only-evaluators
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · pre-push review 3 of 3 at 20e0241f32524c9dad504e44a45adac3c36053a6 · iter -
- Decision: remediated: added a runnable MinimumLength class block before the dict-form YAML example; budget exhausted, no fourth review; pushing 2a6c09b7b56ec1b27648e6ee7cdbd12a871e2e54 after the page's docs examples passed
- Why: reviewed tree 8a7b027202fdc01a5b50b40cbeae0b699d8d9616; scope 9010fba7c..20e0241f3 (follow-up); one finding, valid (reviews 2 and 3 converged on an undeclared evaluator)
- Source: https://github.com/pydantic/pydantic-ai/tree/kw-only-evaluators
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · defer: SetToolMetadata schema accepts short forms the loader rejects · iter -
- Decision: Filed as https://github.com/pydantic/pydantic-ai/issues/13333 instead of fixing here; no labels, no assignee (manager routes)
- Why: Out of scope for this PR — fixing it moves the built-in AgentSpec schema this PR keeps byte-identical
- Source: https://github.com/pydantic/pydantic-ai/issues/13333
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · defer: single-field evaluator with a dict value fails its own schema · iter -
- Decision: Filed as https://github.com/pydantic/pydantic-ai/issues/13334 instead of fixing here; no labels, no assignee (manager routes)
- Why: Out of scope for this PR — a positional-field case on main; fixing it moves the built-in Dataset and AgentSpec schemas
- Source: https://github.com/pydantic/pydantic-ai/issues/13334
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · douwebot applied at 2a6c09b7b56ec1b27648e6ee7cdbd12a871e2e54 · iter -
- Decision: Applied douwebot after CI success (run 43350525480) and PR Review Checks success on this head
- Why: Merge bar for size M needs a clean douwebot approval on the current head; diff touches no agent-config guarded path
- Source: https://github.com/pydantic/pydantic-ai/pull/13332
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · douwebot optional: agent-spec.md note skipped · iter -
- Decision: No change to docs/agent-spec.md; replied, 👎, resolved
- Why: net-neutral: its capability-forms line already says the scalar form is a single positional argument calling from_spec(value)
- Source: https://github.com/pydantic/pydantic-ai/pull/13332#discussion_r4559123359
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · douwebot optional: duplicate inspect.signature kept · iter -
- Decision: Keep _keyword_only_spec_params computing its own signature; replied, 👎, resolved
- Why: net-neutral: one call per registry entry at schema generation, same target so no drift; sharing it means changing _spec_param_type_hints, whose first port went over the complexity limit
- Source: https://github.com/pydantic/pydantic-ai/pull/13332#discussion_r4559123502
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · merge bar met at 2a6c09b7b56ec1b27648e6ee7cdbd12a871e2e54 · iter -
- Decision: Holding for the manager's step-7 second-opinion choice; final metadata check runs after it settles
- Why: CI run 43350525480 success; PR Review Checks success; douwebot APPROVE run 43352890288 with two Optional findings, both resolved; Gate: clean douwebot approval success; 0 unresolved threads
- Source: https://github.com/pydantic/pydantic-ai/pull/13332
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · step-7 second opinion: pydanty:review-lite · iter -
- Decision: David picked pydanty:review-lite as the second opinion for head 2a6c09b7b56ec1b27648e6ee7cdbd12a871e2e54; the manager enqueues
- Why: Small diff that changes a shared schema helper the capabilities also use; review-lite writes per-diff charters, reports only, and has no CI precondition
- Source: https://github.com/pydantic/pydantic-ai/pull/13332
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · pydanty review-lite finding fixed at 76c37f616bcc9510821f82cfdc2873f0437852dd · iter -
- Decision: Moved test_evaluator_file_round_trip's per-evaluator expectations into a RoundTripCase table (evaluator, saved arguments, spec forms in the saved schema); pushed without a local review
- Why: Finding valid against tests/AGENTS.md Parametrization with Expectations; delta 2a6c09b7b..76c37f616bcc9510821f82cfdc2873f0437852dd is test code only with the same expectations (exempt: test-harness repair to established behavior), and the pre-push budget is exhausted; 14 pass on the branch, the same 10 fail on main
- Source: https://github.com/pydantic/pydantic-ai/pull/13332#issuecomment-6842505824
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · douwebot applied at 76c37f616bcc9510821f82cfdc2873f0437852dd · iter -
- Decision: Applied douwebot after CI success (run 43363219561) and PR Review Checks success on this head
- Why: Head moved after the review-lite fix; the merge bar needs a clean douwebot approval on the current head
- Source: https://github.com/pydantic/pydantic-ai/pull/13332
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
2026-11-13 · douwebot optional: capability schema tests stay in test_spec.py · iter -
- Decision: Keep test_keyword_only_capability_schema and test_variadic_keyword_capability_schema in tests/test_spec.py::TestBuildSchemaTypes; replied, 👎, resolved
- Why: net-neutral: they pin the build_schema_types change next to its tests, where 11270 placed them; moving changes no coverage; douwebot called it a judgment call
- Source: https://github.com/pydantic/pydantic-ai/pull/13332#discussion_r4559713637
- Session: bfc3e9b1-a80d-48e4-b27f-a09a73e40dc0
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.
- PR title is fit for the release changelog.