AiDiwasAiDiwas
Share:
ai-agents

Getting Started with Pydantic for LLM Outputs

Srikanth M1 min read

This is a sample post. Real, original write-ups are being cooked up right now, brewing slowly, but properly.

Getting Started with Pydantic for LLM Outputs

An LLM that returns raw text is an LLM your code can't trust. Pydantic gives you a typed contract the model's output has to satisfy.

Quick answer: define a Pydantic model for the shape you want, pass its schema to the model (via tool calling or structured output mode), and validate the response before touching it in code.

The problem with parsing free text

Regex-parsing an LLM's prose is fragile — a slightly different phrasing breaks the parser. Structured output sidesteps this entirely.

A minimal example

from pydantic import BaseModel

class Invoice(BaseModel):
    vendor: str
    amount_cents: int
    due_date: str

# Pass Invoice.model_json_schema() to the model as the expected output
# shape, then validate the response:
invoice = Invoice.model_validate_json(model_response)

Key Takeaways

  • Structured output beats prompt-engineered text parsing every time.
  • Validation errors are actionable — you know exactly what field failed.
  • This pattern works the same for agent tool arguments and final answers.

Frequently Asked Questions

Does this only work with OpenAI? No — most major providers support structured/tool-call output now.

pydanticpythonllmsstructured-output

Related reading