Pydantic¶
Beginner → Intermediate · 5 topics
Pydantic turns Python type hints into data that is checked and converted at run time. In GenAI
work it sits everywhere data crosses a boundary: the JSON an LLM returns, the arguments of an agent's
tool call, the body of an API request, and the settings your app reads from .env. FastAPI, the OpenAI
and Anthropic SDKs, LangChain and most agent frameworks are built on it.
| # | Topic | Sub-topics |
|---|---|---|
| 1 | Models & validation | BaseModel · type conversion · defaults · optional fields · ValidationError |
| 2 | Fields & validators | Field limits · Literal & enums · field_validator · model_validator |
| 3 | Nested models & JSON | Nested models · lists of models · model_dump · JSON in and out · aliases |
| 4 | Settings & secrets | BaseSettings · .env files · SecretStr · one settings object for the app |
| 5 | Pydantic for GenAI | Structured LLM output · JSON Schema for tool calling · validate-and-retry · agent state |
Pydantic v2
These notes use Pydantic v2 (2023+). If you see .dict(), .parse_obj() or @validator in old
tutorials, that is v1 — the v2 names are .model_dump(), .model_validate() and @field_validator.
Already read Type hints, dataclasses & Pydantic? This series goes deeper into the parts GenAI apps use every day.
Next: FastAPI — Pydantic models become API requests and responses.