Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.
Guide · OtherFree

Pydantic Documentation

The official docs for Pydantic, Python's most widely used data validation library: models declared with type hints, strict and lax validation, JSON Schema generation and serialisation. It is how Python agents validate model output and tool arguments, and most agent frameworks accept Pydantic models directly.

GitHub stars

28.9k

GitHub · Sept 2026

Latest version

2.13

PyPI · Sept 2026

Monthly downloads

Over 550M

Pydantic docs · Sept 2026

Format

Guide

Topic

Other

Provider

Pydantic

Time needed

A few evenings, then reference

Level

Intermediate

Access

Free

What Pydantic is

Pydantic validates data using Python type hints. You declare a model as a class with typed fields and constraints, and Pydantic checks incoming data against it, converting where that is safe and raising a ValidationError that lists every problem when it is not. The docs call it the most widely used data validation library for Python, and say its core validation logic is written in Rust, which makes it among the fastest.

The documentation covers:

  • Models: BaseModel, fields, defaults, Field() constraints such as ranges and patterns, and nested models.
  • Validation modes: lax mode, which coerces compatible types, and strict mode, which does not.
  • Validators and serialisers: custom checks and custom output.
  • JSON Schema: model_json_schema() generates a schema from a model.
  • Serialisation: model_dump() and model_dump_json().
  • Types: dataclasses, TypedDicts, enums, literals and many standard library types.

Why it matters for AI agents

An agent is only as reliable as the data flowing between the model and your code. With one Pydantic model you get the JSON Schema to send as a tool or output schema, the validator that checks what comes back, and a typed object your code can trust. The OpenAI Agents SDK uses Pydantic for tool schemas, LangChain and LangGraph accept Pydantic models for structured output, and FastAPI uses it for request bodies, so the same skill pays off across your backend.

Prerequisites and time

Comfortable Python, including type hints and classes. The concepts section takes a few evenings; after that the docs are reference you search when you need a constraint or a custom validator.

Free or paid?

The library is free and open source under the MIT licence. The Pydantic company also sells Logfire, an observability product, which is separate and optional.

What you can do after it

Define models for every tool input and structured output in an agent, generate JSON Schema from them, validate model replies and feed precise error messages back to the model when validation fails.

Common questions

Pydantic v1 or v2? Learn version 2. Its API differs from version 1 (for example model_validate instead of parse_obj), and old tutorials still show version 1 code.

Is it the same as Pydantic AI? No. Pydantic AI is a separate agent framework from the same team, built on Pydantic.

pythonbackendopen sourceai

Ready to start?

Opens on Pydantic in a new tab.

Open resource
Link broken or out of date?

Report a problem

With "Pydantic Documentation". Only Tauseef reads this.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.