Make the `ModelFallback` capability constructible from agent specs

Merged
DO
1mo ago
+386-12
  • Closes #9896
  • Stacked on #9348 (ModelFallback); this PR is retargeted to main when that merges.

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

  1. fallback_on names: a spec's fallback_on takes the names of the 14 public ModelAPIError classes in pydantic_ai.exceptions, as an enum in the schema so editors autocomplete them. Any other name raises a UserError that 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.
  2. Single-string forms: a bare string is accepted for models and for fallback_on, matching ModelFallback('x').
  3. 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_on names, given singly and as a list;
  • a guard that the names match the public ModelAPIError subclasses;
  • six error cases;
  • YAML and JSON files round-tripped through Agent.from_file;
  • run(spec=) and override(spec=);
  • the CAPABILITY_TYPES and 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_TYPES entry only; waiver included.)
  • PR title is fit for the release changelog.

šŸ¤– Generated with Claude Code