Skip to content

pydantic_ai.exceptions

ModelRetry

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.

Attributes

message

The message to return to the model.

Type: str Default: message

Methods

__get_pydantic_core_schema__

@classmethod

def __get_pydantic_core_schema__(cls, _: Any, __: Any) -> core_schema.CoreSchema

Pydantic core schema to allow ModelRetry to be (de)serialized.

Returns

core_schema.CoreSchema

CallDeferred

Bases: Exception

Exception to raise when a tool call should be deferred.

See tools docs for more information.

Constructor Parameters

metadata : dict[str, Any] | None Default: None

Optional dictionary of metadata to attach to the deferred tool call. This metadata will be available in DeferredToolRequests.metadata keyed by tool_call_id.

ApprovalRequired

Bases: Exception

Exception to raise when a tool call requires human-in-the-loop approval.

See tools docs for more information.

Constructor Parameters

metadata : dict[str, Any] | None Default: None

Optional dictionary of metadata to attach to the deferred tool call. This metadata will be available in DeferredToolRequests.metadata keyed by tool_call_id.

SkipModelRequest

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.

SkipToolValidation

Bases: Exception

Exception to raise in before/wrap tool validate hooks to skip validation.

The provided args will be used as the validated arguments.

SkipToolExecution

Bases: Exception

Exception to raise in before/wrap tool execute hooks to skip execution.

The provided result will be used as the tool result.

UserError

Bases: RuntimeError

Error caused by a usage mistake by the application developer — You!

Attributes

message

Description of the mistake.

Type: str Default: message

AgentRunError

Bases: RuntimeError

Base class for errors occurring during an agent run.

Attributes

message

The error message.

Type: str Default: message

UsageLimitExceeded

Bases: AgentRunError

Error raised when a Model’s usage exceeds the specified limits.

ConcurrencyLimitExceeded

Bases: AgentRunError

Error raised when the concurrency queue depth exceeds max_queued.

UnexpectedModelBehavior

Bases: AgentRunError

Error caused by unexpected Model behavior, e.g. an unexpected response code.

Attributes

message

Description of the unexpected behavior.

Type: str Default: message

body

The body of the response, if available.

Type: str | None Default: json.dumps(json.loads(body), indent=2)

ContentFilterError

Bases: UnexpectedModelBehavior

Raised when content filtering is triggered by the model provider resulting in an empty response.

ModelAPIError

Bases: AgentRunError

Raised when a model provider API request fails.

Attributes

model_name

The name of the model associated with the error.

Type: str Default: model_name

ModelHTTPError

Bases: ModelAPIError

Raised when an model provider response has a status code of 4xx or 5xx.

Attributes

status_code

The HTTP status code returned by the API.

Type: int Default: status_code

body

The body of the response, if available.

Type: object | None Default: body

FallbackExceptionGroup

Bases: ExceptionGroup[Any]

A group of exceptions that can be raised when all fallback models fail.

ToolRetryError

Bases: Exception

Exception used to signal a ToolRetry message should be returned to the LLM.

IncompleteToolCall

Bases: UnexpectedModelBehavior

Error raised when a model stops due to token limit while emitting a tool call.