Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

Pydantic models

A Pydantic model is a class that inherits from BaseModel and checks every value against its type hints when an object is made.

Last updated: 30 Sep, 2026 · Python 3.14 · Pydantic 2.12

Type hints describe, and nothing checks them. Pydantic is a package that reads the same hints and checks the values. It is installed separately; the editor beside this lesson loads it for you, and on your computer you install it inside a virtual environment, as Virtual environments and pip shows, with pip install pydantic.

A Person model and a validation error · from 1-LangGraph Tutorial-Getting Started With Pydantic-Data Validations · 11:12 to 15:55
In the clip the class is renamed from Person to Person1 while the video compares it with a dataclass of the same name; the run below is the final code on screen.
ExampleFrom the video, run here
from pydantic import BaseModel

class Person1(BaseModel):
    name: str
    age: int
    city: str

person = Person1(name="Krish", age=35, city="Bangalore")
print(person)
person1 = Person1(name="Krish", age=35, city=12)
print(person1)

The first Person1 printed its fields. city=12 is an int where the model wants a string, so Pydantic refused it with a ValidationError; the same dataclass in the video stored 12 without a word. Pydantic does convert in the other direction when it is safe, as the Triage model shows with a priority of "4".

Syntax:

python
from pydantic import BaseModel

class Name(BaseModel):
    field: type

Defining the Triage model

Example
from pydantic import BaseModel

class Triage(BaseModel):
    category: str
    priority: int

result = Triage(category="billing", priority=4)
print(result)
print(result.priority + 1)

A Pydantic model looks like the dataclass from dataclasses, except it inherits from BaseModel instead of using a decorator. Fields are passed by name.

Converting text that is clearly a number

Example
result = Triage(category="billing", priority="4")
print(result.priority, type(result.priority))

"4" became the int 4. Pydantic converts text that clearly is a number, because data from files and models so often arrives as text.

Refusing a value it cannot convert

Example
Triage(category="billing", priority="high")

A ValidationError names the model, the field, what was wrong and the value that was given. In dataclasses the same mistake was stored silently.

Field constraints · from 1-LangGraph Tutorial-Getting Started With Pydantic-Data Validations · 23:20 to 26:57
The spoken limits in the clip drift from the code on screen. The code says gt=0, le=1000 for the price and ge=0 for the quantity, so a price of 0 is refused and a quantity of 0 is allowed.
ExampleFrom the video, run here
from pydantic import BaseModel, Field

class Item(BaseModel):
    name: str = Field(min_length=2, max_length=50)
    price: float = Field(gt=0, le=1000)
    quantity: int = Field(ge=0)

item = Item(name="Book", price=29.99, quantity=10)
print(item)
Item(name="Book", price=-1, quantity=10)

The valid Item prints its fields. A price of -1 breaks gt=0, and the error names the field, the rule and the value given. The Triage model uses the same Field, plus Literal for the category:

Limiting values with Literal and Field

Example
from typing import Literal
from pydantic import BaseModel, Field

class Triage(BaseModel):
    category: Literal["billing", "shipping", "other"]
    priority: int = Field(ge=1, le=5)

print(Triage(category="shipping", priority=3))
Triage(category="sales", priority=9)

Literal[...] allows only the listed values. Field(ge=1, le=5) means greater than or equal to 1 and less than or equal to 5. One bad object gives one error that lists every field that failed, not only the first. If you are building one file, delete the Triage(category="sales", priority=9) line before the next example.

Turning a model back into a dictionary

Example
result = Triage(category="shipping", priority=3)
print(result.model_dump())

model_dump gives the plain dictionary back, ready for json.dump from JSON.

dataclass vs Pydantic model

@dataclassBaseModel
Comes with PythonYesNo, pip install pydantic
Wrong type passed inStored silentlyConverted if safe, else ValidationError
Limits such as 1 to 5NoField(ge=1, le=5)

Where Pydantic shows up in AI code

  • Structured output: frameworks take a Pydantic class and make the model answer in that shape.
  • Tool arguments and API request bodies, checked before your code runs.
  • Settings loaded from files or the environment.
Watch out. Pydantic's conversion can hide a problem. priority="4" passes, and so does priority=4.0; if an answer must be a real int from the start, check the raw value too.
Try it yourself
  • Add a field reply: str | None = None and make a Triage without it.
  • Try priority=4.0, then priority=4.5.
  • Make the error happen inside try and except ValidationError, importing it from pydantic.
PreviousType hints

Every expert started right here.