Overview

Pydantic 2.9, released on October 9, 2024, introduces graph validation to handle cyclic and nested data structures.

Main Features

Graph validation

Graph validation enables validating structures with circular references, useful for trees and data graphs.

python
from __future__ import annotations
from pydantic import BaseModel

class Node(BaseModel):
    value: int
    children: list[Node] = []

tree = Node(
    value=1,
    children=[
        Node(value=2),
        Node(value=3, children=[Node(value=4)]),
    ],
)
print(tree.model_dump())

Sources