Type-Safe Retries: Programmatically Recovering from LLM Hallucinations and Schema Errors in PydanticAI

Type-Safe Retries: Programmatically Recovering from LLM Hallucinations and Schema Errors in PydanticAI

(Updated: ) 📖 4 min read

Even when using state-of-the-art foundation models with native JSON mode enabled, LLMs frequently produce outputs that violate application invariants:

  • Generating an invalid 2-letter country code when an ISO 3166-1 alpha-3 code was demanded.
  • Formatting dates as MM/DD/YYYY instead of ISO-8601 YYYY-MM-DD.
  • Inventing a negative discount or setting a departure date that occurs after the arrival date.
  • Passing malformed arguments to database tools.

In traditional Python architectures using raw SDKs, these errors trigger an unhandled pydantic.ValidationError or crash the runtime, returning an HTTP 500 error to end users.

PydanticAI fundamentally changes this paradigm through Self-Healing Type-Safe Retries. By raising ModelRetry within field validators, model validators, or tool functions, developers can instruct the LLM on exactly why its output failed and give it the opportunity to self-correct autonomously.

This guide provides a comprehensive, production-grade guide to mastering type-safe retries and self-healing error recovery.


Constrained Decoding vs. Semantic Business Validation

A common misconception is that provider-level “Structured Outputs” eliminate the need for application-level validation:

Capability Provider Constrained Decoding (JSON Schema) PydanticAI Semantic Validation & ModelRetry
Valid JSON Syntax Guaranteed (Token masking at generation time) Guaranteed
Field Types (str, int, bool) Guaranteed Guaranteed
Regular Expression Formats Supported on some providers Fully supported via custom Python regex
Relational Cross-Field Logic Impossible (Cannot verify return_date > depart_date) Native via @model_validator
External State Verification Impossible (Cannot check if SKU exists in PostgreSQL) Native via tool-level and agent context validators
Self-Healing Recovery Not supported (Throws raw API error) Automated conversational re-prompting

1. The Self-Healing Validation Cycle

sequenceDiagram
    autonumber
    actor User as Client Application
    participant Agent as PydanticAI Agent
    participant LLM as Foundation Model (Gemini / OpenAI)
    participant Validator as Pydantic Validator / Business Rule

    User->>Agent: Submit Request (e.g., "Book JFK to LHR")
    Agent->>LLM: Prompt + Target Output Schema
    LLM-->>Agent: Returns JSON: {origin: "New York", dest: "LHR"}
    Agent->>Validator: Executes @field_validator("origin")
    Note over Validator: Fails! "New York" is not a 3-letter IATA code
    Validator-->>Agent: raises ModelRetry("'New York' is not a valid 3-letter IATA code. Did you mean 'JFK'?")
    Agent->>LLM: Corrective Prompt with ModelRetry Feedback
    LLM-->>Agent: Corrected JSON: {origin: "JFK", dest: "LHR"}
    Agent->>Validator: Re-evaluates validators
    Note over Validator: All Checks Passed!
    Agent-->>User: Validated Pydantic Model Instance

2. Field-Level vs Model-Level Retries: Complete Python Implementation

Below is a production flight reservation service demonstrating both @field_validator (checking individual format constraints) and @model_validator (evaluating cross-field chronological logic):

import os
from datetime import date
from typing import Optional
from pydantic import BaseModel, Field, field_validator, model_validator
from pydantic_ai import Agent, ModelRetry

class FlightReservation(BaseModel):
    passenger_name: str = Field(..., description="Full legal passenger name")
    departure_airport: str = Field(..., description="3-letter uppercase IATA code (e.g., 'JFK', 'SFO')")
    destination_airport: str = Field(..., description="3-letter uppercase IATA code (e.g., 'LHR', 'HND')")
    departure_date: date = Field(..., description="Departure date in YYYY-MM-DD format")
    return_date: Optional[date] = Field(None, description="Return date in YYYY-MM-DD format if round trip")
    seat_class: str = Field(..., description="'economy', 'premium_economy', 'business', or 'first'")

    # 1. Field-Level Validation with ModelRetry
    @field_validator("departure_airport", "destination_airport")
    @classmethod
    def validate_iata_format(cls, v: str) -> str:
        clean_code = v.strip().upper()
        if len(clean_code) != 3 or not clean_code.isalpha():
            raise ModelRetry(
                f"Invalid airport code '{v}'. Airport codes must be strictly 3 uppercase letters (e.g., 'JFK', 'LAX'). "
                "Convert city names to their primary international IATA code."
            )
        return clean_code

    @field_validator("seat_class")
    @classmethod
    def validate_seat_tier(cls, v: str) -> str:
        allowed = {"economy", "premium_economy", "business", "first"}
        normalized = v.strip().lower().replace(" ", "_")
        if normalized not in allowed:
            raise ModelRetry(f"Seat class '{v}' is invalid. Must be one of: {', '.join(allowed)}.")
        return normalized

    # 2. Cross-Field Model-Level Validation with ModelRetry
    @model_validator(mode="after")
    def validate_trip_invariants(self):
        # Invariant 1: Destination cannot be identical to departure
        if self.departure_airport == self.destination_airport:
            raise ModelRetry(
                f"Departure airport '{self.departure_airport}' cannot match destination airport '{self.destination_airport}'. "
                "Check passenger itinerary for origin and destination."
            )

        # Invariant 2: Return date must follow departure date
        if self.return_date and self.return_date < self.departure_date:
            raise ModelRetry(
                f"Chronological error: Return date ({self.return_date}) cannot occur before departure date ({self.departure_date})."
            )

        return self

3. Initializing the Self-Healing Agent with Retry Quotas

Now we instantiate the agent, configuring retries=3. This gives the LLM up to three attempts to fix schema or business rule violations before propagating an exception:

# Initialize PydanticAI Agent with self-healing retry guardrails
travel_agent = Agent(
    "google-gla:gemini-2.5-flash",
    result_type=FlightReservation,
    retries=3,  # Up to 3 self-healing retry turns
    system_prompt="""
You are an expert corporate airline reservation coordinator.
Extract traveler flight requests into strictly verified booking records.
Today's date is 2026-10-10.
"""
)

def book_flight(user_request: str) -> FlightReservation:
    """Invokes the agent with automatic hallucination recovery."""
    try:
        # Run agent synchronously or via `await travel_agent.run(...)`
        result = travel_agent.run_sync(user_request)
        return result.data
    except Exception as exc:
        # Triggers only if the LLM failed all 3 correction attempts
        raise RuntimeError(f"Booking pipeline could not resolve valid itinerary: {exc}")

# Test Case: Input has informal city names and ambiguous dates
if __name__ == "__main__":
    prompt = "Book a business class seat for Dr. Evelyn Reed from Paris to Tokyo leaving 2026-11-15 and returning 2026-11-28."
    booking = book_flight(prompt)
    print("\n✅ Successfully Extracted & Validated Reservation:")
    print(booking.model_dump_json(indent=2))

4. Self-Healing Tool Retries: Dynamic Parameter Recovery

The ModelRetry mechanism is not limited to structured output schemas; it functions identically within agent tools.

If the model invokes a tool with incorrect parameters, you can throw a ModelRetry directly inside the tool to tell the agent what went wrong and how to adjust its call:

@travel_agent.tool
def query_seat_inventory(ctx, flight_number: str, travel_class: str) -> str:
    """Check seat availability for a given flight number."""
    if not flight_number.startswith(("AA", "BA", "AF", "JL")):
        # Raise ModelRetry directly inside the tool!
        raise ModelRetry(
            f"Flight number '{flight_number}' is missing a valid 2-letter IATA airline prefix. "
            "Re-check the airline code (e.g., 'BA117' for British Airways, 'JL005' for Japan Airlines)."
        )
        
    return f"Confirmed: 4 seats available in {travel_class} on {flight_number}."

5. Token Economics & Monitoring: Managing the Retry Tax

While self-healing retries prevent crashes, each retry turn consumes additional tokens. Here is how token consumption compounds during retries:

Attempt Number Context Included in Turn Average Token Volume Cost Multiplier
Attempt 1 (Initial) System Prompt + User Query 450 tokens 1.0x
Attempt 2 (Retry 1) Attempt 1 + Failed JSON + ModelRetry Traceback 920 tokens 2.0x
Attempt 3 (Retry 2) Attempt 2 + Second Failed JSON + Traceback 1,480 tokens 3.2x

Production Best Practices:

  1. Set retries=2 in Production: In empirical testing, if a model does not self-heal on the second attempt, a third attempt succeeds less than 8% of the time, while consuming unnecessary compute.
  2. Log Retry Rates to Prometheus: Monitor the metric pydantic_ai_retry_count_total. If a specific schema triggers retries on > 5% of requests, improve the system prompt instructions or field descriptions rather than relying on runtime retries.
  3. Write Prescriptive ModelRetry Messages: Instead of a generic “Invalid format”, provide specific corrective guidance: “Invalid date. You provided ‘tomorrow’; compute the exact date formatted as YYYY-MM-DD based on today’s date.”
WEEKLY NEWSLETTER

Get Weekly AI Architect Cost & Strategy Updates

Join 14,000+ developers receiving weekly, data-driven cost-reduction blueprints and production-ready agent guidelines.

Professor XAI
Professor XAI ML Engineer passionate about advancing AI technologies and building intelligent systems.
comments powered by Disqus