Overview
Pydantic 2.3, released on October 13, 2023, improves JSON schema generation and adds new validators.
Main Features
Improved JSON schema
JSON schema generation is more spec-compliant and better supports Union types and references.
python
from pydantic import BaseModel
class Address(BaseModel):
street: str
city: str
class User(BaseModel):
name: str
address: Address
schema = User.model_json_schema()
print(schema) # $defs with references
New validators
New field validators are available, including field_validator with wrap mode for full control.
python
from pydantic import BaseModel, field_validator
class Product(BaseModel):
name: str
price: float
@field_validator('price')
@classmethod
def check_price(cls, v: float) -> float:
if v <= 0:
raise ValueError('Price must be positive')
return round(v, 2)
