Overview
Pydantic 2.8, released on August 2, 2024, adds validation warnings and improves tagged unions.
Main Features
Warnings and tagged unions
Validation warnings report non-blocking issues. Tagged unions benefit from clearer syntax.
python
from pydantic import BaseModel, Field
from typing import Literal, Union, Annotated
class Cat(BaseModel):
type: Literal['cat'] = 'cat'
meows: bool = True
class Dog(BaseModel):
type: Literal['dog'] = 'dog'
barks: bool = True
Animal = Annotated[
Union[Cat, Dog],
Field(discriminator='type'),
]
class Household(BaseModel):
animal: Animal
h = Household(animal={'type': 'cat', 'meows': True})
print(h.animal) # type='cat' meows=True
