Make the `ModelFallback` capability constructible from agent specs
What
ModelFallback can be declared in agent specs, as decided on #9896: the capability itself is spec-constructible, model names are resolved lazily so ResolveModelId aliases keep working, and model: isn't overloaded with a list.
capabilities:
- ModelFallback: [anthropic:claude-opus-5-5, openai:gpt-6.1-sol]
- ModelFallback:
models: [anthropic:claude-opus-5-5]
fallback_on: [ModelRateLimitError, ModelUnavailableError]
ModelFallback.from_spec passes model names through untouched, so they resolve at attempt time like any string given to ModelFallback(...). Precedence is unchanged: the agent's model, run(model=) and SelectModel behave as in Python, and when a spec has no model:, the first candidate supplies it. ModelFallback is added to CAPABILITY_TYPES, and the spec JSON schema picks up both forms.
The default from_spec (cls(*args, **kwargs)) wouldn't have handled the list form: the loader passes the list as a single argument, so the capability needs its own from_spec.
Decisions for review
fallback_onnames: a spec'sfallback_ontakes the names of the 14 publicModelAPIErrorclasses inpydantic_ai.exceptions, as an enum in the schema so editors autocomplete them. Any other name raises aUserErrorthat lists the valid ones. Exception and response handlers stay Python-only. This is easy to drop if you'd rather keep specs to models only.- Single-string forms: a bare string is accepted for
modelsand forfallback_on, matchingModelFallback('x'). - The enum is inlined twice in the schema (single-name and list forms), rather than introducing a named shared definition.
API compatibility
CAPABILITY_TYPES gains ModelFallback. It has a waiver in .github/api-compatibility-allowlist.json, the same kind as the existing Citations and FileUnderstanding entries.
Tests
tests/test_capability_model_fallback.py and tests/test_capability_spec.py. Spec models map aliases to FunctionModels through ResolveModelId, which also covers lazy alias resolution. They cover:
- both forms with no
model:; - a spec
model:failing over to the spec's fallback; run(model=)outranking it;fallback_onnames, given singly and as a list;- a guard that the names match the public
ModelAPIErrorsubclasses; - six error cases;
- YAML and JSON files round-tripped through
Agent.from_file; run(spec=)andoverride(spec=);- the
CAPABILITY_TYPESand JSON schema snapshots.
Docs: an "In agent specs" section on the ModelFallback page, docs/agent-spec.md, and the agent skill.
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver. (Additive
CAPABILITY_TYPESentry only; waiver included.) - PR title is fit for the release changelog.
š¤ Generated with Claude Code