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.
Person to Person1 while the video compares it with a dataclass of the same name; the run below is the final code on screen.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)name='Krish' age=35 city='Bangalore'
Traceback (most recent call last):
File "main.py", line 10, in <module>
person1 = Person1(name="Krish", age=35, city=12)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Person1
city
Input should be a valid string [type=string_type, input_value=12, input_type=int]
For further information visit https://errors.pydantic.dev/2.12/v/string_typeThe 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:
from pydantic import BaseModel
class Name(BaseModel):
field: typeDefining the Triage model
from pydantic import BaseModel
class Triage(BaseModel):
category: str
priority: int
result = Triage(category="billing", priority=4)
print(result)
print(result.priority + 1)category='billing' priority=4 5
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
result = Triage(category="billing", priority="4")
print(result.priority, type(result.priority))4 <class 'int'>
"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
Triage(category="billing", priority="high")Traceback (most recent call last):
File "main.py", line 1, in <module>
Triage(category="billing", priority="high")
pydantic_core._pydantic_core.ValidationError: 1 validation error for Triage
priority
Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='high', input_type=str]
For further information visit https://errors.pydantic.dev/2.12/v/int_parsingA ValidationError names the model, the field, what was wrong and the value that was given. In dataclasses the same mistake was stored silently.
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.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)name='Book' price=29.99 quantity=10
Traceback (most recent call last):
File "main.py", line 10, in <module>
Item(name="Book", price=-1, quantity=10)
pydantic_core._pydantic_core.ValidationError: 1 validation error for Item
price
Input should be greater than 0 [type=greater_than, input_value=-1, input_type=int]
For further information visit https://errors.pydantic.dev/2.12/v/greater_thanThe 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
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)category='shipping' priority=3
Traceback (most recent call last):
File "main.py", line 9, in <module>
Triage(category="sales", priority=9)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for Triage
category
Input should be 'billing', 'shipping' or 'other' [type=literal_error, input_value='sales', input_type=str]
For further information visit https://errors.pydantic.dev/2.12/v/literal_error
priority
Input should be less than or equal to 5 [type=less_than_equal, input_value=9, input_type=int]
For further information visit https://errors.pydantic.dev/2.12/v/less_than_equalLiteral[...] 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
result = Triage(category="shipping", priority=3)
print(result.model_dump()){'category': 'shipping', 'priority': 3}model_dump gives the plain dictionary back, ready for json.dump from JSON.
dataclass vs Pydantic model
@dataclass | BaseModel | |
|---|---|---|
| Comes with Python | Yes | No, pip install pydantic |
| Wrong type passed in | Stored silently | Converted if safe, else ValidationError |
| Limits such as 1 to 5 | No | Field(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.
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.Related
- Previous: Type hints
- Next: model_validate_json
- Reference: Models in the Pydantic docs
- Add a field
reply: str | None = Noneand make a Triage without it. - Try
priority=4.0, thenpriority=4.5. - Make the error happen inside
tryandexcept ValidationError, importing it frompydantic.
Every expert started right here.