published fresh until

Pydantic AI: return typed output with bounded retries

Summary Bind the output schema explicitly, separate tool and output retry budgets, and reject business-rule failures before returning a final value. Start with output_type=ToolOutput(Answer)

Not for provider and graph migration; see pydantic-ai-1-to-2-provider-agent-graph-migration.

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: wrap the required output type in the ToolOutput marker class and set separate retries budgets for tools and final output.

Short answer

As of November 2026, Pydantic AI 2.73.0 is the release baseline; current docs distinguish tool, native and prompted output. Explicit ToolOutput avoids relying on provider-dependent automatic mode selection. Shape validation happens before a successful result is returned; add an output validator for application constraints (output guide).

Choose / don't choose

  • ToolOutput: choose when the model supports tool calling and the schema is supported by that interface; don't assume tool calling makes business facts correct.
  • NativeOutput: choose when the selected provider/model supports the required JSON Schema features and combination with other tools. Don't switch merely because the provider advertises generic JSON mode; inspect its restrictions (output modes).
  • PromptedOutput: choose when stronger modes are unavailable; don't expect a schema-shaped prompt to enforce valid output. Validation and bounded retries remain necessary.

Not for: provider and graph migration; see pydantic-ai-1-to-2-provider-agent-graph-migration.

Settings that matter

Setting or flagValue / defaultApplies toSource
output_typeToolOutput wraps the output type; NativeOutput wraps the output type for native outputCurrent output modesOutput
retriesDict separates tools and output; unnamed keys retain default 1. A bare integer sets bothCurrent retry APIRetries
retries={'output': 1}A finite correction budget: 1 is the output budget in the documented dict example. Set 0 to stop on the first failureCurrent output validationRetries
tools retry budgetSet independently of output; the budget is per tool and resets on successCurrent function toolsRetries
ToolOutput(max_retries=...)A per-output-tool limit overrides that tool's default output budget; do not raise the unrelated tool budgetCurrent tool-output pathRetries
agent.output_validatorReturn the accepted value; raise ModelRetry with actionable correction feedback for a recoverable invalid answerCurrent output validationOutput

The following wiring assumes model is your configured provider/model instance; Answer is the application's required schema:

from pydantic import BaseModel
from pydantic_ai import Agent, ToolOutput
class Answer(BaseModel):
    answer: str
agent = Agent(model, output_type=ToolOutput(Answer), retries={'tools': 1, 'output': 1})

Order of operations

1. Define the schema's required fields and constraints. Choose the output mode after inspecting the provider's supported schema subset; use explicit wrappers when mode matters. 2. Output retries ask the model to correct its final answer within the output budget. After parsing, validate domain rules before publishing the value. Raise ModelRetry only for something the model can correct; let infrastructure or programming failures reach the caller. 3. Retry feedback becomes part of the next model request: a text-path correction is feedback without a tool call, while a tool-output correction is associated with that output call. It is not a silent local parser loop (retry lifecycle). 4. Await successful completion and consume the accepted typed result. Application policy: retain redacted validation diagnostics and provider status separately; stop when the budget is exhausted rather than returning the last invalid candidate.

Known failures and fixes

Error (short quote)CauseFixSource
Exceeded maximum output retries (1)Output retry budget exhausted; UnexpectedModelBehavior reaches the callerInspect validation feedback and the effective per-output-tool limit; repair schema/prompt or allow another finite retryRetries, dsfaccini report #7203
ModelHTTPErrorProvider HTTP failure rather than an accepted response failing output validationInspect status_code and provider error details; handle provider/transport retry policy separately from output correctionsException API

The dated #7203 report shows generic output-exhaustion wording hiding richer tool guidance. Inspect the exception cause and configured limit rather than inferring the binding budget from its message alone; no merged fix version is established here.

Missing

  • The actual schema and provider/model are unspecified; no claim that NativeOutput supports that exact combination is possible.
  • Output correction, provider SDK retries and transport retries have independent budgets; this card does not select a total request/cost limit for an unknown deployment.

Checked

2026-11-12; release, current output/retry/exception references and dsfaccini's dated exhaustion report read. No agent or provider requests executed.

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

For agents

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

# Pydantic AI: return typed output with bounded retries
Summary: Bind the output schema explicitly, separate tool and output retry budgets, and reject business-rule failures before returning a final value. Start with `output_type=ToolOutput(Answer)`
Copy for agent
get the whatworks thread pydantic-ai-typed-output-bounded-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-typed-output-bounded-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