As AI systems become more advanced, their data structures also become more complex.
Simple flat schemas are useful for beginners, but real-world AI applications often need to represent:
- users with addresses,
- products with categories,
- workflows with multiple steps,
- API responses with nested metadata,
- and multi-agent systems exchanging structured information.
This is where nested Pydantic models become extremely important.
Nested models allow developers to build structured schemas inside other schemas, creating cleaner and more scalable AI systems.
In PydanticAI, nested models play a major role in:
- structured outputs,
- agent communication,
- workflow orchestration,
- tool calling,
- and production AI engineering.
This tutorial explains:
- what nested Pydantic models are,
- how they work,
- why they matter,
- and how to use them in AI applications.

What Is a Nested Pydantic Model?
A nested model is a Pydantic model that contains another Pydantic model as one of its fields.
Basic idea:
User └── Address
Instead of storing all information in one flat structure, related data becomes organized into reusable components.
Why Nested Models Matter
Real-world systems rarely use flat data only.
Examples:
- customers have addresses,
- orders contain products,
- AI agents generate structured reports,
- APIs return deeply structured JSON,
- workflows contain multiple steps and states.
Nested models allow developers to represent these structures cleanly.
Simple Flat Model Example
Flat structure:
from pydantic import BaseModelclass User(BaseModel): name: str city: str country: str
This works for small examples.
But as systems grow:
- structures become repetitive,
- schemas become harder to maintain,
- and reuse becomes difficult.
Introducing Nested Models
Now let’s improve the structure.
from pydantic import BaseModelclass Address(BaseModel): city: str country: strclass User(BaseModel): name: str address: Address
Now:
User
contains:- an
Addressmodel.
This creates a much cleaner architecture.
Creating a Nested Object
Example:
user = User( name="Alice", address={ "city": "Amsterdam", "country": "Netherlands" })print(user)
Output:
name='Alice'address=Address(city='Amsterdam', country='Netherlands')
Pydantic automatically converts the dictionary into a nested model object.
Why This Is Powerful
Nested models provide:
- structure,
- organization,
- reusability,
- and validation.
Instead of massive flat schemas, systems become modular.
This is extremely useful in:
- AI agents,
- APIs,
- workflow orchestration,
- and automation systems.
Nested Models Improve Readability
Compare these two approaches.
Flat Schema
class User(BaseModel): name: str city: str country: str street: str zipcode: str
Nested Schema
class Address(BaseModel): street: str city: str zipcode: str country: strclass User(BaseModel): name: str address: Address
The nested version is:
- cleaner,
- easier to maintain,
- and more scalable.
Nested Validation
One of the biggest advantages is automatic nested validation.
Suppose invalid data appears:
user = User( name="Alice", address={ "city": 123, "country": "Netherlands" })
Pydantic raises a validation error because:
cityshould be a string.
Nested validation happens automatically.
Nested Models in AI Systems
Nested models are extremely common in AI workflows.
Examples:
- AI-generated reports,
- tool-calling responses,
- workflow states,
- structured agent memory,
- and multi-step execution pipelines.
AI systems often generate hierarchical data naturally.
Example: AI Research Report
class Source(BaseModel): title: str url: strclass ResearchReport(BaseModel): topic: str summary: str sources: list[Source]
This allows AI systems to generate:
- reports,
- with structured source lists,
- and validated references.
Example AI Output
{ "topic": "AI Agents", "summary": "AI agents automate workflows.", "sources": [ { "title": "Research Paper", "url": "https://example.com" } ]}
This structure is:
- predictable,
- machine-readable,
- and easy to validate.
Nested Models and Tool Calling
Nested schemas become especially useful when AI systems interact with tools.
Example:
class Coordinates(BaseModel): latitude: float longitude: floatclass WeatherRequest(BaseModel): city: str location: Coordinates
This allows:
- strongly typed tool inputs,
- and safer API interactions.
Deeply Nested Models
Models can be nested multiple levels deep.
Example:
class Company(BaseModel): name: strclass Employee(BaseModel): name: str company: Companyclass Project(BaseModel): title: str owner: Employee
This creates hierarchical structured data systems.
Nested Lists of Models
You can also store lists of nested models.
Example:
class Product(BaseModel): name: str price: floatclass Order(BaseModel): products: list[Product]
Now each order can contain multiple validated products.
Why AI Engineers Should Care About Nested Models
As AI systems grow:
- workflows become interconnected,
- APIs exchange structured data,
- agents collaborate,
- and orchestration becomes more complex.
Nested models create:
- cleaner architecture,
- safer automation,
- and easier debugging.
Nested Models Improve Reusability
One major advantage:
- models can be reused across applications.
Example:
Address
could be reused in:- users,
- orders,
- shipping systems,
- customer profiles,
- and billing systems.
This reduces duplication dramatically.
Converting Nested Models to Dictionaries
Pydantic models can easily become dictionaries.
Example:
print(user.model_dump())
Output:
{ "name": "Alice", "address": { "city": "Amsterdam", "country": "Netherlands" }}
This is useful for:
- APIs,
- JSON responses,
- databases,
- and serialization.
Converting Nested Models to JSON
print(user.model_dump_json())
This creates structured JSON automatically.
Nested Models and APIs
Nested schemas are extremely common in APIs.
For example:
FastAPI heavily relies on nested Pydantic models for:
- requests,
- responses,
- and validation.
Learning nested models benefits much more than AI development alone.
Nested Models in Multi-Agent Systems
Multi-agent systems often exchange structured messages.
Example:
class AgentMessage(BaseModel): sender: str task: str metadata: dict
Nested schemas improve:
- communication consistency,
- validation,
- and orchestration reliability.
Common Beginner Mistakes
1. Creating Giant Flat Models
Large flat schemas become difficult to maintain.
2. Over-Nesting Too Early
Do not create unnecessary complexity.
Start simple.
3. Ignoring Validation Errors
Validation errors are extremely valuable debugging tools.
4. Treating AI Outputs as Trusted Automatically
Always validate generated data.
Nested Models and Production AI Systems
Production AI systems increasingly require:
- structured outputs,
- schema validation,
- typed APIs,
- and reliable automation.
Nested models help organize these systems cleanly.
As workflows scale, nested schemas become increasingly important.
Real-World Use Cases
Nested Pydantic models are commonly used in:
- AI agents,
- retrieval systems,
- workflow orchestration,
- customer APIs,
- automation platforms,
- document extraction,
- analytics systems,
- and multi-agent architectures.
What You Should Learn Next
Recommended next tutorials:
- AI Output Validation Strategies
- Parsing LLM Responses Safely
- Tool Calling Explained
- Building Stateful AI Agents
- Multi-Agent Architectures Explained
These topics build directly on structured schema design.
Final Thoughts
Nested Pydantic models are one of the most important tools for building scalable AI systems.
They transform schemas from:
- flat,
- repetitive structures
into:
- modular,
- reusable,
- hierarchical architectures.
In modern AI engineering, structured workflows increasingly rely on:
- nested schemas,
- validation,
- and typed data contracts.
Pydantic AI strongly embraces this approach because reliable AI systems require more than prompts alone.
They require structure.
Nested models are one of the best ways to create that structure cleanly and safely.
👉 You can experiment with a practical PydanticAI implementation of this concept in the official GitHub repository for the LearnPydanticAI examples: https://github.com/BenardoKemp/learn-pydantic-ai