Skip to main content
A Pydantic model is a Python class that describes the structure of a piece of data — what fields it has, what types those fields must be, and which fields are optional versus required. You define the model once, and Pydantic takes care of validating every value that flows into it. This means that wherever you create or receive a model instance in your code, you can trust that the data inside it is exactly what you declared. That guarantee is what makes Pydantic so powerful in APIs, AI pipelines, and any other context where data comes from outside your application.

Defining a model

Inherit from BaseModel and declare fields using Python type annotations:
This model says: a User has a name (string), an email (string), and an age (integer). That’s it. No __init__ method, no boilerplate.
Type annotations are not just documentation here — Pydantic reads them at runtime and uses them to validate data. Missing a type annotation means Pydantic ignores the field entirely.

Pydantic vs Python dataclasses

You may have seen Python’s built-in dataclass decorator. It looks similar:
Dataclasses generate an __init__ method from your type hints but perform no validation at all — the hints are pure documentation. Pydantic enforces them at runtime. For most projects, reach for Pydantic directly: you get validation, serialization, and JSON Schema generation with almost no extra effort.

Creating instances

Pass your data as keyword arguments:
Pydantic validates the data the moment you call User(...). If anything is wrong, you get a ValidationError right there — not somewhere downstream in your code.

Automatic type coercion

Pydantic is smart about compatible types. If a value can be safely converted to the declared type, it will be:
This is especially useful when parsing form data or API responses where numbers often arrive as strings.
Coercion is not unlimited. The string "twenty-five" cannot be coerced to an int and will raise a ValidationError. If you need strict mode (no coercion at all), see the section below.

Handling validation errors

When data is invalid, Pydantic raises a ValidationError with a precise message:
Output:
The error tells you exactly which field failed and why — making debugging fast and clear.

Required vs optional fields

Fields with no default value are required. Fields with a default are optional:

Default values

Set a default for fields that usually have a common value:
Default values in Pydantic models are safe to use with mutable types like lists. Each instance gets its own copy — unlike regular Python class attributes where = [] is shared across all instances.

Converting a model to a dictionary

Use model_dump() to export a model’s data as a plain Python dictionary:
You’ll use model_dump() constantly — when storing data in a database, sending it to another service, or logging it.

Converting a model to JSON

Use model_dump_json() to get a JSON-encoded string:

Creating a model from a dictionary

When you receive data as a dictionary (from an API response, a database row, or a parsed JSON file), you have two equally valid ways to create a model:
Both approaches validate the incoming data. Use **data for everyday use and model_validate() when you need options like strict=True.

Using models as type hints

Pydantic models work naturally as type hints in function signatures, giving you IDE autocomplete and making your intent clear:
This makes your code self-documenting: any function that accepts user: User communicates exactly what data it needs.

Strict mode

By default, Pydantic coerces compatible types. If you need exact-type matching, enable strict mode:
ConfigDict is a special configuration object Pydantic looks for on the model_config class attribute. For most use cases, the default lax mode is what you want.

Learn more


What’s next?

You can now create and instantiate basic models. Next, you’ll learn how to add constraints to fields — minimum lengths, numeric ranges, regex patterns, and custom validation logic.

Validation and Fields

Learn how to add validation rules and constraints to your Pydantic fields.