Skip to main content

Error Recovery for AI Agents

This package ships error handling and recovery patterns for improved reliability of AI Agents, as a native Pydantic AI 2.0 capability.

Motivation

Agentic tool calling can fail for many reasons, and adding error handling and retries to every tool is cumbersome. For third-party tools and MCPs, it's often impossible. ToolErrorRecovery is a standardized capability for AI agents to recover from errors during tool calls. Similar in spirit to FastAPI's exception handlers, but for agents.

This package combines the ideas and vocabulary of pydantic-ai-harness PR #171 with its own mechanics: it uses a classify callable rather than a per-tool strategy map for more flexibility; a single wrap_tool_execute hook rather than two; and does not expose str(error) to the agent by default for added security. It is intended as a stand-alone capability, but also as a proposal of how to make progress on PR #171.

Quick Start

pip install pydantic-ai-error-recovery
from pydantic_ai import Agent
from pydantic_ai_error_recovery import ToolErrorRecovery

agent = Agent('openai:gpt-5', capabilities=[ToolErrorRecovery()])

Out of the box, errors raised before a request was sent (refused connections, connect timeouts) are retried, bugs are propagated, and everything else is reported to the model, preventing a crash of the agent run. Continue reading to learn how to customize the capability.

Features

The ToolErrorRecovery capability supports four different recovery outcomes:

Outcome Effect
retry Re-attempt the tool call a number of times (invisible to the model)
inform Report a terminal failure to the model via ToolFailed (outcome='failed')
fallback Return a substitute value as the tool result
propagate Re-raise the exception, will crash the run unless caught by another wrapper

Error Classification

ToolErrorRecovery intercepts tool execution errors and applies a per-error reaction. A classify callable you supply inspects each failure and returns the intended recovery outcome:

def classify(ctx: RunContext[Any], call: ToolCallPart, error: BaseException) -> RecoveryOutcome:
    if isinstance(error, SomeException):
        return RecoveryOutcome.inform(
            label='Tool call ran into SomeException',
            expose_message=False,
        )
    # and so on

Notes:

  • You can classify on a tool's name and other properties of the tool call, on the exception type, or both.
  • The ctx: RunContext parameter is included for cases in which your classification logic additionally depends on the run context.
  • To not interfere with existing error handling, the following exceptions from the Pydantic AI control flow are never classified: SkipToolExecution, CallDeferred, ApprovalRequired, ModelRetry, ToolRetryError, ToolFailed, and ToolFailedError.
  • Special care must be taken when classifying an exception which is a subclass, be careful about the order of isinstance(...) calls, or use type(...) is instead.
  • The inform text and the fallback value are produced after every after_tool_execute hook and outside innermost wrappers such as ToolGuardrail, so result screening does not see them. Only expose exception text (expose_message, include_traceback, format_error) you trust to reach the model.

Default Behaviour

As mentioned above, control flow exceptions are never classified. Our default classifier is:

def classify(ctx: RunContext[Any], call: ToolCallPart, error: BaseException) -> RecoveryOutcome:
    if isinstance(error, DEFAULT_BUG_TYPES):
        return RecoveryOutcome.propagate()
    if isinstance(error, HookTimeoutError):
        return RecoveryOutcome.inform()
    if isinstance(error, DEFAULT_TRANSIENT_TYPES):
        return RecoveryOutcome.retry(3)
    return RecoveryOutcome.inform()

The classification function is the key piece required to construct your own RecoveryPolicy via RecoveryPolicy(classify=classify). A fully custom example could look like this:

from pydantic_ai import Agent
from pydantic_ai_error_recovery import ToolErrorRecovery

def classify(...):
    # your classification logic

agent = Agent('openai:gpt-5', capabilities=[
    ToolErrorRecovery(
        policy=RecoveryPolicy(
            classify=classify,
            format_error=my_formatter,
            max_message_len=450,
            include_traceback=False,
            logger=my_logger,
        ),
        max_recoveries=10,
        per_tool_recoveries={'my_tool_name': 5},
    )
])

Contributing

We welcome community contributions under Apache-2.0. Please create an issue to discuss an improvement, or submit a pull request. Clone the repository, then run:

make install   # uv sync --extra dev
make all       # lint, format, typecheck, and tests with 100% branch coverage

Please ensure that make all passes before committing. Coding standards, file layout, and testing patterns can be found in AGENTS.md.

License

Apache-2.0

Metadata

Release files for pydantic-ai-error-recovery 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-ai-error-recovery 0.3.0
File Size Uploaded Provenance Actions
pydantic_ai_error_recovery-0.3.0.tar.gz 60.8 kB With provenance Details for pydantic_ai_error_recovery-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-ai-error-recovery 0.3.0
File Interpreter ABI Platform Actions
pydantic_ai_error_recovery-0.3.0-py3-none-any.whl Python 3 none any Details for pydantic_ai_error_recovery-0.3.0-py3-none-any.whl

Total release size: 78.5 kB

Release files / pydantic_ai_error_recovery-0.3.0.tar.gz

Download URL pydantic_ai_error_recovery-0.3.0.tar.gz
Size 60.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c544287cb5b8b565aa99fcb04f6f85a6667bc7f7ead2531c4d76a01b6c646238
BLAKE2b-256 checksum
How to use checksums
3ae442eaa4085b6d8ac395af4956d9c673991ba0c75a5dc8e0aa65dcfd9d6a37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Release files / pydantic_ai_error_recovery-0.3.0-py3-none-any.whl

Download URL pydantic_ai_error_recovery-0.3.0-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
276c8f2819225fe241216ffd51cbd6de795404eb58f965c966c3f48e9012e0f4
BLAKE2b-256 checksum
How to use checksums
68c58580b152dd1633a370457ac8efaf17da1aff3d8581a315fb250391b0d7a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

Security for pydantic-ai-error-recovery 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pydantic_ai_error_recovery-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 20, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page