pydantic_ai.exceptions
Bases: UserWarning
Warning emitted when a deprecated Pydantic AI API is used.
Inherits from UserWarning instead of DeprecationWarning so that
deprecations are visible by default at runtime, following the approach
described in https://sethmlarson.dev/deprecations-via-warnings-dont-work-for-python-libraries.
Bases: Warning
Warning raised when cost calculation fails.
Bases: Warning
Warning raised when usage extraction fails.
Bases: Warning
Warning raised when cost is not found.
Bases: Warning
Warning raised when some responses omit usage while a token or cost limit is configured.
Bases: Exception
Exception to raise to request a model retry.
Can be raised from tool functions, output validators, and capability hooks
(such as after_model_request, after_tool_execute, etc.) to send
a retry prompt back to the model asking it to try again.
For a terminal failure the model should see but not retry, raise
ToolFailed instead.
The message to return to the model.
Type: str Default: message
@classmethod
def __get_pydantic_core_schema__(cls, _: Any, __: Any) -> core_schema.CoreSchema
Pydantic core schema to allow ModelRetry to be (de)serialized.
core_schema.CoreSchema
Bases: Exception
Exception to raise to report a terminal tool failure to the model.
Raise this when a tool call is done and has failed — a missing resource, an unsupported operation, a definitive upstream error — and you want the model to see the failure and adapt rather than try the same call again. Can be raised from tool functions, args validators, and tool validation/execution hooks.
Like ModelRetry, this produces a failed tool result the
model sees; unlike ModelRetry it does not prepend retry/correction instructions and does not
consume the tool’s retry budget. Bound repeated failures with
UsageLimits at the run level instead.
The failure message to return to the model.
Type: str Default: message
@classmethod
def __get_pydantic_core_schema__(cls, _: Any, __: Any) -> core_schema.CoreSchema
Pydantic core schema to allow ToolFailed to be (de)serialized.
core_schema.CoreSchema
Bases: Exception
Exception to raise when a tool call should be deferred.
See tools docs for more information.
Optional dictionary of metadata to attach to the deferred tool call.
This metadata will be available in DeferredToolRequests.metadata keyed by tool_call_id.
kind : Literal[‘external’, ‘question’] Default: 'external'
'question' when the call asks for content, such as an answer from the user, rather than
standing for work done outside the agent. Read back with DeferredToolRequests.kind().
Bases: Exception
Exception to raise when a tool call requires human-in-the-loop approval.
See tools docs for more information.
Optional dictionary of metadata to attach to the deferred tool call.
This metadata will be available in DeferredToolRequests.metadata keyed by tool_call_id.
Bases: Exception
Exception to raise in before/wrap model request hooks to skip the model call.
The provided response will be used instead of calling the model.
Note: when raised in before_model_request, any message history modifications
made by earlier capabilities in that hook will not be persisted to the agent’s
message history, since the request preparation is aborted.
Bases: Exception
Exception to raise in model request hooks to attempt the request again.
Raise from on_model_request_error
to retry after a failed attempt, from
after_model_request to reject
a response the model did return, or from
prepare_model_request to send
the attempt to another model before anything is sent. In each case the agent stays on the same
request step: no retry prompt is added to the history, and the model never learns that an earlier
attempt happened. That is what distinguishes it from ModelRetry,
which asks the model itself to try again. before_model_request and wrap_model_request run before
any attempt, so raising it from them is a UserError.
Pass model to attempt a different model, or omit it to attempt the same one again:
from pydantic_ai import ModelResponse, RetryModelRequest, RunContext
from pydantic_ai.capabilities import AbstractCapability
from pydantic_ai.exceptions import ModelOverloadedError
from pydantic_ai.models import ModelRequestContext
class RetryOnOverload(AbstractCapability[None]):
async def on_model_request_error(
self, ctx: RunContext[None], *, request_context: ModelRequestContext, error: Exception
) -> ModelResponse:
if not isinstance(error, ModelOverloadedError):
raise error
if request_context.attempt == 1:
raise RetryModelRequest() # the same model again
raise RetryModelRequest('anthropic:claude-fable-5-1') # a different model
Each attempt re-runs
prepare_model_request for
the model that is about to serve it, so preparation a capability does there for that model is
redone rather than inherited from the previous attempt, as are the model’s own message preparation
and token counting. An attempt at another model than the step’s also resolves the instructions
and model settings again, with that model as ctx.model.
before_model_request and wrap_model_request are not re-run: they belong to the request as a
whole, not to a single attempt.
The model to attempt next, or None to attempt the current one again.
Type: Model | KnownModelName | str | None Default: model
Bases: Exception
Exception to raise in before/wrap tool validate hooks to skip validation.
The provided args will be used as the validated arguments.
Bases: Exception
Exception to raise in before/wrap tool execute hooks to skip execution.
The provided result will be used as the tool result.
Bases: RuntimeError
Error caused by a usage mistake by the application developer — You!
Description of the mistake.
Type: str Default: message
Bases: UserError
Error that used to be raised when an agent run ended with messages still queued via enqueue.
A bare async for node in agent_run loop used to skip the node hooks, so 'when_idle'
messages and end-of-run redirects (which drain in after_node_run) were stranded. Bare
iteration now advances through AgentRun.next() like every
other way of driving a run, so pending messages always drain and this error is no longer
raised. It is kept so existing except clauses keep working.
Bases: RuntimeError
Base class for errors occurring during an agent run.
The error message.
Type: str Default: message
The conversation of the run that raised this, with the messages it got to, so it can still be stored.
It includes a partial response with state='interrupted', and saving it to a
ConversationStore appends just what the run
added. It’s set once the error leaves the run, iter or run_stream call or async with block,
so it’s None when caught inside an iter or run_stream block, if the error didn’t come out of
an Agent run, or if it came out of a realtime session. To get the
conversation there, or for an exception raised by a tool’s own code, use
capture_run_conversation.
Type: Conversation | None
Bases: AgentRunError
Raised when the agent run was cancelled by the application itself.
Raised by AgentRun.cancel() and
RunContext.cancel(), and as its subclass
RunTimedOut when a run’s timeout= expires.
This is a normal, catchable application-level outcome: the run stopped because your own code
asked it to. External cancellation of the task running the agent (asyncio.Task.cancel(),
a timeout scope, workflow cancellation under durable execution) is infrastructure-level and
keeps propagating as asyncio.CancelledError instead — it is never translated into this
exception, and when both race, the external cancellation wins.
Everything the run completed before the cancellation took effect — including the partial
response of an interrupted stream and the results of tool calls that finished — is preserved
in all_messages(): pass it as
message_history to a new run (with a new user prompt or not) to resume the conversation; any
tool calls that never produced a result are automatically closed out with synthesized
outcome='interrupted' returns before the history is sent to a model.
Cancellation is terminal: capability hooks (wrap_run, wrap_node_run, on_run_error) may
observe it and clean up, but cannot recover a cancelled run into a successful result.
Return the last response from the message history.
Type: ModelResponse
Return the timestamp of the last response.
Type: datetime
Return the usage of the cancelled run.
Type: RunUsage
Metadata associated with this agent run, if configured.
The unique identifier for the agent run, or None if it was cancelled before starting.
The conversation identifier, or None if the run was cancelled before starting.
@classmethod
def from_cancellation(cls, exc: BaseException) -> RunCancelled | None
Recover run state from a cancellation-related exception.
External cancellation of a plain agent.run() keeps its standard asyncio semantics. Catch
it with except asyncio.CancelledError as exc, then call
RunCancelled.from_cancellation(exc) to access the partial run state attached by Pydantic
AI. This also works with the TimeoutError raised by asyncio.timeout() or
asyncio.wait_for(), whose exception chain contains the original CancelledError, and with
the KeyboardInterrupt raised by pressing Ctrl-C during agent.run_sync() or
agent.run_stream_sync(). An
external CancelledError must keep propagating for timeouts and task groups to tear down
correctly, so re-raise it after capturing the state rather than returning from the handler;
only a first-party RunCancelled is yours to consume.
Passing a RunCancelled directly returns the same instance, providing uniform handling for
first-party and external cancellation paths.
This method traverses __context__ to find attached run state. Use
capture_run_messages() as the fallback when only message history is needed.
def all_messages() -> list[ModelMessage]
Return the complete resumable history of the cancelled run.
This is a DETACHED snapshot of the run’s message history at termination, ready to pass as
message_history for a resumed run.
list[ModelMessage] — List of messages.
def all_messages_json() -> bytes
Return all messages from all_messages as JSON bytes.
bytes — JSON bytes representing the messages.
def new_messages() -> list[ModelMessage]
Return the messages produced during the cancelled run.
Messages provided via message_history and messages from older runs are excluded.
list[ModelMessage] — List of new messages.
def new_messages_json() -> bytes
Return new messages from new_messages as JSON bytes.
bytes — JSON bytes representing the new messages.
Bases: RunCancelled, TimeoutError
Raised when an agent run doesn’t finish before its deadline.
The deadline comes from timeout= on the agent run methods, or is inherited from the run that
started this one from inside a tool (see RunContext.deadline).
The run is stopped exactly like RunCancelled, whose
subclass this is, so except RunCancelled sees every first-party stop and
all_messages() holds everything the run
completed, ready to resume from. It is also a TimeoutError, like the error asyncio.timeout() raises.
Bases: AgentRunError
Raised when resuming a suspended response whose server-side job is no longer available.
Suspended/background jobs are only resumable within the provider’s retention window (e.g. ~10 minutes for OpenAI background mode). Resuming a persisted suspended response after that window raises this instead of an opaque provider HTTP error; start a new run from the preceding messages to retry from scratch.
Bases: AgentRunError
Error raised when a Model’s usage exceeds the specified limits.
The run’s messages aren’t attached to this error. To keep the work the run did before the limit
and resume from it, wrap the run in capture_run_messages:
see Recovering a failed run’s messages.
Bases: AgentRunError
Error raised when the concurrency queue depth exceeds max_queued.
Bases: AgentRunError
Error caused by unexpected Model behavior, e.g. an unexpected response code.
Description of the unexpected behavior.
Type: str Default: message
The body of the response, if available.
Type: str | None Default: json.dumps(json.loads(body), indent=2)
Bases: UnexpectedModelBehavior
Raised when a model provider triggers content filtering or refuses a request.
During an agent run, the agent loop raises ContentFilterError when a response has finish_reason='content_filter'
and no actionable output (no parts, empty text, or thinking-only). A content_filter response that still includes
text is returned as output, unless ModelFallback moves on from it or you
add RaiseContentFilterError.
When raised by the agent loop or RaiseContentFilterError, message includes the finish or block reason and,
when the provider sends one, its refusal text (provider_details['refusal']); body holds a one-element JSON
list containing the serialized ModelResponse (loadable via
ModelMessagesTypeAdapter, including provider_details such as
refusal and refusal_category). Image generation and decision models may also raise ContentFilterError with
provider-specific message and body.
Because the agent loop raises this only after the model request returns, fallback can’t catch it as a normal
exception. Instead, naming ContentFilterError in fallback_on makes fallback reject any 'content_filter'
response and try the next model; if every model refuses, the run raises
FallbackExceptionGroup. ModelFallback names it in its default
fallback_on; FallbackModel has to be given it. See
Falling back when a model refuses.
Bases: AgentRunError
Raised when a model provider API request fails.
Adapters raise one of its category subclasses when the provider’s error says what went wrong:
ModelRateLimitError,
ModelQuotaExceededError,
ModelUnavailableError (and its
ModelOverloadedError),
ModelServerError,
ModelConnectionError (and its
ModelTimeoutError), or
ModelRequestRejectedError (and its
ModelAuthenticationError,
ModelNotFoundError,
ModelContextWindowExceededError and
ModelSchemaTooComplexError). When the error
came with an HTTP status code, the raised exception is also a
ModelHTTPError; see
ModelHTTPError.for_category.
A request can be streamed without you asking for it, e.g. when the agent has an event stream handler, so an error
the provider sends inside an already open stream is reported the same way as the same error before the stream
opened: with the HTTP status the provider uses for it, where that is clear. Such errors have
in_stream set.
The provider SDK’s own exception, if any, is available as __cause__.
The name of the model associated with the error.
Type: str Default: model_name
The body of the provider’s error response or error event, if available.
Type: object | None Default: body
The provider’s machine-readable error code, if it sent one.
For example 'rate_limit_exceeded' or 'context_length_exceeded' (OpenAI), 'ThrottlingException' (Bedrock),
'RESOURCE_EXHAUSTED' (Google, xAI), or '3059' (Mistral).
Type: str | None Default: provider_error_code
The provider’s error type, if it sent one separately from the code.
For example 'overloaded_error' or 'invalid_request_error' (Anthropic, OpenAI).
Type: str | None Default: provider_error_type
Seconds the provider asked you to wait before retrying, if it said.
An HTTP error reads it from the retry-after-ms or Retry-After response header, or, for Google, from the
google.rpc.RetryInfo in the error body when no such header carries it.
Type: float | None Default: retry_after
Whether the provider reported the error inside a response stream that had already started successfully.
Its status_code, if any, is then the status the provider
uses for the same error before a stream opens, rather than the stream’s own, and its
headers, if any, are the stream’s.
Type: bool Default: in_stream
Bases: ModelAPIError
Raised when the provider refused the request because a rate limit was reached.
For example an HTTP 429 with rate_limit_exceeded, Bedrock’s ThrottlingException, or a gRPC
RESOURCE_EXHAUSTED, whether it arrived as an HTTP status or inside a stream. Exhausted quota or billing that
the provider says so explicitly is a ModelQuotaExceededError
instead, since waiting won’t help.
Check retry_after for how long the provider asked
you to wait.
Bases: ModelAPIError
Raised when the provider refused the request because the account’s quota, credits, or billing is exhausted.
Only raised when the provider says so explicitly, e.g. an HTTP 402, OpenAI’s insufficient_quota,
Anthropic’s billing_error or a reached spend limit, Bedrock’s ServiceQuotaExceededException, a Gemini API
RESOURCE_EXHAUSTED that names a daily quota, or its FAILED_PRECONDITION asking to enable billing. Unlike a
ModelRateLimitError, retrying after a wait won’t help.
Bases: ModelAPIError
Raised when the provider or model was temporarily unable to serve the request.
For example an HTTP 503, Bedrock’s ServiceUnavailableException, or a gRPC UNAVAILABLE sent by the provider,
whether it arrived as an HTTP status or inside a stream. When the provider says the cause is load, it’s the more
specific ModelOverloadedError.
Bases: ModelUnavailableError
Raised when the provider or model was temporarily unable to serve the request because it’s overloaded.
Only raised when the provider says so: an HTTP 529, Anthropic’s overloaded_error, Hugging Face Text
Generation Inference’s overloaded error, or an unavailable error whose code or message says overloaded.
A subclass of ModelUnavailableError.
Bases: ModelAPIError
Raised when the provider failed with a server error that isn’t otherwise categorized.
For example an HTTP 500, 502 or 504, or a gRPC INTERNAL or DEADLINE_EXCEEDED sent by the provider, whether
it arrived as an HTTP status or inside a stream. An unavailable or overloaded provider is a
ModelUnavailableError instead.
Bases: ModelAPIError
Raised when the request could not reach the provider, its response could not be read, or the provider timed it out.
For example a refused connection, a reset connection, or a dropped stream. The transport library’s exception
is available as __cause__.
The stage of the request at which the transport failed, if known.
'pool': waiting for a free connection in the local connection pool. The request was not sent.'connect': opening the connection to the provider. The request was not sent.'write': sending the request. It may have been partly or fully sent.'read': receiving the response, including a stream that broke off. The request was sent, so the provider may have acted on it.
None when the transport error doesn’t say, e.g. a connection closed at an unknown point, when retrieving
credentials fails before the request is sent, and for a timeout the provider reports, which is not a transport
error.
Type: TransportPhase | None Default: phase
Bases: ModelConnectionError
Raised when a single attempt at the request timed out, at the transport layer or at the provider.
A transport timeout is the provider SDK’s or HTTP client’s own, controlled by
[ModelSettings['timeout']][pydantic_ai.settings.ModelSettings.timeout], and applies to one attempt. A timeout
the provider reports, like an HTTP 408 or Bedrock’s ModelTimeoutException, is also a
ModelHTTPError, and its phase is None. A deadline over all attempts
at a request is [ModelSettings['request_timeout']][pydantic_ai.settings.ModelSettings.request_timeout], which
raises ModelRequestTimeout instead.
Bases: ModelAPIError
Raised when the provider rejected the request itself, so sending it again unchanged won’t succeed.
For example an HTTP 400, 413 or 422, or a gRPC INVALID_ARGUMENT, when no more specific category applies.
Its subclasses say why, where the provider does:
ModelAuthenticationError,
ModelNotFoundError,
ModelContextWindowExceededError, and
ModelSchemaTooComplexError.
Exhausted credits are a ModelQuotaExceededError instead, even
when the provider sends them as a 400 like Anthropic does, since topping up the account fixes them.
Another model or provider may still accept the request, so
FallbackModel falls back on it by default.
Bases: ModelRequestRejectedError
Raised when the provider rejected the request because the input exceeds the model’s context window.
Catch it to compact or truncate the message history and try again; see
Compaction. When a capability has a
compact_history hook, an agent run first
compacts a request the model rejected this way before any output, and sends it to the same model once more.
An overflow reported as an HTTP 400 is also a
ModelHTTPError with status 400, as before. A subclass of
ModelRequestRejectedError.
FallbackModel falls back on it by default, since a later
model may have a larger context window. See
Handling context window overflow to change that.
Bases: ModelRequestRejectedError
Raised when the provider rejected the request’s credentials, or doesn’t allow them to make it.
For example an HTTP 401 or 403, a gRPC UNAUTHENTICATED or PERMISSION_DENIED, or Bedrock’s
AccessDeniedException, UnrecognizedClientException or ExpiredTokenException. Check the API key or
credentials, and that they have access to the model. A subclass of
ModelRequestRejectedError.
A 403 isn’t always about credentials. OpenRouter also sends one for a guardrail block, without saying which it is,
so that’s a ModelAuthenticationError too, but input its moderation flagged is a plain
ModelRequestRejectedError. Bedrock’s MPAgreementBeingCreated 403, sent while the account’s AWS Marketplace
subscription to the model is being processed, is a
ModelUnavailableError, since AWS says to retry it.
Bases: ModelRequestRejectedError
Raised when the provider couldn’t find the model, deployment or other resource the request names.
For example an HTTP 404, a gRPC NOT_FOUND, Bedrock’s ResourceNotFoundException, or OpenAI’s
model_not_found. When the provider says the model name is unknown,
suggested_model_id may suggest a close known one.
A 404 can also mean that the provider’s base URL is wrong. A subclass of
ModelRequestRejectedError.
Bases: ModelRequestRejectedError
Raised when the provider rejected the request because it couldn’t compile its strict schemas.
For example Anthropic’s 400 saying that the schema is too complex or that the compiled grammar is too large,
which can arrive only after minutes of compiling, for strict tool schemas and a Native Output schema that are
within the documented limits. Its hint says how to simplify
them. When the model chose Native Output for the request itself, because the output type didn’t pick a mode, the
run sends the request again in Tool Output instead of raising this, on the agent’s model or one
ModelFallback moved the request to, but not on a model inside a
FallbackModel, which handles the error itself; see
Schema complexity limits. A subclass of
ModelRequestRejectedError.
It’s not the UserError Pydantic AI raises before sending a schema whose
$refs expand too far when inlined: this is the provider rejecting a request that was sent.
Bases: ModelRequestRejectedError, UserError
Raised by Pydantic AI before sending a request that asks for something this model can’t do, but another model might.
The model’s profile or class says it doesn’t support the
feature the request asks for, like Native
Output on a model without JSON schema output, so the request is never sent. As a
ModelRequestRejectedError,
ModelFallback and
FallbackModel fall back on it by default, to a model that may
support the feature. It’s also a UserError, as it was before it had a
class of its own, so an existing except UserError keeps catching it.
A request no model could serve, like one with an invalid setting, raises a plain UserError instead.
The feature the request asks for that this model doesn’t support.
One of the ModelFeature values, or one specific to a model class.
Type: ModelFeature | str Default: feature
Bases: ModelAPIError
Raised when a model request didn’t finish within its [ModelSettings['request_timeout']][pydantic_ai.settings.ModelSettings.request_timeout], or a streamed one produced no output within its [ModelSettings['first_event_timeout']][pydantic_ai.settings.ModelSettings.first_event_timeout].
The deadlines are enforced by Pydantic AI around one request to one model, so they cover every attempt the
provider SDK and HTTP transport make, and request_timeout’s covers reading a streamed response through its last
chunk. setting says which one ran out. Under a
FallbackModel, each model gets deadlines of its own, and its
default fallback_on falls back on this error.
The timeout that ran out, in seconds.
Type: float Default: timeout
The model setting whose timeout ran out.
Type: RequestTimeoutSetting Default: setting
Bases: _ModelResponseError
Raised when the provider answered a request successfully but sent no response in it.
For example a stream that ended before its first event, or a completion whose choices are empty. It’s often a
transient glitch on the provider’s side, so sending the request again, or to another model, may well succeed.
A ModelAPIError, so
FallbackModel and
ModelFallback fall back on it by default. These responses used to
raise a plain UnexpectedModelBehavior, which this class still
subclasses so except UnexpectedModelBehavior keeps catching it.
Some adapters still raise a plain ModelAPIError for the same kind of response, so to handle all of them, catch
ModelAPIError.
Named in the fallback_on of ModelFallback or
FallbackModel, or the retry_on of
RetryOnModelErrors, it also stands for a response the provider
ended with finish_reason='error' and nothing the agent can act on, which isn’t raised as an error; see
empty error responses. ModelFallback and
RetryOnModelErrors name it by default.
Bases: _ModelResponseError
Raised when the provider answered a request successfully but its response couldn’t be used.
For example a completion that doesn’t match the API’s schema, or a response the API doesn’t allow, like an MCP
sampling result that’s not from the assistant. Unlike a
ModelEmptyResponseError, sending the same request again
often gets the same response, but another model or provider may answer it.
A ModelAPIError, so
FallbackModel and
ModelFallback fall back on it by default. These responses used to
raise a plain UnexpectedModelBehavior, which this class still
subclasses so except UnexpectedModelBehavior keeps catching it.
Some adapters still raise a plain ModelAPIError for the same kind of response, so to handle all of them, catch
ModelAPIError.
Bases: ModelAPIError
Raised when a model provider response has a status code of 4xx or 5xx.
An HTTP error that also belongs to an error category is an instance of that category too, e.g.
except ModelRateLimitError catches a 429 and except ModelHTTPError still does.
Errors that didn’t come with an HTTP status of their own get the status the provider uses for the same error
where that is clear, so they are handled the same way: xAI maps gRPC status codes (e.g. RESOURCE_EXHAUSTED
to 429), OpenRouter uses the code of an error inside a 200 response, and an error inside an open stream gets
the status of the same error before the stream opened, with
in_stream set.
The HTTP status code returned by the API.
Type: int Default: status_code
Response headers from the provider, with keys lowercased for consistent access.
For example, use exc.headers.get('retry-after') to read the Retry-After header
regardless of provider casing. None when the provider does not supply headers
(e.g. gRPC-based providers or synthesised errors).
Type: dict[str, str] | None Default: {(k.lower()): v for k, v in (headers.items())} if headers is not None else None
A close known model identifier suggested from a provider-confirmed model-name error.
Type: str | None Default: suggested_model_id
Guidance on resolving the error, appended to the exception message.
For example, the account setting a provider requires.
Type: str | None Default: hint
Whether the provider said retrying the request may succeed, from its x-should-retry response header.
None when the header is absent or isn’t true or false. OpenAI and Anthropic send it, and their SDKs
follow it when they retry.
@staticmethod
def for_category(
category: type[ModelRateLimitError | ModelQuotaExceededError | ModelUnavailableError | ModelServerError | ModelTimeoutError | ModelRequestRejectedError] | None,
*,
status_code: int,
model_name: str,
body: object | None = None,
headers: Mapping[str, str] | None = None,
suggested_model_id: str | None = None,
hint: str | None = None,
provider_error_code: str | None = None,
provider_error_type: str | None = None,
retry_after: float | None = None,
in_stream: bool = False,
) -> ModelHTTPError
Create a ModelHTTPError that is also an instance of the error category, as Pydantic AI’s models raise.
Use it in a custom model or a test to raise an HTTP error that
both except ModelHTTPError and except the category catch:
from pydantic_ai import ModelHTTPError, ModelRateLimitError
error = ModelHTTPError.for_category(ModelRateLimitError, status_code=429, model_name='my-model')
print(isinstance(error, ModelHTTPError), isinstance(error, ModelRateLimitError))
#> True True
category : type[ModelRateLimitError | ModelQuotaExceededError | ModelUnavailableError | ModelServerError | ModelTimeoutError | ModelRequestRejectedError] | None
The error category, or None for a plain ModelHTTPError. One of
ModelRateLimitError,
ModelQuotaExceededError,
ModelUnavailableError,
ModelOverloadedError,
ModelServerError,
ModelTimeoutError,
ModelContextWindowExceededError,
ModelRequestRejectedError,
ModelAuthenticationError,
ModelNotFoundError, or
ModelSchemaTooComplexError.
status_code : int
The HTTP status code.
model_name : str
The name of the model.
The body of the error response.
The response headers.
A close known model identifier, for a model-name error.
Guidance on resolving the error, appended to the message.
The provider’s machine-readable error code.
The provider’s error type.
Seconds the provider asked to wait; read from headers when not given.
in_stream : bool Default: False
Whether the provider reported the error inside an already open stream.
Bases: ExceptionGroup[Any]
A group of exceptions that can be raised when all fallback models fail.
Every attempt the FallbackModel or the
ModelFallback capability made, in order.
Unlike the grouped exceptions, this includes the usage of any response that was rejected by a
fallback_on response handler, which the agent also adds to the run’s
RunUsage. Only set on a group a FallbackModel or the agent raised
itself: it doesn’t survive a durable execution boundary, such as a FallbackModel’s group leaving a
Temporal activity.
Type: Sequence[ModelRequestAttempt] Default: ()
Bases: Exception
Exception used to signal a retry message should be returned to the LLM.
tool_retry is whichever part the retry travels as: a ToolReturnPart with outcome='retried'
when it answers a tool call, a RetryFeedbackPart when it doesn’t. A deprecated
RetryPromptPart passed in is translated into the one it
always meant.
Bases: Exception
Exception used to signal a failed ToolReturnPart should be returned to the LLM.
Bases: UnexpectedModelBehavior
Raised when a response reached the output token limit before the model finished its output.
The limit is the max_tokens model setting, or the model’s
own limit, and the response’s finish_reason is 'length'.
The agent raises it when the response has no output it can use, as re-prompting the model with the same limit
would run into it again, including when NativeOutput or
PromptedOutput text was cut off into JSON that doesn’t parse.
Plain text output that was cut off is the run’s output instead.
Naming it in ModelFallback’s or
FallbackModel’s fallback_on, as ModelFallback does by default,
also rejects any response with finish_reason='length', so the next model is tried.
Bases: OutputTokenLimitExceeded
Error raised when a model stops due to token limit while emitting a tool call.
Bases: Warning
Warning raised when in-place mutation of the message history is detected at the end of a run.
Mutating messages that are already part of the run’s history in place (e.g.
ctx.messages[0].parts[0].content = '...' from a tool) is not supported: the per-request
gen_ai.input.messages span attribute caches each message’s serialized form, so spans recorded
after the mutation may not match the messages actually sent to the model. The run-level
pydantic_ai.all_messages attribute is always serialized fresh and does reflect the mutation.
To transform history mid-run, build new message or part objects instead — e.g. with
dataclasses.replace, passing the message a new parts list (replacing a message in the
history and reassigning its parts list are both safe) — for instance in a history processor
(ProcessHistory).
The warning is best-effort: it’s raised when a mutation is detected at the end of a successful run, which covers messages still present in the final history. Errored runs aren’t checked — with warnings configured as errors, the warning would displace the run’s own exception. Its absence does not guarantee that no stale span was recorded.
Bases: UserWarning
Warning emitted when a model ignores a ModelSettings field that a request relies on.
Most settings a model doesn’t support, like temperature or seed, are tuning knobs that are silently
ignored, so the same settings work across models. This warning covers the ones whose absence changes what
the request guarantees: timeout, max_retries, stop_sequences, parallel_tool_calls=False (when the
request has function tools), extra_headers and extra_body. Each field’s Supported by: list names the
models that honor it. An OpenAI Responses model also emits it when
openai_transport='websocket' asks
for a WebSocket it can’t open, such as on an unsupported setup or after a failed connection, and sends the
request over HTTP.
It’s emitted wherever the setting comes from: the run, the agent, a capability or the model’s own settings. The
stop_sequences of an MCP server’s sampling request don’t warn, since the server chose them, not you.
Named in the fallback_on of ModelFallback or
FallbackModel, it’s raised instead, as the attempt’s error, so the
request moves on to the next model rather than being sent without the setting.
To turn it into an error, use a warnings filter:
import warnings
from pydantic_ai import UnsupportedSettingWarning
warnings.filterwarnings('error', category=UnsupportedSettingWarning)
Bases: UserWarning
Warning emitted when a request targets a model that its provider has deprecated or retired.
It’s emitted each time a model prepares a request it sends, streamed or not, for a model whose profile has a
deprecation. The message names the model, its retirement
date, the model the provider routes its requests to from then, if any, and the replacement the provider
recommends, and links the provider’s announcement. Creating the model doesn’t warn. Python’s warning filters
decide how often it’s shown.
The profile is set for models on the Anthropic and OpenAI APIs and, for some Google models, on the Gemini API and Google Cloud, each with that API’s dates, including through the Pydantic AI Gateway. It isn’t set where another platform, such as Amazon Bedrock, Google Cloud’s Vertex AI for Claude models or Azure, serves the same model on a schedule of its own. See Deprecated models.
To turn it into an error, use a warnings filter:
import warnings
from pydantic_ai import DeprecatedModelWarning
warnings.filterwarnings('error', category=DeprecatedModelWarning)
Bases: UserWarning
Warning emitted when a decision model asks a field that was likely meant as a rubric as a pick-one.
A field whose options are the whole numbers from 0 upwards is scored as a rubric only when every level is
described. When some levels are described and others are not, such as an IntEnum mixing in
UseEnumMemberDocstrings with a docstring missing under one member, the
field is asked as a pick-one instead, and this warning names it. Describe every level to have it scored.
If the pick-one is what you want, silence it with a warnings filter:
import warnings
from pydantic_ai import PartialRubricWarning
warnings.filterwarnings('ignore', category=PartialRubricWarning)
Bases: UserWarning
Warning emitted when an enum mixing in UseEnumMemberDocstrings has no source to read its member docstrings from.
The docstrings are read from the enum’s source, or from the notebook cell it was defined in. An enum with
neither, such as one built by calling an opted-in enum class (Priority('Urgency', {...})), defined in a
plain python REPL, or defined in a module installed as bytecode only (.pyc files without their .py
source, as some frozen or compiled deployments do), is described to the model by its values alone, and this
warning names it. Define it with a class statement in a notebook cell or in a module whose source is
installed, or describe the options with Choices().
If the plain values are what you want, silence it with a warnings filter:
import warnings
from pydantic_ai import EnumDocstringsUnavailableWarning
warnings.filterwarnings('ignore', category=EnumDocstringsUnavailableWarning)
The stage of a request at which a transport failure happened; see
ModelConnectionError.phase.
Default: Literal['pool', 'connect', 'write', 'read']
The model setting whose timeout ran out; see
ModelRequestTimeout.setting.
Type: TypeAlias Default: Literal['request_timeout', 'first_event_timeout']
A feature a request can ask of a model, which the model may not support; see
ModelFeatureUnsupportedError.feature.
'tool_output': Tool Output.'native_output': Native Output, which needs JSON schema output.'text_output': plain text output.'image_output': image output.'tool_choice': atool_choicesetting that forces tool use.
Type: TypeAlias Default: Literal['tool_output', 'native_output', 'text_output', 'image_output', 'tool_choice']