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: ) ๐Ÿ“– 1 min read

Even frontier models occasionally generate invalid outputs: returning an ISO-8601 string where a float was expected, omitting a mandatory nested object, or failing business validation rules.

In traditional frameworks, this triggers a fatal pydantic.ValidationError that crashes your API endpoint.

PydanticAI introduces Self-Healing Type-Safe Retries: intercepting validation errors and feeding the exact schema traceback back to the LLM so it corrects its own mistakes autonomously.


1. The Self-Healing Validation Loop

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”       1. Generates JSON       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ PydanticAI Agentโ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€> โ”‚ Pydantic Validator     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                               โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ–ฒ                                                    โ”‚ 2. Fails Rule:
         โ”‚                                                    โ”‚ "Age must be >= 18"
         โ”‚            3. Re-prompts with Traceback            โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

2. Complete Code Example

from pydantic import BaseModel, field_validator
from pydantic_ai import Agent, ModelRetry

class FlightBooking(BaseModel):
    passenger_name: str
    passport_number: str
    departure_airport: str
    destination_airport: str
    seat_class: str

    @field_validator("departure_airport", "destination_airport")
    @classmethod
    def validate_iata_code(cls, v: str) -> str:
        if len(v) != 3 or not v.isupper():
            # Raises a ModelRetry instructing the LLM to fix the IATA code!
            raise ModelRetry(f"'{v}' is not a valid 3-letter uppercase IATA code (e.g., LHR, JFK).")
        return v

agent = Agent(
    "google-gla:gemini-2.5-flash",
    result_type=FlightBooking,
    retries=3, # Automatically retries up to 3 times on validation failure
    system_prompt="You are an airline reservation agent. Extract travel itineraries into booking records."
)

result = agent.run_sync("Book a flight for Jane Doe from London Heathrow to New York JFK in business class.")

print("Validated Booking:", result.data)

3. Key Architectural Benefits

  1. Zero Runtime Crashes: Production endpoints never return unhandled 500 errors due to format discrepancies.
  2. Deterministic Output Guarantees: Downstream database and payment microservices receive 100% verified data.
  3. Transparent Audit Logging: PydanticAI records all retry attempts, providing clear debugging telemetry.
FREE CODE TEMPLATE

Download the Complete PydanticAI Document Parser Blueprint

Get the complete, type-safe invoice and ID card parsing codebase in Python + a ready-to-run Docker environment. 100% free.

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