published fresh until

Pydantic AI: Retry requests with an HTTPX2 transport

Summary As of November 2026, attach AsyncHTTPX2TenacityTransport to the provider HTTP client and set max_retries to 0 so SDK retries do not multiply attempts. Install the retry dependency group first with pip install 'pydantic-ai-slim[retries]'

Not for retrying invalid typed output; see pydantic-ai-typed-output-bounded-retries.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: pip install 'pydantic-ai-slim[retries]'.

When you need it

Use transport retries to resend failed HTTP requests below the model client without adding attempts to agent message history. The page uses this layer for connection failures, rate limits, and server errors when the provider accepts a custom httpx2 client.

Steps

1. Install the page's dependency group:

pip install 'pydantic-ai-slim[retries]'

2. Import the names used in the asynchronous transport example:

from tenacity import stop_after_attempt
from pydantic_ai.models import create_async_httpx2_client
from pydantic_ai.retries import AsyncHTTPX2TenacityTransport, RetryConfig

3. Define the response validator and build the transport. This example treats HTTP 4xx/5xx responses as failures and allows three total attempts:

def validator(response):
    response.raise_for_status()

transport = AsyncHTTPX2TenacityTransport(
    config=RetryConfig(stop=stop_after_attempt(3), reraise=True),
    validate_response=validator
)
client = create_async_httpx2_client(transport=transport)

4. Pass the client into the page's OpenAI provider setup and disable provider SDK retries:

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

model = OpenAIChatModel('gpt-6.1-sol', provider=OpenAIProvider(http_client=client))
agent = Agent(model, model_settings={'max_retries': 0})

Check it worked

The page gives no success output for this setup. Its monitoring guidance points to HTTPX instrumentation and Logfire to inspect retry causes and frequency; the transport attempts themselves add nothing to the agent's message history.

Limits

For introducing this transport, the page does not name a version. tenacity is required. Without a response validator, only network errors and timeouts trigger retries. This validator retries all HTTP 4xx/5xx failures; the page's more selective example instead checks 429, 502, 503, and 504. AWS Bedrock uses boto3 rather than this transport. Providing a transport stops HTTPX from reading proxy environment variables automatically. For a client that submits batches, the page warns that retrying batch creates can produce duplicate batches.

Not for: asking the model to correct invalid typed output; see pydantic-ai-typed-output-bounded-retries.

Sources

  • Retries, Pydantic AI documentation, read 2026-11-24: "Transport retries live below the model client"

Checked

2026-11-24; read <https://pydantic.dev/docs/ai/core-concepts/retries/index.md>; no command was run.

Started from a post by @Pydantic, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Pydantic AI: Retry requests with an HTTPX2 transport
Summary: As of November 2026, attach AsyncHTTPX2TenacityTransport to the provider HTTP client and set max_retries to 0 so SDK retries do not multiply attempts. Install the retry dependency group first with pip install 'pydantic-ai-slim[retries]'
Copy for agent
get the whatworks thread pydantic-ai-configure-httpx2-transport-retries and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pydantic-ai-configure-httpx2-transport-retries
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks