Structured output validation

Tool-calling LLMs occasionally invent arguments that do not match the function signature. On a phone call, that turns into a failed lookup and an awkward repair turn. Validate the arguments with Pydantic before the tool runs and recover with a quick clarifying question.

Validation gate before tool execution

Parse arguments into a Pydantic model. Valid call goes through. Invalid call routes back to the agent for clarification.

agent/tools.py
python
from pydantic import BaseModel, Field, ValidationError

class FlightStatusArgs(BaseModel):
    flight_number: str = Field(..., pattern=r'^[A-Z]{2}\d{2,4}$')

async def get_flight_status_safe(raw_args: dict) -> str:
    try:
        args = FlightStatusArgs(**raw_args)
    except ValidationError as e:
        return 'Sorry, I did not catch the flight number. Could you say it again?'
    return await get_flight_status(args.flight_number)

A regex on the field shape stops most hallucinations. The recovery message is short and natural, never a stack trace.

Quiz: Quiz

Loading practice…