Add `CapabilityNamespace` to resolve agent spec capability names from `module:attribute` targets, entry points and modules

Merged
DO
4d ago
+863-34
  • Part of #8426: this is the first of its two pieces, CapabilityNamespace (where a spec's capability names resolve to classes). CapabilityPolicy (which of those names a caller admits, and with which argument values) follows in its own PR, so #8426 stays open.

A spec names a capability by its bare class name, such as - Planning: {}, so the code that loads it decides which class each name means. Until now the only way to go beyond the built-ins was to import every class and pass it in custom_capability_types. CapabilityNamespace adds the other ways #8426 lays out, as tiers that resolve in this order before the built-ins:

from pydantic_ai import Agent, CapabilityNamespace

namespace = CapabilityNamespace(
    types=[MyCapability],                                       # what custom_capability_types is today
    names={'Planning': 'pydantic_ai_harness.planning:Planning'},  # imported only when a spec uses it
    entry_points='pydantic_ai.capabilities',                    # any installed package can add to it
    modules=['my_package.capabilities'],                        # every capability the module exports
)
agent = Agent.from_file('agent.yaml', capability_namespace=namespace)
  • Lazy where it can be. names and entry point targets are imported on first use, and checking or listing a name (the "Valid choices" error included) never imports it. modules are imported when the spec is loaded, since finding a module's capabilities means importing it.
  • Overrides are explicit. A higher tier overrides a lower one, including a built-in. Two different classes for one name within a tier raise, as duplicates in custom_capability_types already did.
  • A target is checked. It must be a @dataclass AbstractCapability whose serialization name matches the name it's listed under, or the load fails with one error that names the target.
  • Nested specs use the same namespace. PrefixTools: {capability: Planning} resolves through it, via the existing spec-loading context.
  • Schemas. AgentSpec.to_file and model_json_schema_with_capabilities accept it.
  • Backward compatible. capability_namespace defaults to None, so every existing call behaves as before. custom_capability_types keeps working and shares the types tier.

Out of scope here, following #8426's plan: admission and argument policy (CapabilityPolicy, with the nested-load fallback raising under a policy), CLI flags, and registering the Harness's capabilities as entry points.

Docs: the agent-spec page's "Custom capabilities in specs" section, with a running example and the tier table, plus pointers from the custom-capabilities and extensibility pages and the building-pydantic-ai-agents skill.

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.

šŸ¤– Generated with Claude Code


Summary by cubic

Adds CapabilityNamespace so agent specs can resolve bare capability names to classes from module:attribute targets, entry point groups, or whole modules, complementing custom_capability_types. Part of #8426.

  • Names resolve in tier order types → names → entry_points → modules → built-ins; a higher tier overrides a lower one, while two different classes for one name within a tier raise an error. The same class under a name — through custom_capability_types and types, or via two entry points or modules — is one capability.
  • names and entry point targets are imported only when a spec uses them; listing or checking a name never imports it, while modules are imported at spec load. An entry point named like a built-in is ignored with a warning naming its package — skipped when the name is already settled in names or types — since overriding a built-in is the caller's choice. A clashing entry point entry, or an installed one that fails to load (a missing extra, attribute, or a name mismatch), is left out of listings and the generated schema and raises only when a spec uses that name; a target the caller wrote still raises in schema generation. A names or types entry settles the clash.
  • A target must be a @dataclass AbstractCapability whose serialization name matches the name it is listed under, or loading fails with one error that names the target and whatever its import raised. An entry point naming a bare module fails as "not a capability".
  • modules honors a module's __all__ when present (including an empty one, which exports nothing); without __all__, it offers only the capability classes the module itself defines, not ones it imports.
  • Without a namespace, the registry order is unchanged, so the JSON schema's capability union and the "Valid choices" list don't move for existing custom_capability_types users. Built-ins resolve under their own serialization name even if registered under a different key.
  • pydantic_ai.capabilities is the entry point group packages register capabilities in; the publishing guide and skill show the pyproject.toml entry to use.
  • Nested specs like PrefixTools: {capability: ...} resolve through the same namespace, and AgentSpec.to_file and model_json_schema_with_capabilities accept it for schema generation.
  • An unknown name error points at both custom_capability_types and capability_namespace, and the threat model notes that enabling entry_points trusts every installed package publishing into that group.
  • capability_namespace defaults to None and custom_capability_types keeps working, so existing behavior is unchanged.