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
- Zero Runtime Crashes: Production endpoints never return unhandled 500 errors due to format discrepancies.
- Deterministic Output Guarantees: Downstream database and payment microservices receive 100% verified data.
- 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.