Add `CapabilityNamespace` to resolve agent spec capability names from `module:attribute` targets, entry points and modules
- 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.
namesand entry point targets are imported on first use, and checking or listing a name (the "Valid choices" error included) never imports it.modulesare 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_typesalready did. - A target is checked. It must be a
@dataclassAbstractCapabilitywhose 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_fileandmodel_json_schema_with_capabilitiesaccept it. - Backward compatible.
capability_namespacedefaults toNone, so every existing call behaves as before.custom_capability_typeskeeps working and shares thetypestier.
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 ā throughcustom_capability_typesandtypes, or via two entry points or modules ā is one capability. namesand entry point targets are imported only when a spec uses them; listing or checking a name never imports it, whilemodulesare 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 innamesortypesā 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. Anamesortypesentry settles the clash.- A target must be a
@dataclassAbstractCapabilitywhose 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". moduleshonors 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_typesusers. Built-ins resolve under their own serialization name even if registered under a different key. pydantic_ai.capabilitiesis the entry point group packages register capabilities in; the publishing guide and skill show thepyproject.tomlentry to use.- Nested specs like
PrefixTools: {capability: ...}resolve through the same namespace, andAgentSpec.to_fileandmodel_json_schema_with_capabilitiesaccept it for schema generation. - An unknown name error points at both
custom_capability_typesandcapability_namespace, and the threat model notes that enablingentry_pointstrusts every installed package publishing into that group. capability_namespacedefaults toNoneandcustom_capability_typeskeeps working, so existing behavior is unchanged.