Load the `{SetToolMetadata: [...]}` and `{SetToolMetadata: 'all'}` agent spec short forms instead of failing

Merged
DS
22d ago
+31-6

This pull request was posted by Claude Code using claude-opus-5-5 on behalf of David.

Why we make these changes

The AgentSpec schema accepts {SetToolMetadata: ['a']} and {SetToolMetadata: 'all'}, but Agent.from_spec rejects both with SetToolMetadata.__init__() takes 1 positional argument but 2 were given. The schema builder keeps a short form for tools because SetToolMetadata takes **metadata, and the default from_spec passes the short-form value positionally to an __init__ whose tools is keyword-only.

SetToolMetadata.from_spec now takes tools positionally too, so the loader agrees with the schema. The issue suggested changing the schema instead. That would edit the shared _spec.py, which builds the schema for every capability, while only this one target needs the fix.

New public surface

SetToolMetadata.from_spec, an override of AbstractCapability.from_spec.

User-visible behavior

from pydantic_ai import Agent

Agent.from_spec({'model': 'test', 'capabilities': [{'SetToolMetadata': ['a']}]})
# before: ValueError: Failed to instantiate capability 'SetToolMetadata': SetToolMetadata.__init__() takes 1 positional argument but 2 were given
# after: loads SetToolMetadata(tools=['a']), the same capability as {'SetToolMetadata': {'tools': ['a']}}

Verification

  • TestBuildSchemaTypes.test_variadic_keyword_capability_schema validates and loads ['a'], 'all', {code_mode: True} and {tools: ['a'], code_mode: True}. The two short-form cases fail on main.
  • test_model_json_schema_with_capabilities passes with its snapshot unchanged, and the full AgentSpec.model_json_schema_with_capabilities() output is byte-identical to main's.

What changes for existing users

Nothing that loads today changes; the two short forms now load instead of raising.

Deliberate scope choices:

  • tools is positional in the spec but keyword-only in Python. A spec short form carries a single value, so the ambiguity the keyword-only split guards against cannot arise there.
  • No docs change: the capability spec syntax section already says {'MyCapability': value} calls MyCapability.from_spec(value).
PR decisions

2026-11-13 · fix shape (a): SetToolMetadata.from_spec · iter -

  • Decision: Add SetToolMetadata.from_spec(cls, tools: ToolSelector[Any] = 'all', **metadata: Any) -> SetToolMetadata[Any] returning cls(tools=tools, **metadata); do not touch _spec.py
  • Why: Narrowest fix at the one reproduced target (fix_shapes K1: only SetToolMetadata moves of 137 targets); schema-side fix (b) edits shared _spec.py and changes the AgentSpec schema; decided by deliberator, no Douwe or David ruling needed
  • Source: https://github.com/pydantic/pydantic-ai/issues/13333
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · schema and load behavior with the fix · iter -

  • Decision: AgentSpec schema byte-identical; ['a'] and 'all' load as the same no-ops {tools: ['a']} / {tools: 'all'} already load as on main (metadata {}); {code_mode: true} unchanged
  • Why: Validator and deliberator probes on main af851a667, re-run on a553eb969
  • Source: https://github.com/pydantic/pydantic-ai/issues/13333
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · #13378 out of scope · iter -

  • Decision: #13378 (reverse short form) stays out of this PR; bundled with #13334
  • Why: Different cause: short form uses the param's full type and the loader reads any string-keyed dict as kwargs; fix_shapes.out shows one change does not fix both
  • Source: https://github.com/pydantic/pydantic-ai/issues/13378
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · PR body: why not the issue's schema-side suggestion · iter -

  • Decision: Closes #13333; say the schema-side suggestion would edit the shared _spec.py while only this one target needs the fix; do not cite NativeTool.from_spec as the reason
  • Why: NativeTool's tool is already positional in init, so that precedent is cosmetic
  • Source: https://github.com/pydantic/pydantic-ai/issues/13333
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · deliberate: positional in spec, keyword-only in Python · iter -

  • Decision: Flag in PR body: tools is positional in the spec but keyword-only in Python
  • Why: A spec short form carries one value, so the ambiguity the keyword-only split guards against is absent
  • Source: https://github.com/pydantic/pydantic-ai/issues/13333
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · deliberate: no docs change · iter -

  • Decision: No docs change; flag as deliberate in PR body
  • Why: docs/agent-spec.md 'Capability spec syntax' short-form line ({'MyCapability': value} calls MyCapability.from_spec(value)) already covers it
  • Source: https://github.com/pydantic/pydantic-ai/issues/13333
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · tests · iter -

  • Decision: Next to the existing SetToolMetadata spec test: load ['a'] and 'all' (each must fail on main), keep the keyword-form cases, AgentSpec schema snapshot unchanged
  • Why: Deliberator test plan
  • Source: https://github.com/pydantic/pydantic-ai/issues/13333
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · #5436 pointer · iter -

  • Decision: Once the PR exists, comment on #5436 linking it: the new from_spec keeps spec short forms loading alongside that init rewrite
  • Why: DouweM's open #5436 rewrites SetToolMetadata.init
  • Source: https://github.com/pydantic/pydantic-ai/pull/5436
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · pre-push review 1 of 3 at 2ba646e6c65108a5586b768a3bdc48eda2ffafc1 · iter -

2026-11-13 · pre-push review 1 of 3 at 2ba646e6c65108a5586b768a3bdc48eda2ffafc1 · iter -

2026-11-13 · douwebot ahead of PR Review Checks; one more PRC rerun · iter -

  • Decision: Apply douwebot now on 2ba646e6c, before PR Review Checks passes; rerun PR Review Checks once in about 1 h. Verbatim: "If it fails on quota again, report it and don't retry further."
  • Why: PR Review Checks failed twice (run 43421112899 attempts 1-2) with 429 quota on all reviewer providers and no findings; douwebot runs on other engines. Manager ruling relayed for David (option 2)
  • Source: https://github.com/pydantic/pydantic-ai/pull/13380
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · no cluster · iter -

2026-11-13 · douwebot third try · iter -

  • Decision: Re-apply douwebot once more on 2ba646e6c. Verbatim: "If this third try fails, report it and stop."
  • Why: Douwebot's Claude engine fails with 'did not return structured_output' on every PR right now (also #13385's run 43433486240); on #13385 the opencode-zai fallback approved at 13:31Z, so fallback quota is back. Manager ruling relayed for David
  • Source: https://github.com/pydantic/pydantic-ai/actions/runs/43433486240
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · douwebot optional docstring nit dismissed · iter -

  • Decision: Keep the from_spec docstring wording; dismissed the Optional finding, resolved the thread, no push
  • Why: The sentence states current behavior (positional tools in spec vs keyword-only init), the deliberate divergence; bullets already list both spec forms; dispatch: don't push just to clear an optional nit
  • Source: https://github.com/pydantic/pydantic-ai/pull/13380#discussion_r4564029233
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · step 7 second opinion: none · iter -

  • Decision: No second opinion on 2ba646e6c; the merge bar's required reviews gate
  • Why: David's pick, relayed by the manager; stats: size S, 2 files, +31 -6, only public-API rung an override of an existing hook with byte-identical schema; local pre-push, douwebot, PR Review Checks and Macroscope already reviewed
  • Source: https://github.com/pydantic/pydantic-ai/pull/13380
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

2026-11-13 · final metadata check · iter -

  • Decision: Fresh Haiku checker from policy-base 23c03ca12: kept 1 correction (one-sentence 'What changes for existing users'), applied; dropped 1 (missing step-7 decisions entry) as a stale cached read: the live body already had it
  • Why: Rule: pushing-commits-to-the-repo 'Write the title and body' item 5; live body verified with gh-fresh after the edit
  • Source: https://github.com/pydantic/pydantic-ai/pull/13380
  • Session: 9c11d269-af3e-4252-bda4-3ef4f5e5323e

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.

https://claude.ai/code/session_01HhQjZ9kPBC6F76DGEAWrFm


Summary by cubic

Fixes Agent.from_spec failing on the {SetToolMetadata: [...]} and {SetToolMetadata: 'all'} spec short forms, which the AgentSpec schema already accepted. Adds SetToolMetadata.from_spec so tools is taken positionally and the loader matches the schema builder.

Both short forms now load as the same SetToolMetadata instances as their keyword equivalents instead of raising SetToolMetadata.__init__() takes 1 positional argument but 2 were given. No behavior changes for anything that loaded before, and the AgentSpec schema output stays byte-identical to main.

Closes #13333. The issue suggested changing the schema, but that would edit the shared _spec.py and affect every capability, while only this target needs the fix. tools remains keyword-only in __init__; the short form carries a single value, so the ambiguity that split guards against cannot arise there. No docs change is needed since the capability spec syntax section already documents short forms.