How to Put One pydantic Model Inside Another
Real data comes in layers: a shop has an address, and the address has a city and a postcode. In pydantic, you build that by giving a field another model as its type. pydantic then checks the inner data too, and turns a plain dictionary into the inner model. This guide nests two models with pydantic 2.14.4 on Python 3.15.2.
Syntax
from pydantic import BaseModel
class <Inner>(BaseModel):
<field>: <type>
class <Outer>(BaseModel):
<field>: <Inner>
| Part | What it means |
|---|---|
<Inner> | A normal model: the shape of the nested part |
<field>: <Inner> | A field whose value must fit the inner model. A dictionary with the right keys works, and so does an <Inner> object |
pydantic's models guide says: "More complex hierarchical data structures can be defined using models themselves as types in annotations."
The Example
A Shop has a name and an address, and an Address has a city and a postcode. The script creates a shop with the address written as a plain dictionary.
Prerequisites
- pydantic in your project, for example with
uv add pydantic. This guide uses pydantic 2.14.4 on Python 3.15.2.
Step 1: Write the Script
from pydantic import BaseModel
class Address(BaseModel):
city: str
postcode: str
class Shop(BaseModel):
name: str
address: Address
shop = Shop(name="Bike shop", address={"city": "Leeds", "postcode": "LS1 1AA"})
print(shop)
print(shop.address.city)

Step 2: Run It
uv run python shop.py
name='Bike shop' address=Address(city='Leeds', postcode='LS1 1AA')
Leeds

The dictionary became an Address object, so shop.address.city reads it with dots. The command works unchanged in PowerShell.
Errors in the Inner Model
When the inner data is wrong, the error shows the path to it, with dots. Leaving out the postcode, address={"city": "Leeds"}, gave:
1 validation error for Shop
address.postcode
Field required [type=missing, input_value={'city': 'Leeds'}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.14/v/missing
address.postcode is the postcode field inside address.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Typing the inner field as dict | pydantic accepts any dictionary and checks nothing inside it: address={"city": "Leeds"} passed without a postcode | Give it the inner model as its type |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
An error located at address.postcode | The nested data is missing a field or has a wrong value | Fix the inner dictionary at that path |
FAQ
Can I pass an Address object instead of a dictionary?
Yes. Shop(name="Bike shop", address=Address(city="Leeds", postcode="LS1 1AA")) printed the same shop.
Does the inner model have to be defined first?
Not here. With Shop written above Address, the same script printed the same shop on Python 3.15 with pydantic 2.14. pydantic's models guide covers annotations that "refer to symbols not defined when the model class is being created", and says "Pydantic tries to determine when this is necessary automatically". Putting Address first still reads more naturally.
How deep can models be nested?
As deep as the data. Each model can have fields whose types are other models, and errors show the whole path.
Conclusion
Using Address as the type of Shop.address made pydantic turn a dictionary into an Address, read it as shop.address.city, and report a missing postcode at address.postcode.
Additional Resources
- Nested models — the models guide.
- Validation errors — what
missingmeans.
