Skip to content

The validation evolution

Pydantic V2tronger


About me

bg:40% 80%

Marcelo Trylesinski


FastAPI Expert


OSS Maintainer

### Uvicorn ![w:450](https://raw.githubusercontent.com/tomchristie/uvicorn/master/docs/uvicorn.png)
### Starlette ![w:600](https://raw.githubusercontent.com/koddr/starlette-logo/master/src/dark/svg/starlette__full_logo__with_text__dark.svg)

Software Engineer at Pydantic

w:900


What is Pydantic?

w:900


What is Pydantic?

w:900


What's this talk about?


Why did we create V2?


Pydantic Core

github.com/pydantic/pydantic-core


Let's go through some changes


Config -> model_config

w:900


Config -> model_config

w:900


Which settings changed?

w:700


Changes on validators

@validator -> @field_validator

w:900


Changes on validators

@validator -> @field_validator

w:900


Validation mode (before)

w:900


Validation mode (before)

w:900


Validation mode (wrap)

w:900


Validation mode (after)

w:900


Changes on validators

@root_validator -> @model_validator


__root__ -> RootModel

w:700


__root__ -> RootModel

w:700


TypeAdapter

w:900


pydantic.BaseSettings -> pydantic_settings.BaseSettings

github.com/pydantic/pydantic-settings


Modify the JSON schema

w:900


Modify the JSON schema

w:900


Custom Types

w:900


Custom Types

w:900


Pydantic Extra Types

https://github.com/pydantic/pydantic-extra-types


Performance Tips

Use model_validate_json(), and not model_validate(json.loads())

w:900


Performance Tips

TypeAdapter instantiated once

w:900


Performance Tips

TypeAdapter instantiated once

w:900


Performance Tips

Don't do validation when you don't have to - use Any

w:900


Performance Tips

Use Literal, not Enum


Performance Tips

Use TypedDict over nested models


Performance Tips

https://docs.pydantic.dev/latest/concepts/performance


Is V3 planned?


Where to see more about it?

https://docs.pydantic.dev/latest/migration/


Early Access to our Product!

Talk to me to get my business card.


Follow me on YouTube

The FastAPI Expert


Thank You!

marcelotryle.com

Marcelo Trylesinski @marcelotryle Kludex