pydantic-ai/docs/models/overview.md

68 KiB

description
Pydantic AI is model-agnostic: switch between OpenAI, Anthropic, Gemini, Amazon Bedrock, Google Vertex AI, Azure OpenAI and local models by changing one string. Find each provider's setup guide, model selection and fallback configuration.

Models and Providers

Pydantic AI supports model developers, cloud platforms, inference services, gateways, and local model servers. Find your service below and follow its setup guide for installation, authentication, and supported features.

Pydantic AI is model-agnostic: switch an agent between OpenAI, Anthropic, Gemini, Amazon Bedrock, Google Vertex AI, Azure OpenAI and local models by changing one string, without rewriting the agent. It runs on Azure OpenAI, Amazon Bedrock and Google Vertex AI with your cloud's own credentials.

To keep an agent running when a provider fails, give it other models to fall back to with the ModelFallback capability: see Fallback Model.

Provider directory

Pass a name in the form <provider>:<model> to [Agent][pydantic_ai.Agent] to select a provider by its prefix. Some routes require an explicit model or client instead; each setup guide shows the available options.

Provider and setup Service Model selection
Pydantic AI Gateway Gateway gateway/<provider>:
OpenAI Model developer openai:, openai-chat:, openai-responses:
Anthropic Model developer anthropic:
Google / Gemini API Model developer google:
AWS Bedrock Cloud platform bedrock:, bedrock-mantle:; Anthropic client
Google Cloud / Vertex AI Cloud platform google-cloud:; Anthropic client
Microsoft Azure / Foundry Cloud platform azure:, azure-responses:; Anthropic client
Alibaba Cloud / Qwen (DashScope) Cloud platform; model developer alibaba:
Cerebras Inference platform cerebras:
Cohere Model developer cohere:
Crusoe Inference platform crusoe:
DeepSeek Model developer deepseek:; explicit Responses model
Fireworks AI Inference platform fireworks:
GitHub Copilot Subscription access github-copilot:
Google / Gemini API decisions Decision model google-decisions:
Groq Inference platform groq:
Heroku AI Cloud platform heroku:
Hugging Face Inference platform huggingface:
LiteLLM Self-hosted gateway (over HTTP; doesn't use the litellm package) litellm:
Meta Model developer meta:
Mistral Model developer mistral:
Moonshot AI / Kimi Model developer moonshotai:
Nebius AI Studio Inference platform nebius:
Ollama Local inference; cloud inference ollama:
OpenAI ChatGPT plan usage Subscription access for your app's users openai-chatgpt: with an explicit provider
OpenAI Codex Subscription access openai-codex:
OpenAI Decisions API Decision model openai-decisions:
OpenRouter Gateway openrouter:
Otari Gateway otari:
OVHcloud AI Endpoints Cloud platform ovhcloud:
SambaNova Inference platform sambanova:
Snowflake Cortex Cloud platform snowflake:
Stav Gateway stav:
System One API Decision models such as CLM, Laya, and Ollama's system-one:
Together AI Inference platform together:
TypeSafe (Jev) Decision model typesafe:
Vercel AI Gateway Gateway vercel:
vLLM Self-hosted inference vllm:
xAI Model developer xai:
Z.AI Model developer zai:

!!! tip "One key for every model" The easiest way to try models from several providers is the Pydantic AI Gateway: one API key for models from OpenAI, Anthropic, Google Cloud, Groq, and AWS Bedrock, with spending limits and cost monitoring in Pydantic Logfire. Set PYDANTIC_AI_GATEWAY_API_KEY and add the gateway/ prefix to the model string, for example Agent('gateway/{{ anthropic:xlarge }}'). The Gateway quick start shows how to create a key.

The service descriptions help you find a deployment option; a company may offer more than one kind of service. Feature support depends on the model and API you select, even when two services use the same API format.

For work that isn't waiting on a user, Anthropic, OpenAI and Gemini API models can also send requests through their providers' batch APIs, at about half the price.

For an OpenAI-compatible endpoint not listed here, use [OpenAICompatibleProvider][pydantic_ai.providers.openai_compatible.OpenAICompatibleProvider] with a name for the service, as Other endpoints explains; for any other API, implement a custom model.

For testing and development, use TestModel or FunctionModel.

OpenAI-compatible Providers

Many entries in the directory use OpenAI-compatible APIs. Their setup guides cover which model class to use; Other compatible APIs explains custom endpoints, which use [OpenAICompatibleProvider][pydantic_ai.providers.openai_compatible.OpenAICompatibleProvider] rather than OpenAIProvider, and model profiles.

Models, providers, and profiles

Pydantic AI uses a few key terms to describe how it interacts with different LLMs:

  • Model: This refers to the Pydantic AI class used to make requests following a specific LLM API (generally by wrapping a vendor-provided SDK, like the openai python SDK). These classes implement a vendor-SDK-agnostic API, ensuring a single Pydantic AI agent is portable to different LLM vendors without any other code changes just by swapping out the Model it uses. Model classes are named roughly in the format <VendorSdk>Model, for example, we have OpenAIChatModel, AnthropicModel, GoogleModel, etc. When using a Model class, you specify the actual LLM model name (e.g., {{ openai:medium.name }}, {{ anthropic:medium.name }}, {{ google:medium.name }}) as a parameter.
  • Provider: This refers to provider-specific classes which handle the authentication and connections to an LLM vendor. Passing a non-default Provider as a parameter to a Model is how you can ensure that your agent will make requests to a specific endpoint, or make use of a specific approach to authentication (e.g., you can use Azure auth with the OpenAIChatModel by way of the AzureProvider). In particular, this is how you can make use of an AI gateway, or an LLM vendor that offers API compatibility with the vendor SDK used by an existing Model (such as OpenAIChatModel).
  • Profile: This refers to a description of how requests to a specific model or family of models need to be constructed to get the best results, independent of the model and provider classes used. For example, different models have different restrictions on the JSON schemas that can be used for tools, and the same schema transformer needs to be used for Gemini models whether you're using GoogleModel or OpenAIChatModel with OpenRouterProvider.

When you instantiate an [Agent][pydantic_ai.Agent] with just a name formatted as <provider>:<model>, e.g. {{ openai:medium }} or openrouter:google/gemini-3-pro-preview, Pydantic AI will automatically select the appropriate model class, provider, and profile. If you want to use a different provider or profile, you can instantiate a model class directly and pass in provider and/or profile arguments.

Inspecting a model's profile

A model's [ModelProfile][pydantic_ai.profiles.ModelProfile] also describes what the model can do. It is a TypedDict whose keys are all optional, so you read capability flags with .get() on model.profile — for example [supports_text_output][pydantic_ai.profiles.ModelProfile.supports_text_output], [supports_tools][pydantic_ai.profiles.ModelProfile.supports_tools], [supports_json_schema_output][pydantic_ai.profiles.ModelProfile.supports_json_schema_output], and [supported_native_tools][pydantic_ai.profiles.ModelProfile.supported_native_tools]. This is useful when you want to branch on a capability rather than discover a limitation at request time — for example checking whether a model supports text generation, tool calling, native JSON-schema output, or a specific native tool before relying on it:

from pydantic_ai.models.test import TestModel
from pydantic_ai.native_tools import WebSearchTool

model = TestModel()
profile = model.profile

print(profile.get('supports_tools'))
#> True
print(profile.get('supports_text_output'))
#> True
print(profile.get('supports_json_schema_output'))
#> False
print(WebSearchTool in profile.get('supported_native_tools', frozenset()))
#> True

The profile also says which files the model accepts: [supports_image_input][pydantic_ai.profiles.ModelProfile.supports_image_input], [supports_document_input][pydantic_ai.profiles.ModelProfile.supports_document_input], [supports_audio_file_input][pydantic_ai.profiles.ModelProfile.supports_audio_file_input] and [supports_video_input][pydantic_ai.profiles.ModelProfile.supports_video_input] for each kind of file, narrowed to particular media types by [supported_image_media_types][pydantic_ai.profiles.ModelProfile.supported_image_media_types], [supported_document_media_types][pydantic_ai.profiles.ModelProfile.supported_document_media_types], [supported_audio_media_types][pydantic_ai.profiles.ModelProfile.supported_audio_media_types] and [supported_video_media_types][pydantic_ai.profiles.ModelProfile.supported_video_media_types]. The image, audio and video sets are unset by default, so a file of those kinds is judged by its kind alone; where a profile sets one, a file of a media type outside it counts as one the model can't read. The OpenAI, Anthropic, Google and xAI pages list the formats their models read. Each model class also turns off what its API can't take, whatever the profile says: [AnthropicModel][pydantic_ai.models.anthropic.AnthropicModel], for example, reads neither audio nor video, and only PDF and plain-text documents, and OpenAI Chat Completions takes only WAV and MP3 audio. A file a tool returns that the model can't take is sent as a note naming it, and the File Understanding capability reads the same flags to decide which files to describe in text first.

model.profile is usually the fully resolved profile: keys from [DEFAULT_PROFILE][pydantic_ai.profiles.DEFAULT_PROFILE] are merged with the provider's defaults, so profile.get('supports_tools') returns a value. If you supply profile= as a callable (or otherwise have a partial profile dict), pass the default as the fallback, profile.get('supports_tools', DEFAULT_PROFILE['supports_tools']) (after importing DEFAULT_PROFILE), to tolerate missing keys. Individual model adapters expose their resolved profile the same way, so the same check works whether the model was selected automatically from a <provider>:<model> name or instantiated directly. A [FallbackModel][pydantic_ai.models.fallback.FallbackModel] is different: it has no single profile because its candidate models may have different capabilities. Inspect the profile of each model in fallback_model.models instead. With the ModelFallback capability, each attempt is made with a model of its own, so a capability hook reads the profile of the model serving the attempt from request_context.model. Don't confuse profiles with Capabilities, which are reusable bundles of tools, hooks, and settings you add to an agent — the profile describes what the underlying model itself supports.

The profile also carries the model's [context_window][pydantic_ai.profiles.ModelProfile.context_window]: the maximum number of tokens it can handle in a single request, or None when unknown. Every model, including a FallbackModel and wrappers like [InstrumentedModel][pydantic_ai.models.instrumented.InstrumentedModel], exposes it as [model.context_window][pydantic_ai.models.AbstractModel.context_window]; a fallback model reports the smallest window among its candidates, so history that fits it fits whichever candidate answers. Inside a run, [ctx.context_window_used][pydantic_ai.tools.RunContext.context_window_used] reports the fraction in use, or None when it cannot be calculated. See Compact when the context window fills for an example that handles this case.

Deprecated models

When a provider has deprecated or retired a model, each request to it emits a [DeprecatedModelWarning][pydantic_ai.exceptions.DeprecatedModelWarning] naming the model, its retirement date and the replacement the provider recommends, with a link to the provider's announcement. Creating the model doesn't warn. Python's warning filters decide how often it's shown: with the default filters, run_sync and other code that enters warnings.catch_warnings() reset the record of warnings already shown, so a model can warn again on a later request. The warning is a UserWarning, so Python shows it by default.

The deprecation comes from the model's profile, whose [deprecation][pydantic_ai.profiles.ModelProfile.deprecation] holds a [ModelDeprecation][pydantic_ai.profiles.ModelDeprecation]. Pydantic AI sets it for models on the Anthropic and OpenAI APIs, from those providers' deprecation pages, including when you reach them through the Pydantic AI Gateway. Anthropic's dates also apply to the platforms it operates: Microsoft Foundry, Claude Platform on AWS and Claude Models on Google Cloud (Anthropic managed). The deprecation isn't set where another platform serves the same model on a schedule of its own: Amazon Bedrock, Google Cloud's Vertex AI, Azure, OpenRouter and the Codex backend. A Gateway endpoint that fails over to one of those platforms still gets the Anthropic or OpenAI date.

To turn the warning into an error, use a warnings filter. To stop it for one model, set the profile's deprecation to None; to warn about a model you serve elsewhere, set it to a ModelDeprecation of your own:

import warnings

from pydantic_ai import DeprecatedModelWarning
from pydantic_ai.models.anthropic import AnthropicModel

warnings.filterwarnings('error', category=DeprecatedModelWarning)

model = AnthropicModel('{{ anthropic:medium.name }}', profile={'deprecation': None})

HTTP Client Lifecycle

When a [Provider][pydantic_ai.providers.Provider] creates its own HTTP client (i.e. you don't pass a custom http_client), it owns that client's lifecycle. Using the [Agent][pydantic_ai.Agent] as an async context manager keeps the HTTP client open across every run inside the block, so the runs reuse its connections, and closes it cleanly on exit:

from pydantic_ai import Agent

agent = Agent('{{ openai:medium }}')

async def main():
    async with agent:
        result = await agent.run('What is the capital of France?')
        print(result.output)
        #> The capital of France is Paris.

You can also use a [Model][pydantic_ai.models.Model] or [Provider][pydantic_ai.providers.Provider] directly as an async context manager for the same effect. In a long-lived service such as a web server, enter the agent once when the service starts and run it inside that block.

What happens without that block depends on where a run's model comes from:

  • The agent's own model, or a Model instance passed to a run: the provider creates its HTTP client along with the model, and runs on the same event loop reuse it. Nothing closes the client until you enter and exit the agent, model or provider. An agent created with defer_model_check=True treats its model name like a model name passed to a run.
  • A model name passed to a run, as in agent.run(..., model='{{ openai:medium }}'), or returned by a model selector: the run creates a new provider, with a new HTTP client, and closes the client when it ends, so no connections are reused. Inside async with agent:, the agent instead creates one provider per model name and reuses it for later runs until the block exits. A resolve_model_id capability can resolve the same name differently for each run, so with one, each run resolves the name again.
  • A Model instance a model selector returns: each run enters and exits the model, which closes its provider's HTTP client at the end of the run and creates a new one for the next. Inside async with agent:, a model that's selected again, which the agent tells apart by identity, is instead entered by the agent and kept open until the block exits. A new instance the selector builds for a single run is still closed when that run ends. With a resolve_model_id capability, each run enters and exits the model again.

An HTTP client's connections belong to the event loop that opened them, so a client that stays open between runs can't be used from another event loop. Repeated [run_sync()][pydantic_ai.agent.AbstractAgent.run_sync] calls in one thread share an event loop, and so share the client. When a run of an agent that isn't entered is on a new event loop and the one the client was last used on isn't running, as with a second asyncio.run(agent.run(...)), the provider creates a new client for it. The old one isn't closed, as it can't be closed from another event loop, and its own has usually closed already. So when each run gets its own event loop, enter the agent inside it to close the client before its event loop is. That includes run_sync() calls from a pool of worker threads, as in a FastAPI def endpoint, a Django or Flask view or a ThreadPoolExecutor: each thread has its own event loop, so a call on a different thread than the last one replaces the client, and the old one keeps its connections open until it's garbage collected. Give each thread its own model, or enter the agent in each thread. Event loops that run at the same time would share the client, so give each its own model as well. A durable execution run leaves the clients alone, as its requests are sent from the engine's activities, steps or tasks.

Pydantic AI only ever closes or replaces an HTTP client it created itself. A client you pass in is yours to close, whether it's an http_client or a provider SDK client, such as openai_client, anthropic_client (including AsyncAnthropicVertex), Google's client, Hugging Face's hf_client, or xai_client. It's also kept when a run is on another event loop, where it can fail with the provider SDK's own error, such as RuntimeError: Event loop is closed.

Configuring the HTTP client

The HTTP clients Pydantic AI creates have a 600-second timeout (20 minutes for Google) with a 5-second connect timeout, and a connection pool of up to 1000 connections, of which up to 100 are kept alive while idle. These are the defaults of the OpenAI and Anthropic SDKs' own clients. To change them, create a client with [create_async_httpx2_client()][pydantic_ai.models.create_async_httpx2_client], which keeps the remaining defaults and Pydantic AI's User-Agent, and pass it to the provider as http_client:

import httpx2

from pydantic_ai import Agent
from pydantic_ai.models import create_async_httpx2_client
from pydantic_ai.models.openai import OpenAIChatModel
from pydantic_ai.providers.openai import OpenAIProvider


async def main():
    async with create_async_httpx2_client(
        timeout=httpx2.Timeout(120, connect=5, pool=10),
        limits=httpx2.Limits(max_connections=200, max_keepalive_connections=50),
    ) as http_client:
        model = OpenAIChatModel('{{ openai:medium.name }}', provider=OpenAIProvider(http_client=http_client))
        agent = Agent(model)
        result = await agent.run('What is the capital of France?')
        print(result.output)
        #> The capital of France is Paris.

Because you created the client, you close it, here by leaving the async with block. Passing the same client to several providers makes them share its connection pool. It also takes a transport, such as a retrying one: see A retrying client.

The Groq, Cohere and GitHub providers take a legacy httpx.AsyncClient instead, which you build yourself with the same arguments, for example httpx.AsyncClient(timeout=httpx.Timeout(120, connect=5), limits=httpx.Limits(max_connections=200, max_keepalive_connections=50)).

Custom Models

!!! note If a model API is compatible with the OpenAI API, you do not need a custom model class: use [OpenAICompatibleProvider][pydantic_ai.providers.openai_compatible.OpenAICompatibleProvider] with OpenAIChatModel or OpenAIResponsesModel instead, as Other endpoints explains.

To implement support for a model API that's not already supported, you will need to subclass the [Model][pydantic_ai.models.Model] abstract base class. For streaming, you'll also need to implement the [StreamedResponse][pydantic_ai.models.StreamedResponse] abstract base class.

A subclass with dataclass equality, including one that inherits it, compares its settings and profile along with its dataclass fields. A subclass that hashes by identity, such as a direct non-dataclass subclass of Model or a direct @dataclass(eq=False) subclass, compares by identity. Its repr shows its model_name, any dataclass fields, and its settings and profile when set. Define __eq__ or __repr__ on the subclass to replace either.

The best place to start is to review the source code for existing implementations, e.g. OpenAIChatModel.

For details on when we'll accept contributions adding new models to Pydantic AI, see the contributing guidelines.

HTTP Request Concurrency

You can limit the number of concurrent HTTP requests to a model using the [ConcurrencyLimitedModel][pydantic_ai.ConcurrencyLimitedModel] wrapper. This is useful for respecting rate limits or managing resource usage when running many agents in parallel.

import asyncio

from pydantic_ai import Agent, ConcurrencyLimitedModel

# Wrap a model with concurrency limiting
model = ConcurrencyLimitedModel('{{ openai:medium }}', limiter=5)

# Multiple agents can share this rate-limited model
agent = Agent(model)


async def main():
    # These will be rate-limited to 5 concurrent HTTP requests
    results = await asyncio.gather(
        *[agent.run(f'Question {i}') for i in range(20)]
    )
    print(len(results))
    #> 20

The limiter parameter accepts:

  • An integer for simple limiting (e.g., limiter=5)
  • A [ConcurrencyLimit][pydantic_ai.ConcurrencyLimit] for advanced configuration with backpressure control
  • A [ConcurrencyLimiter][pydantic_ai.ConcurrencyLimiter] for sharing limits across multiple models

Shared Concurrency Limits

To share a concurrency limit across multiple models (e.g., different models from the same provider), you can create a [ConcurrencyLimiter][pydantic_ai.ConcurrencyLimiter] and pass it to multiple ConcurrencyLimitedModel instances:

import asyncio

from pydantic_ai import Agent, ConcurrencyLimitedModel, ConcurrencyLimiter

# Create a shared limiter with a descriptive name
shared_limiter = ConcurrencyLimiter(max_running=10, name='openai-pool')

# Both models share the same concurrency limit
model1 = ConcurrencyLimitedModel('{{ openai:medium }}', limiter=shared_limiter)
model2 = ConcurrencyLimitedModel('{{ openai:small }}', limiter=shared_limiter)

agent1 = Agent(model1)
agent2 = Agent(model2)


async def main():
    # Total concurrent requests across both agents limited to 10
    results = await asyncio.gather(
        *[agent1.run(f'Question {i}') for i in range(10)],
        *[agent2.run(f'Question {i}') for i in range(10)],
    )
    print(len(results))
    #> 20

An agent and the ConcurrencyLimitedModel used for its own model request must use separate ConcurrencyLimiter instances. If you set Agent(max_concurrency=...) as well as ConcurrencyLimitedModel(limiter=...), using the same instance raises [UserError][pydantic_ai.exceptions.UserError]. Nested ConcurrencyLimitedModel wrappers also need different limiter instances.

Re-entering a ConcurrencyLimiter through an agent on the same task raises RuntimeError; use a separate limiter for the nested run. When an agent delegates to another agent through a tool, each run or model request acquires its own slot. A shared pool must have enough capacity for the parent and nested operation to run at the same time.

When instrumentation is enabled, requests waiting for a concurrency slot appear as spans with attributes showing the queue depth and configured limits. The name parameter on ConcurrencyLimiter helps identify shared limiters in traces.

Handling Model API Errors

When a request to a model provider fails, Pydantic AI raises a [ModelAPIError][pydantic_ai.exceptions.ModelAPIError] rather than the provider SDK's own exception, which remains available as __cause__. When the provider says what went wrong, the error is an instance of one of these categories, whichever provider it came from:

Exception Raised when
[ModelRateLimitError][pydantic_ai.exceptions.ModelRateLimitError] A rate limit was reached, e.g. an HTTP 429. Waiting and retrying may help.
[ModelQuotaExceededError][pydantic_ai.exceptions.ModelQuotaExceededError] The provider said the account's quota, credits, or billing is exhausted, e.g. an HTTP 402 or OpenAI's insufficient_quota. Waiting won't help.
[ModelUnavailableError][pydantic_ai.exceptions.ModelUnavailableError] The provider or model is temporarily unavailable, e.g. an HTTP 503 or a gRPC UNAVAILABLE sent by the provider.
[ModelOverloadedError][pydantic_ai.exceptions.ModelOverloadedError] The provider said it's unavailable because it's overloaded, e.g. an HTTP 529 or Anthropic's overloaded_error. A subclass of ModelUnavailableError.
[ModelServerError][pydantic_ai.exceptions.ModelServerError] The provider failed with another server error, e.g. an HTTP 500, 502, or 504.
[ModelConnectionError][pydantic_ai.exceptions.ModelConnectionError] The request could not reach the provider, its response could not be read, or the provider timed out the attempt (see ModelTimeoutError). Its [phase][pydantic_ai.exceptions.ModelConnectionError.phase] says whether the request may have reached the provider.
[ModelTimeoutError][pydantic_ai.exceptions.ModelTimeoutError] An attempt timed out at the transport layer, or the provider reported that it timed out the attempt, e.g. an HTTP 408 or Bedrock's ModelTimeoutException. A subclass of ModelConnectionError; a timeout the provider reports has no phase. A request_timeout deadline over all attempts raises [ModelRequestTimeout][pydantic_ai.exceptions.ModelRequestTimeout] instead.
[ModelRequestRejectedError][pydantic_ai.exceptions.ModelRequestRejectedError] The provider rejected the request itself, e.g. an HTTP 400, 413 or 422, so sending it again unchanged won't succeed.
[ModelContextWindowExceededError][pydantic_ai.exceptions.ModelContextWindowExceededError] The input exceeds the model's context window. A subclass of ModelRequestRejectedError.
[ModelAuthenticationError][pydantic_ai.exceptions.ModelAuthenticationError] The provider rejected the credentials, or doesn't allow them to make the request, e.g. an HTTP 401 or 403. A subclass of ModelRequestRejectedError.
[ModelNotFoundError][pydantic_ai.exceptions.ModelNotFoundError] The provider couldn't find the model, deployment or other resource the request names, e.g. an HTTP 404. A subclass of ModelRequestRejectedError.

A provider's own signal decides between categories that share a status code: a 429 is a rate limit unless the provider says the quota is exhausted (the Gemini API names the exceeded quotas in its RESOURCE_EXHAUSTED error, so its daily quota is a ModelQuotaExceededError), and a 503 is only unavailable unless the provider says it's overloaded. Anthropic's tier spend cap is a 429 too, but its enforced_spend_limit_reached error code makes it a ModelQuotaExceededError. A 400 is a ModelRequestRejectedError, or one of its subclasses when the provider says why: a ModelContextWindowExceededError, a ModelNotFoundError, or a ModelAuthenticationError (the Gemini API sends API_KEY_INVALID as a 400). Exhausted credits, a reached spend limit, or disabled billing are a ModelQuotaExceededError instead, even when the provider sends them as a 400, as Anthropic and the Gemini API do.

Other errors are raised as a plain ModelAPIError or ModelHTTPError, as they always were. Categories are assigned from the provider's error code or type where it sends one, and from the error message only where the provider offers nothing else. Every ModelAPIError also exposes the provider's [provider_error_code][pydantic_ai.exceptions.ModelAPIError.provider_error_code], [provider_error_type][pydantic_ai.exceptions.ModelAPIError.provider_error_type], and error [body][pydantic_ai.exceptions.ModelAPIError.body], when available.

Embedding models raise the same errors.

An error is reported the same way whether the provider sent it before a stream opened or inside an already open stream. For example, both an HTTP 503 from Bedrock and a serviceUnavailableException in a Bedrock stream raise a ModelHTTPError with status 503 that is also a ModelUnavailableError. The second has [in_stream][pydantic_ai.exceptions.ModelAPIError.in_stream] set, so you can still tell them apart.

This lets you decide what to do about a failure without knowing which provider it came from:

from pydantic_ai import Agent, ModelContextWindowExceededError, ModelRateLimitError

agent = Agent('{{ openai:medium }}')

try:
    result = agent.run_sync('What is the capital of France?', message_history=history)
except ModelRateLimitError as exc:
    raise MyRateLimitException('AI service is rate-limited. Try again shortly.', retry_after=exc.retry_after)
except ModelContextWindowExceededError:
    result = agent.run_sync('What is the capital of France?', message_history=summarize(history))

[retry_after][pydantic_ai.exceptions.ModelAPIError.retry_after] is the number of seconds the provider asked you to wait, as a float, or None if it didn't say.

See Compaction for keeping a conversation under the context window in the first place, which is preferable to waiting for ModelContextWindowExceededError: model performance usually degrades well before the hard limit.

The categories also tell a failure that may go away apart from one that won't. If you retry model requests yourself, skip the ones that sending the same request to the same model again can't fix:

from pydantic_ai import (
    ModelAPIError,
    ModelAuthenticationError,
    ModelHTTPError,
    ModelQuotaExceededError,
    ModelRateLimitError,
    ModelRequestRejectedError,
)


def should_retry(error: ModelAPIError) -> bool:
    return not isinstance(error, ModelRequestRejectedError | ModelQuotaExceededError)


print(should_retry(ModelHTTPError.for_category(ModelAuthenticationError, status_code=401, model_name='my-model')))
#> False
print(should_retry(ModelHTTPError.for_category(ModelRateLimitError, status_code=429, model_name='my-model')))
#> True

[ModelAuthenticationError][pydantic_ai.exceptions.ModelAuthenticationError], [ModelNotFoundError][pydantic_ai.exceptions.ModelNotFoundError] and [ModelContextWindowExceededError][pydantic_ai.exceptions.ModelContextWindowExceededError] are all a ModelRequestRejectedError, so should_retry skips them too. Under Temporal, the activity doesn't retry them either, as they're in non_retryable_model_errors by default.

HTTP errors

When a provider returns a 4xx or 5xx response, the error is a [ModelHTTPError][pydantic_ai.exceptions.ModelHTTPError], and also an instance of the error category above that it belongs to, if any. The exception exposes the [status_code][pydantic_ai.exceptions.ModelHTTPError.status_code], the response [body][pydantic_ai.exceptions.ModelAPIError.body], and the provider's response headers via the [headers][pydantic_ai.exceptions.ModelHTTPError.headers] attribute (a dict[str, str] with lowercase keys, or None for providers that don't surface headers, such as gRPC-based providers). Its [retry_after][pydantic_ai.exceptions.ModelAPIError.retry_after] is parsed from the retry-after-ms or Retry-After header, handling both the integer delta-seconds and HTTP-date formats, or, for a Google error without them, from the RetryInfo in its body, and [provider_retry_hint][pydantic_ai.exceptions.ModelHTTPError.provider_retry_hint] from the x-should-retry header that OpenAI and Anthropic send to say whether retrying may help. A status a retrying transport gave up on carries its body and headers too.

When OpenAI, Anthropic, the Google Gemini API, Amazon Bedrock, or Groq reports that a requested model identifier is unavailable, Pydantic AI adds a close known match to the error message when one exists. The suggestion is also available as [suggested_model_id][pydantic_ai.exceptions.ModelHTTPError.suggested_model_id]. This is best-effort guidance after the provider rejects a request, not local validation: unknown model identifiers remain valid so custom deployments and newly released models continue to work.

When a provider error has a known fix outside the request, such as the data retention mode Amazon Bedrock requires for some models, Pydantic AI appends guidance to the error message. It is also available as [hint][pydantic_ai.exceptions.ModelHTTPError.hint].

!!! note Some ModelHTTPErrors don't come with an HTTP status of their own, and get the status the provider uses for the same error instead, so they're handled the same way:

- xAI's gRPC status codes are mapped to their HTTP equivalents, like `RESOURCE_EXHAUSTED` to 429. These have
  `headers=None`. A gRPC error the client raised itself, because it couldn't connect, lost the connection, or
  reached its `timeout`, is a `ModelConnectionError` or `ModelTimeoutError` instead, as with other providers.
- OpenRouter errors parsed from a 200-OK response report the error's own `code`.
- An error sent inside an already open stream gets the status of the same error before the stream opens, like
  529 for Anthropic's `overloaded_error`. These have
  [`in_stream`][pydantic_ai.exceptions.ModelAPIError.in_stream] set, and their `headers`, if any, are the
  stream's.
  A request can be streamed without you asking for it (e.g. when the agent has an event stream handler), which
  is why the error doesn't otherwise depend on it.

A custom model can raise the same errors with [ModelHTTPError.for_category][pydantic_ai.exceptions.ModelHTTPError.for_category], which builds a ModelHTTPError that is also an instance of the given category, so code that handles either catches it. Its class is named after the category, so tracebacks, repr(), OpenTelemetry spans and [failed_attempts][pydantic_ai.messages.ModelResponse.failed_attempts] show it as, say, ModelRateLimitError:

from pydantic_ai import ModelHTTPError, ModelRateLimitError


def map_error(status_code: int, body: object) -> ModelHTTPError:
    if status_code == 429:
        return ModelHTTPError.for_category(ModelRateLimitError, status_code=429, model_name='my-model', body=body)
    return ModelHTTPError(status_code, 'my-model', body)


error = map_error(429, {'error': 'Too many requests'})
print(isinstance(error, ModelHTTPError), isinstance(error, ModelRateLimitError))
#> True True

Fallback Model

To make an agent fall back to other models when a provider fails, add the ModelFallback capability. The agent's model is attempted first, then each of the capability's models in turn, until one succeeds:

from pydantic_ai import Agent
from pydantic_ai.capabilities import ModelFallback

agent = Agent('{{ openai:medium }}', capabilities=[ModelFallback('{{ anthropic:medium }}')])

ModelFallback makes each attempt within the agent's own request loop, so capabilities see the model that will serve each attempt and prepare the request for it, each attempt is batched on its own model, and under durable execution each attempt is a durable model operation of its own. See Model Fallback for how it works, and Migrating from FallbackModel to move an agent over. Recovery scopes compared sets fallback beside retrying the same model and the other ways a request is attempted again.

The rest of this section covers [FallbackModel][pydantic_ai.models.fallback.FallbackModel], which remains the way to fall back with direct model requests or a standalone model, outside an agent. Its fallback_on policies, failed attempts and telemetry work the same way under ModelFallback, so the handler examples below apply to both.

You can use [FallbackModel][pydantic_ai.models.fallback.FallbackModel] to attempt multiple models in sequence until one succeeds. Pydantic AI can switch to the next model when the current model raises an exception (like a 4xx/5xx API error) or when the response content indicates a semantic failure (like a truncated response or a failed native tool call).

By default, fallback triggers on [ModelAPIError][pydantic_ai.exceptions.ModelAPIError] (4xx/5xx API errors, and connection failures or timeouts before the response starts), so you don't need to configure anything for the most common use case. That includes every error category: rate limits, exhausted quota, unavailable or overloaded providers, server errors, connection failures and timeouts, and rejected requests, including context window overflows: another model or provider may accept a request, credentials or model name the first one rejected, and a model with a larger context window may accept an input that overflowed another model's. To fall back only on some of them, pass those categories as fallback_on, e.g. fallback_on=(ModelRateLimitError, ModelUnavailableError, ModelServerError).

This behavior is controlled by the fallback_on parameter (see [FallbackModel][pydantic_ai.models.fallback.FallbackModel]), which accepts exception types, exception handlers, and response handlers — all of which can be sync or async.

!!! note The provider SDKs on which Models are based (like OpenAI, Anthropic, etc.) often have built-in retry logic that can delay the FallbackModel from activating.

When using `FallbackModel`, it's recommended to disable provider SDK retries to ensure immediate fallback, by setting [`ModelSettings['max_retries']`](../retries.md#setting-the-sdk-retry-count-per-request) to `0` on the agent, or on each candidate model but the last. Bedrock and xAI ignore it and warn with [`UnsupportedSettingWarning`][pydantic_ai.exceptions.UnsupportedSettingWarning], so with one of those as a candidate, set it on the other candidates. See [The layers](../retries.md#the-layers) in the retries guide for the full retry map.

To bound how long the fallback can be delayed instead, set [`request_timeout`](../timeouts.md#model-request-deadlines) in the model settings: each model gets that long across all of its attempts, and when it runs out the resulting [`ModelRequestTimeout`][pydantic_ai.exceptions.ModelRequestTimeout] triggers the fallback to the next model, which gets a fresh deadline. On a streamed request, [`first_event_timeout`](../timeouts.md#waiting-for-the-first-event) does the same for the wait until a model starts producing output, without cutting off a long response once it has.

In the following example, the agent first makes a request to the OpenAI model (which fails due to an invalid API key), and then falls back to the Anthropic model.

from pydantic_ai import Agent
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.models.fallback import FallbackModel
from pydantic_ai.models.openai import OpenAIChatModel

openai_model = OpenAIChatModel('{{ openai:medium.name }}')
anthropic_model = AnthropicModel('{{ anthropic:medium.name }}')
fallback_model = FallbackModel(openai_model, anthropic_model)

agent = Agent(fallback_model)
response = agent.run_sync('What is the capital of France?')
print(response.output)
#> The capital of France is Paris.

print(response.all_messages())
"""
[
    ModelRequest(
        parts=[
            UserPromptPart(
                content='What is the capital of France?',
                timestamp=datetime.datetime(...),
            )
        ],
        timestamp=datetime.datetime(...),
        run_id='...',
        conversation_id='...',
    ),
    ModelResponse(
        parts=[TextPart(content='The capital of France is Paris.')],
        usage=RequestUsage(cost=Decimal('0.000182'), input_tokens=56, output_tokens=7),
        model_name='{{ anthropic:medium.name }}',
        model_id='fallback:{{ openai:medium }},{{ anthropic:medium }}',
        timestamp=datetime.datetime(...),
        run_id='...',
        conversation_id='...',
        failed_attempts=[
            ModelRequestAttempt(
                model_name='{{ openai:medium.name }}',
                provider_name='openai',
                outcome='error',
                error="ModelHTTPError: status_code: 401, model_name: {{ openai:medium.name }}, body: {'error': 'Invalid API Key'}",
                timestamp=datetime.datetime(...),
                duration=datetime.timedelta(...),
            )
        ],
    ),
]
"""

The ModelResponse message above indicates in the model_name field that the output was returned by the Anthropic model, which is the second model specified in the FallbackModel.

!!! note Each model's options should be configured individually. For example, base_url, api_key, and custom clients should be set on each model itself, not on the FallbackModel.

Per-Model Settings

You can configure different [ModelSettings][pydantic_ai.settings.ModelSettings] for each model in a fallback chain by passing the settings parameter when creating each model. This is particularly useful when different providers have different optimal configurations:

from pydantic_ai import Agent, ModelSettings
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.models.fallback import FallbackModel
from pydantic_ai.models.openai import OpenAIChatModel

# Configure each model with provider-specific optimal settings
openai_model = OpenAIChatModel(
    '{{ openai:medium.name }}',
    settings=ModelSettings(thinking='high', max_tokens=2000)  # More reasoning for OpenAI
)
anthropic_model = AnthropicModel(
    '{{ anthropic:medium.name }}',
    settings=ModelSettings(thinking='low', max_tokens=1000)  # A quicker fallback
)

fallback_model = FallbackModel(openai_model, anthropic_model)
agent = Agent(fallback_model)

result = agent.run_sync('Write a creative story about space exploration')
print(result.output)
"""
In the year 2157, Captain Maya Chen piloted her spacecraft through the vast expanse of the Andromeda Galaxy. As she discovered a planet with crystalline mountains that sang in harmony with the cosmic winds, she realized that space exploration was not just about finding new worlds, but about finding new ways to understand the universe and our place within it.
"""

In this example, if the OpenAI model fails, the agent will automatically fall back to the Anthropic model with its own configured settings. The FallbackModel itself doesn't have settings - it uses the individual settings of whichever model successfully handles the request.

Exception Handling

The next example demonstrates the exception-handling capabilities of FallbackModel. If all models fail, a [FallbackExceptionGroup][pydantic_ai.exceptions.FallbackExceptionGroup] is raised, which contains all the exceptions encountered during the run execution.

from pydantic_ai import Agent, ModelAPIError
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.models.fallback import FallbackModel
from pydantic_ai.models.openai import OpenAIChatModel

openai_model = OpenAIChatModel('{{ openai:medium.name }}')
anthropic_model = AnthropicModel('{{ anthropic:medium.name }}')
fallback_model = FallbackModel(openai_model, anthropic_model)

agent = Agent(fallback_model)
try:
    response = agent.run_sync('What is the capital of France?')
except* ModelAPIError as exc_group:
    for exc in exc_group.exceptions:
        print(exc)

By default, the FallbackModel only moves on to the next model if the current model raises a [ModelAPIError][pydantic_ai.exceptions.ModelAPIError], which includes [ModelHTTPError][pydantic_ai.exceptions.ModelHTTPError]. You can customize this behavior by passing a custom fallback_on argument to the FallbackModel constructor.

!!! note Validation errors (from structured output or tool parameters) do not trigger fallback. These errors use the retry mechanism instead, which re-prompts the same model to try again. This is intentional: validation errors stem from the non-deterministic nature of LLMs and may succeed on retry, whereas API errors (4xx/5xx) generally indicate issues that won't resolve by retrying the same request.

Context Window Overflow

FallbackModel also falls back when a model raises [ModelContextWindowExceededError][pydantic_ai.exceptions.ModelContextWindowExceededError], since a later model in the chain may have a larger context window. If the whole chain fails, the overflow is in the [FallbackExceptionGroup][pydantic_ai.exceptions.FallbackExceptionGroup], where except* ModelContextWindowExceededError reaches it. If none of your models has a larger window, you can avoid sending the oversized request to each of them by excluding the overflow from fallback_on, so it propagates as is:

from pydantic_ai import Agent, ModelContextWindowExceededError, ModelAPIError
from pydantic_ai.models.fallback import FallbackModel


def fallback_on(exc: Exception) -> bool:
    return isinstance(exc, ModelAPIError) and not isinstance(exc, ModelContextWindowExceededError)


agent = Agent(FallbackModel('{{ openai:medium }}', '{{ anthropic:medium }}', fallback_on=fallback_on))

try:
    result = agent.run_sync('What is the capital of France?', message_history=history)
except ModelContextWindowExceededError:
    result = agent.run_sync('What is the capital of France?', message_history=summarize(history))

Failed attempts and usage

The response that answers lists every attempt the FallbackModel moved on from in [failed_attempts][pydantic_ai.messages.ModelResponse.failed_attempts], in order. Each [ModelRequestAttempt][pydantic_ai.messages.ModelRequestAttempt] records the model and provider it tried, whether it raised an error (with the error as 'ExceptionType: message') or returned a response a response handler rejected, when it started and how long it took, and the usage the provider reported for it. An attempt that raised has no usage, as the provider reported none. When every model fails, the same attempts are on the [FallbackExceptionGroup][pydantic_ai.exceptions.FallbackExceptionGroup]'s attempts.

A rejected response was still generated and billed, so its tokens and cost are added to the run's [RunUsage][pydantic_ai.usage.RunUsage] and count towards the token and cost limits in [UsageLimits][pydantic_ai.usage.UsageLimits]. They stay on the attempt rather than being folded into the answering response's usage, and the rejected response itself is not added to the message history. A fallback attempt isn't a response the agent acted on, so it doesn't count towards [request_limit][pydantic_ai.usage.UsageLimits.request_limit].

The rejected responses' usage counts when every model fails, too. If it exceeds a limit, [UsageLimitExceeded][pydantic_ai.exceptions.UsageLimitExceeded] is raised with the FallbackExceptionGroup as its __cause__.

The rejected responses' usage also counts when the request ends another way: a later model raises an error that fallback_on doesn't cover, the run is cancelled or times out.

!!! note Under durable execution, a FallbackModel runs inside the durable model operation, and the error that ends its request reaches the workflow without its attempts, so the usage of the responses it rejected is only recorded when another model answers. ModelFallback makes each attempt a durable operation of its own and records the attempts on the workflow side, so it doesn't have this limitation.

from pydantic_ai import Agent, ModelMessage, ModelResponse, OutputTokenLimitExceeded, TextPart
from pydantic_ai.models.fallback import FallbackModel
from pydantic_ai.models.function import AgentInfo, FunctionModel
from pydantic_ai.usage import RequestUsage


def truncated(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
    return ModelResponse(
        parts=[TextPart('The capital of France is')],
        usage=RequestUsage(input_tokens=60, output_tokens=100),
        finish_reason='length',
    )


def complete(messages: list[ModelMessage], info: AgentInfo) -> ModelResponse:
    return ModelResponse(
        parts=[TextPart('The capital of France is Paris.')],
        usage=RequestUsage(input_tokens=60, output_tokens=8),
    )


fallback_model = FallbackModel(
    FunctionModel(truncated, model_name='primary'),
    FunctionModel(complete, model_name='backup'),
    fallback_on=OutputTokenLimitExceeded,
)
agent = Agent(fallback_model)
result = agent.run_sync('What is the capital of France?')
print(result.output)
#> The capital of France is Paris.
print(result.usage)
#> RunUsage(input_tokens=120, output_tokens=108, requests=1)
print(result.response.usage)
#> RequestUsage(input_tokens=60, output_tokens=8)
for attempt in result.response.failed_attempts or []:
    print(attempt.model_name, attempt.outcome, attempt.usage)
    #> primary rejected RequestUsage(input_tokens=60, output_tokens=100)

When the agent is instrumented, the model request (chat) span describes the model that answered and ends OK. Each attempt the FallbackModel moved on from gets its own child span, model request attempt <model name>, which ends with an ERROR status, the way a failed tool call gets its own span inside a successful agent run. An error is recorded on that span as an exception, and a response rejected by a response handler, or as a refusal, carries its usage, cost and finish reason instead, as a chat span would. Each attempt span names the model it tried in its gen_ai.provider.name and gen_ai.request.model attributes, and gives its zero-based position among the request's attempts in pydantic_ai.model_request.attempt. With include_content=False, the exception keeps only its type. The attempt span is created once the attempt has failed, so spans the tried model opens itself, such as a decision model's decide span, sit beside it under the chat span rather than inside it.

Response-Based Fallback

In addition to exception-based fallback, you can also trigger fallback based on the content of a model's response. This is useful when a model returns a successful HTTP response (no exception), but the response content indicates a semantic failure — for example, an unexpected finish reason or a native tool reporting failure.

!!! note "Non-streaming only" Response-based fallback only works with non-streamed requests. A streamed response has already reached you by the time it could be judged, so response handlers aren't applied to it: it's accepted as is, and a UserWarning is emitted. Exception-based fallback still applies until the stream produces its first event, including to errors a provider reports in the stream before any output, like an overload after the request was accepted. An error after that reaches you, since output has already been streamed.

A request is streamed whenever something consumes the run's events: [`agent.run_stream()`][pydantic_ai.agent.AbstractAgent.run_stream], [`agent.run_stream_events()`][pydantic_ai.agent.AbstractAgent.run_stream_events] (which the [UI adapters](../ui/overview.md) use), [`agent.iter()`][pydantic_ai.agent.Agent.iter] with `node.stream()`, and [`agent.run()`][pydantic_ai.agent.AbstractAgent.run] or [`agent.run_sync()`][pydantic_ai.agent.AbstractAgent.run_sync] with an `event_stream_handler`, an [`agent.on_event`][pydantic_ai.agent.Agent.on_event] listener, or a capability that reacts to events, like the [event stream hooks](../hooks.md#event-stream-hooks).

The fallback_on parameter accepts:

  • A tuple of exception types: (ModelAPIError, ModelHTTPError)
  • An exception handler (sync or async): lambda exc: isinstance(exc, MyError)
  • A response handler (sync or async): def check(r: ModelResponse) -> bool
  • A list mixing all of the above: [ModelAPIError, exc_handler, response_handler]

Handler type is auto-detected by inspecting type hints on the first parameter. If the first parameter is hinted as [ModelResponse][pydantic_ai.messages.ModelResponse], it's a response handler. Otherwise (including untyped handlers and lambdas), it's an exception handler.

As the hints are resolved at runtime, every annotated type in the handler signature must be imported at runtime rather than only under if TYPE_CHECKING:. If any annotation can't be resolved, a [UserError][pydantic_ai.exceptions.UserError] is raised instead of the handler being silently treated as an exception handler.

Finish Reason Example

A simple use case is checking the model's finish reason — for example, falling back if the response was truncated due to length limits:

from pydantic_ai import Agent
from pydantic_ai.messages import FinishReason, ModelResponse
from pydantic_ai.models.fallback import FallbackModel


def bad_finish_reason(response: ModelResponse) -> bool:
    """Fallback if the model stopped due to length limit, content filter, or error."""
    reason: FinishReason | None = response.finish_reason
    # Trigger fallback for problematic finish reasons
    return reason in ('length', 'content_filter', 'error')


fallback_model = FallbackModel(
    '{{ openai:medium }}',
    '{{ anthropic:medium }}',
    fallback_on=bad_finish_reason,
)

agent = Agent(fallback_model)
result = agent.run_sync('What is the capital of France?')
print(result.output)
#> The capital of France is Paris.

!!! warning "Solo response handlers replace default exception fallback" When you pass a single response handler as fallback_on (as above), it replaces the default (ModelAPIError,) exception fallback entirely. This means API errors (4xx/5xx) will propagate as exceptions instead of triggering fallback to the next model.

To keep exception-based fallback alongside a response handler, pass them together as a list — see the [mixed example below](#combining-handlers).

!!! note The agent loop only acts on a finish reason when the response has no actionable output. A 'length' finish reason on an empty or thinking-only response raises [OutputTokenLimitExceeded][pydantic_ai.exceptions.OutputTokenLimitExceeded] (typically the model hit the token limit mid-thinking), and an empty or thinking-only response with a 'content_filter' finish reason raises [ContentFilterError][pydantic_ai.exceptions.ContentFilterError]. Other empty or thinking-only responses are re-prompted, up to the output retry limit. Non-empty responses are handled normally regardless of finish reason: a function tool call truncated mid-arguments is re-prompted like any other invalid-arguments failure, and only once its retry budget is exhausted does it surface as [IncompleteToolCall][pydantic_ai.exceptions.IncompleteToolCall] (a subclass of OutputTokenLimitExceeded, itself an UnexpectedModelBehavior). The exceptions are output that was cut off at 'length' into JSON that doesn't parse: an output tool call raises IncompleteToolCall right away, and [NativeOutput][pydantic_ai.output.NativeOutput] or [PromptedOutput][pydantic_ai.output.PromptedOutput] text raises OutputTokenLimitExceeded right away.

All of these are raised from the agent loop, after `model.request()` has already returned
successfully, so an exception-based `fallback_on` can't catch them: not the default
`fallback_on=(ModelAPIError,)`, which wouldn't match anyway, as `ContentFilterError` inherits from
`UnexpectedModelBehavior`, not [`ModelAPIError`][pydantic_ai.exceptions.ModelAPIError]. The exceptions are
`ContentFilterError` and `OutputTokenLimitExceeded` themselves: naming `ContentFilterError` in `fallback_on`
rejects any response with a `'content_filter'` finish reason, and naming `OutputTokenLimitExceeded` any
response with a `'length'` finish reason, empty or not, so the next model is tried instead, as
[`ModelFallback`](../capabilities/model-fallback.md#when-to-fall-back) does by default. To fall back on another bad finish reason instead,
use a response handler (see the [example above](#finish-reason-example)) that inspects
[`finish_reason`][pydantic_ai.messages.ModelResponse.finish_reason]: it rejects the response inside
`FallbackModel` before the agent loop sees it, so the next model is tried instead of an exception
being raised. Note that it rejects *every* response with that finish reason, including non-empty
ones the agent loop would have accepted. To instead raise on `content_filter` responses that still
carry partial or refusal text, add the
[`RaiseContentFilterError`][pydantic_ai.capabilities.RaiseContentFilterError] capability.
[Falling back when a model refuses](../capabilities/model-fallback.md#refusals) puts this together
for safety refusals, and [Falling back when the output is cut off](../capabilities/model-fallback.md#token-limit)
for the token limit.

Native Tool Failure Example

A more complex use case is when using native tools like web search or URL fetching. For example, Google's native web fetch, enabled with the WebFetch capability, may return a successful response with a status indicating the URL fetch failed. This example uses the ModelFallback capability, which takes the same fallback_on:

from pydantic_ai import Agent
from pydantic_ai.capabilities import ModelFallback, WebFetch
from pydantic_ai.messages import ModelResponse
from pydantic_ai.models.anthropic import AnthropicModel
from pydantic_ai.models.google import GoogleModel


def web_fetch_failed(response: ModelResponse) -> bool:
    """Check if a web_fetch native tool failed to retrieve content."""
    for call, result in response.native_tool_calls:
        if call.tool_name != 'web_fetch':
            continue
        if not isinstance(result.content, list):
            continue
        for item in result.content:
            if isinstance(item, dict):
                status = item.get('url_retrieval_status', '')
                if status and status != 'URL_RETRIEVAL_STATUS_SUCCESS':
                    return True
    return False


google_model = GoogleModel('{{ google:medium.name }}')
anthropic_model = AnthropicModel('{{ anthropic:medium.name }}')

agent = Agent(
    google_model,
    capabilities=[
        WebFetch(),
        # Auto-detected as response handler via type hint
        ModelFallback(anthropic_model, fallback_on=web_fetch_failed),
    ],
)

# If Google's web_fetch fails, automatically falls back to Anthropic
result = agent.run_sync('Summarize https://ai.pydantic.dev')
print(result.output)
"""
Pydantic AI is a Python agent framework for building production-grade LLM applications.
"""

Response handlers receive the [ModelResponse][pydantic_ai.messages.ModelResponse] returned by the model and should return True to trigger fallback to the next model, or False to accept the response.

Rejecting Responses Without Actionable Output

Without a handler, an empty or thinking-only response is re-prompted on the same model, up to the output retry limit (see the note above). A response handler can send it to the backup model instead, by rejecting a successful response that contains no text or tool call. This predicate is for a text-output agent; if your output type accepts other response parts, include those parts in the predicate. Include [ModelAPIError][pydantic_ai.exceptions.ModelAPIError] in the handler list so API errors still fall back; see Combining Handlers.

from pydantic_ai import (
    Agent,
    ModelAPIError,
    ModelMessage,
    ModelResponse,
    NativeToolCallPart,
    TextPart,
    ToolCallPart,
)
from pydantic_ai.models.fallback import FallbackModel
from pydantic_ai.models.function import AgentInfo, FunctionModel


def empty(_messages: list[ModelMessage], _info: AgentInfo) -> ModelResponse:
    return ModelResponse(parts=[TextPart('')])


def answer(_messages: list[ModelMessage], _info: AgentInfo) -> ModelResponse:
    return ModelResponse(parts=[TextPart('The capital of France is Paris.')])


def has_no_actionable_output(response: ModelResponse) -> bool:
    return not any(
        (isinstance(part, TextPart) and part.has_content())
        or isinstance(part, (ToolCallPart, NativeToolCallPart))
        for part in response.parts
    )


agent = Agent(
    # These deterministic models simulate an empty primary response and a backup answer.
    FallbackModel(
        FunctionModel(empty, model_name='primary'),
        FunctionModel(answer, model_name='backup'),
        fallback_on=[ModelAPIError, has_no_actionable_output],
    ),
    output_type=str,
)
result = agent.run_sync('What is the capital of France?')
print(result.output)
#> The capital of France is Paris.

Combining Handlers

You can combine exception types, exception handlers, and response handlers in a single list:

from pydantic_ai.exceptions import ModelAPIError
from pydantic_ai.models.fallback import FallbackModel

from fallback_on_native_tool import anthropic_model, google_model, web_fetch_failed

fallback_model = FallbackModel(
    google_model,
    anthropic_model,
    fallback_on=[
        ModelAPIError,  # Exception type
        lambda exc: 'rate limit' in str(exc).lower(),  # Exception handler (untyped lambda)
        web_fetch_failed,  # Response handler (auto-detected via type hint)
    ],
)

Exception Handling in Middleware and Decorators

When using FallbackModel, it's important to understand that [FallbackExceptionGroup][pydantic_ai.exceptions.FallbackExceptionGroup] inherits from Python's ExceptionGroup. This means that existing exception handling code that catches specific exceptions (like ModelAPIError) won't automatically catch the individual exceptions wrapped inside the group.

For example, if you have middleware or a decorator that catches ModelAPIError:

from collections.abc import Callable
from functools import wraps
from typing import TypeVar

from pydantic_ai import ModelAPIError

T = TypeVar('T')


# This handler will NOT catch ModelAPIError when using FallbackModel!
def handle_api_errors(func: Callable[..., T]) -> Callable[..., T]:
    @wraps(func)
    def wrapper(*args, **kwargs) -> T:
        try:
            return func(*args, **kwargs)
        except ModelAPIError as e:  # Won't catch FallbackExceptionGroup
            print(f'API error: {e}')
            raise

    return wrapper

This decorator will miss ModelAPIError exceptions when using FallbackModel, because they're wrapped in a FallbackExceptionGroup containing one exception per failed model, in the order the models were tried.

To handle both cases, you can use Python 3.11+ except* syntax, which catches matching exceptions from exception groups as well as bare exceptions. Note that except* always delivers the caught exceptions as an ExceptionGroup (even if the original was a bare exception), so re-raising will propagate an ExceptionGroup rather than the original exception type:

from collections.abc import Callable
from functools import wraps
from typing import TypeVar

from pydantic_ai import ModelAPIError

T = TypeVar('T')


def handle_api_errors(func: Callable[..., T]) -> Callable[..., T]:
    @wraps(func)
    def wrapper(*args, **kwargs) -> T:
        try:
            return func(*args, **kwargs)
        except* ModelAPIError as exc_group:
            for exc in exc_group.exceptions:
                print(f'API error: {exc}')
            raise

    return wrapper

You can also catch FallbackExceptionGroup directly if you want to handle it specifically:

from pydantic_ai import Agent, FallbackExceptionGroup
from pydantic_ai.models.fallback import FallbackModel

agent = Agent(FallbackModel('{{ openai:small }}', '{{ anthropic:medium }}'))

try:
    response = agent.run_sync('What is the capital of France?')
except FallbackExceptionGroup as exc_group:
    print(f'All {len(exc_group.exceptions)} models failed:')
    for exc in exc_group.exceptions:
        print(f'  - {exc}')