You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Page in the docs with tips on using pydantic in the most performant way.
Should start with a big notice saying something like "In most cases pydantic won't be your bottle neck, only follow this if you're sure it's necessary blah blah blah".
Use model_validate_json() not model_validate(json.loads(...)) (👀 FastAPI)
TypeAdapter instantiated once - generally avoid constructing validators and serializers more than necessary
Sequence vs list/tuple - Mapping vs dict
Don't do validation when you don't have to - use Any to keep the value unchanged
generally avoid recording extra information via subclasses of primatives
Use tagged union, not union
Use Literal not Enum (not sure how big this effect is)
use TypedDict over nested models
Avoid wrap validators if you really care about performance
Another thing you can do I do the simple calculations first, only to the complicated once when you have to. another is object pooling arrays and time slicing.
Looks like this should have been closed by @Kludex's #7567 PR. Closing now, as the minimum reqs for this page have been met. I think we should open a new issue with new requests for this page if/when they're desired.
Page in the docs with tips on using pydantic in the most performant way.
Should start with a big notice saying something like "In most cases pydantic won't be your bottle neck, only follow this if you're sure it's necessary blah blah blah".
model_validate_json()
notmodel_validate(json.loads(...))
(👀 FastAPI)TypeAdapter
instantiated once - generally avoid constructing validators and serializers more than necessarySequence
vslist
/tuple
-Mapping
vsdict
Any
to keep the value unchangedLiteral
notEnum
(not sure how big this effect is)TypedDict
over nested modelsAnything else?
Selected Assignee: @Kludex
The text was updated successfully, but these errors were encountered: