Large language models are good at generating text. But text alone is rarely enough in a real application. You need structured data — a JSON object with specific fields, a document with defined metadata, a tool call with validated arguments.
This is where Pydantic comes in.
Pydantic is a Python library that lets you define the shape of your data using classes. When an LLM returns a response, Pydantic checks that the response matches the shape you defined, converts types where needed, and raises a clear error when something is wrong.
This article focuses on three specific roles Pydantic plays in LLM applications: