Pydantic is a data validation library. It uses/extends python type hints to work during runtime instead of statically.
Pydantic recently went from version 1 to 2. One of the changes was to split pydantic in a few packages. Pydantic core is now partially written in Rust! Some other changes:
In Version 1 you could add a
Config class inside your model class, but
it was sensitive to spelling mistakes. Now they use a
attribute that prevents errors.
As they were breaking the API anyway, they took the opportunity to change lots of names to make them clearer.
@validator is now
@field_validator which accepts extra attributes,
for instance for doing a special check before or after the regular
validation. “After” validations are handy as you can run a check after
pydaantic has ensured it was the correct type, this prevents lots of error
handling in your custom validation.
BaseSettings is now in a separate python package as it got a bit too
__something__ methods were changed to give you more options to
change pydantic’s behaviour.
Some performance tips (see also https://docs.pydantic.dev/latest/concepts/performance/ ):
model_validate_json() on a json string instead of
model_validate(json.loads(...)), as the string gets send to Rust
directly, which is quicker.
TypeAdapter can help you speed up your code if you instantiate a similar
type multiple times.
TypedDict instead of nested models.
If there is a version 3, it won’t be as big as a change as from 1 to 2.
My name is Reinout van Rees and I work a lot with Python (programming language) and Django (website framework). I live in The Netherlands and I'm happily married to Annie van Rees-Kooiman.
Most of my website content is in my weblog. You can keep up to date by subscribing to the automatic feeds (for instance with Google reader):