The validation evolution

Pydantic V2tronger

@marcelotryle

About me

bg:40%

Marcelo Trylesinski

@marcelotryle

FastAPI Expert

@marcelotryle

OSS Maintainer

Uvicorn

Starlette

@marcelotryle

Software Engineer at Pydantic

@marcelotryle

What is Pydantic?

@marcelotryle

What is Pydantic?

@marcelotryle

What's this talk about?

@marcelotryle

Why did we create V2?

@marcelotryle

Pydantic Core

github.com/pydantic/pydantic-core

@marcelotryle

Let's go through some changes

@marcelotryle

Config -> model_config

@marcelotryle

Config -> model_config

@marcelotryle

Which settings changed?

@marcelotryle

Changes on validators

@validator -> @field_validator

@marcelotryle

Changes on validators

@validator -> @field_validator

@marcelotryle

Validation mode (before)

@marcelotryle

Validation mode (before)

@marcelotryle

Validation mode (wrap)

@marcelotryle

Validation mode (after)

@marcelotryle

Changes on validators

@root_validator -> @model_validator

@marcelotryle

__root__ -> RootModel

@marcelotryle

__root__ -> RootModel

@marcelotryle

TypeAdapter

@marcelotryle

pydantic.BaseSettings -> pydantic_settings.BaseSettings

github.com/pydantic/pydantic-settings

@marcelotryle

Modify the JSON schema

@marcelotryle

Modify the JSON schema

@marcelotryle

Custom Types

@marcelotryle

Custom Types

@marcelotryle

Pydantic Extra Types

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

@marcelotryle

Performance Tips

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

@marcelotryle

Performance Tips

TypeAdapter instantiated once

@marcelotryle

Performance Tips

TypeAdapter instantiated once

@marcelotryle

Performance Tips

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

@marcelotryle

Performance Tips

Use Literal, not Enum

@marcelotryle

Performance Tips

Use TypedDict over nested models

@marcelotryle

Performance Tips

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

@marcelotryle

Is V3 planned?

@marcelotryle

Where to see more about it?

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

@marcelotryle

Early Access to our Product!

Talk to me to get my business card.

@marcelotryle

Follow me on YouTube

The FastAPI Expert

@marcelotryle

Thank You!

FastAPIExpert.com

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" referrerpolicy="no-referrer" />

Marcelo Trylesinski
@marcelotryle
Kludex

@marcelotryle

Reference: https://github.com/koddr/starlette-logo

Explain that I'm going to go through some Pydantic features, and explain why/how they changed.

Explain why V2 was released.

Explain or show things that were deprecated/removed/changed on the `Config`. Explain also the reason for it.

This is the Pydantic protocol - other packages should implement this.

Uncertain policy to what gets in yet. Packages should be able to use the Pydantic protocol.

Use the Pydantic documentation page to explain this one.

Explain that at some point next year we are going to have the next major. But... The changes are changes are going to be far less disruptive in comparison to V1->V2.