Models (Pydantic)¶
FastAPI validates request data and serializes responses with Pydantic v2 models. The binary embeds its own
implementation of Pydantic v2's lax mode, with pydantic-core's error types, messages, locations and contexts, so
that a 422 is the same byte for byte. It also covers validators, model_config, computed fields,
TypeAdapter and pydantic-settings.
A Pydantic v2 model with EmailStr, Literal, nested models, Decimal constraints, an alias with
populate_by_name, str_strip_whitespace, a field validator, a model validator and a computed field; then
model_dump with its options. Like every example on this site, it is compiled and compared with FastAPI in CI (how).
from decimal import Decimal
from typing import Literal
from fastapi import APIRouter
from pydantic import BaseModel, ConfigDict, EmailStr, Field, computed_field, field_validator, model_validator
router = APIRouter(prefix="/models", tags=["models"])
class Address(BaseModel):
city: str
country: str = Field(min_length=2, max_length=2, description="ISO 3166 code")
class Customer(BaseModel):
model_config = ConfigDict(str_strip_whitespace=True, populate_by_name=True)
email: EmailStr
name: str = Field(min_length=1, max_length=40)
plan: Literal["free", "pro"] = "free"
tags: list[str] = []
addresses: list[Address] = []
credit: Decimal = Field(default=Decimal("0"), max_digits=8, decimal_places=2)
referrer_id: int | None = Field(default=None, alias="referrerId")
@field_validator("tags")
@classmethod
def normalize_tags(cls, tags: list[str]) -> list[str]:
return sorted({tag.lower() for tag in tags})
@model_validator(mode="after")
def pro_needs_an_address(self):
if self.plan == "pro" and not self.addresses:
raise ValueError("a pro customer needs an address")
return self
@computed_field
@property
def display_name(self) -> str:
return f"{self.name} <{self.email}>"
@router.post("/customers")
async def create_customer(customer: Customer) -> Customer:
return customer
@router.post("/customers/dump")
async def dump_customer(customer: Customer):
return {
"python": customer.model_dump(exclude={"addresses"}),
"json": customer.model_dump(mode="json", by_alias=True, exclude_none=True),
"fields_set": sorted(customer.model_fields_set),
}
What is native¶
- Lax-mode validation with pydantic-core's error types, messages, locations and contexts (speedate for
dates), smart unions (exactness, fields set), enums, literals, nested models, lists/sets/tuples/dicts,
Optional,Fieldconstraints (and pydantic 1'smin_items/max_items, whichField()still maps tomin_length/max_length), aliases (below), defaults anddefault_factory,validate_assignment(an assignment runs the field'sbeforevalidators, its type, itsaftervalidators withinfo.dataholding every other field, then the model'saftervalidators: one of those that raises leaves the value assigned, error at loc(), as pydantic-core does; refused with amode="before"model validator; anaftervalidator's error reports the raw input; amode="before"field validator defined after amode="after"one of the same field is refused),extra=,from_attributes,str_strip_whitespace/to_lower/to_upper,use_enum_values,model_configas a dict orConfigDict, v1class Config(v1-only keys ignored like Pydantic v2 does). Thestr_*settings also apply to the string anEmailStrvalidates. Refused: aMutableSequence[T]orCollection[T]as a field, parameter,TypeAdapteror response type (theAsyncIterable[T]/Iterable[T]return annotation of a generator endpoint is its stream item type, see Responses). Iterable[T]fields as pydantic-core'sValidatorIterator: any iterable is accepted (a str gives its characters, a dict its keys; anything elseiterable_type), the items are validated when reached, once:next(), a loop,list(),model_dump(mode="json")(ValidationError titledValidatorIterator, loc the item's index),model_dump_json()and the response (PydanticSerializationError, a 500). The items are taken when the field is validated (CPython reads a list as it is iterated: a list changed in between differs). Refused: a default other than None (pydantic keeps it unvalidated and warns when dumping it),Field()constraints.model_dump()in python mode (pydantic'sSerializationIterator) raises.- An Enum member given to a scalar field (an ORM enum column read into
status: str...) as pydantic-core takes it: astr/intsubclass member (class X(str, Enum),StrEnum,IntEnum) is its value forstr,int,float,boolandLiteral(anintone isstr(value)in astr), a plain member isstr(value)in astrand its value, unchecked, in an unconstrainedint. uuid.UUIDfields and parameters: a UUID instance, a str in the simple, hyphenated,{braced}orurn:uuid:form, or bytes, with pydantic-core'suuid_type/uuid_parsingerrors (the messages of theuuidcrate pinned by the project's pydantic-core); dumped as the hyphenated str.@field_validator/@validator(after and before, including_x = field_validator(...)(lambda v: ...)),info: ValidationInfoin after validators (info.data: the earlier fields that passed, after their own validators, defaults included;info.field_name; other attributes, andinfoin before validators, are refused), v1values(pydantic's own signature rules:field/configparameters are refused),@model_validator(before/after); validators that raise produce the 422 at their place. Before validators run on the raw input in reverse definition order, like pydantic-core.model_dump(mode=, by_alias=, exclude_none=, exclude_unset=, exclude=, include=)(top-level field names forexclude/include),model_dump_json,model_validate(_json),model_copy(update=, deep=),model_fields_set,model_fields,ValidationError.errors()(URL with the major.minor of the pydantic the project locks, else the one installed next to py2axum;ctx.erroris the exception raised by the validator, rendered as its attributes byjsonable_encoder;include_*options) anderror_count().Model.model_json_schema()(default arguments only), computed at translation time like theGenerateJsonSchemaof pydantic 2.13 and 2.14 (identical on this subset) (key order included; same subset as MCP tool schemas, plusmodel_config extra=). The receiver must be a model class by name, orexpr.attrwhere every value the project binds to an attribute or keywordattris a model class (a tool registry); anything else is refused.Model.model_construct(_fields_set=None, **values)(also read on an instance), as pydantic 2.12 to 2.14 builds it without validation: each field takes itsalias, then its validation aliases (a key ofAliasChoicesis consumed, anAliasPathonly read), then its name, else its default (adefault_factorytaking data gets the fields taken so far); the fields set are those taken, or exactly_fields_set; the keys left are the extras withextra="allow"(not counted inmodel_fields_set, as in pydantic), set the private attributes they name, and are ignored otherwise. Computed fields,model_dump,model_copyand aresponse_modelwork on the instance. Refused at translation: a RootModel, a model withmodel_post_initor a private attribute with a computed default, a field with bothalias=and a differentvalidation_alias=, a required field missing from literal keywords. Differences, raised at run time (TypeError): a required field left out through**values(pydantic leaves it unset, reading it raisesAttributeError), a_fields_setthat is not a set of field names, and dumping a value whose type is not the field's own (astrin anintfield, a dict in a model field: pydantic serializes it as it is, with aPydanticSerializationUnexpectedValuewarning; anintin afloatfield is dumped as pydantic does).model_extra(__pydantic_extra__: the extras withextra="allow", else None).model_config frozen=True(assignment raisesfrozen_instance; frozen models hash by value),Field(validate_default=True), field options given inAnnotated[T, Field(...)].- Aliases:
alias=,validation_alias=(a key,AliasPath,AliasChoices: the first path found wins, a missing field is reported at the first one, the input keys a field did not consume are its extras) andserialization_alias=apart;validate_by_alias/validate_by_name(populate_by_name),serialize_by_alias(each model's own setting whenby_alias=is not given).alias_generator=:pydantic.alias_generators(to_camel,to_pascal,to_snake), a lambda or a function of the name made of string operations (if,for, assignments,return), anAliasGenerator(alias=, validation_alias=, serialization_alias=)of those, orOther.model_config["alias_generator"], evaluated at translation time on the field names (a field that declares an alias keeps it, the generator fills the others, as pydantic does); anything else is refused. A configuration may spread another model's:ConfigDict(**Base.model_config, ...),{**Base.model_config, ...}. allow_inf_nan=Falseinmodel_config(every float of the model, in its containers too:finite_number);validate_default=False.hide_input_in_errors=True:str()of the model'sValidationErrorwithoutinput_value/input_type(errors()and the 422 body keep the input, as in pydantic).Field(exclude=True)(never dumped),repr=False,frozen=True(an assignment raisesfrozen_field),multiple_of=(onint/float, checked before the bounds; a float within|x| / 1e9, like pydantic-core),union_mode="left_to_right",discriminator="field"(a discriminated union, possibly nested in another one: the tag picks the member,union_tag_not_found/union_tag_invalidotherwise; stringLiteraltags only;Discriminator(fn)with members taggedAnnotated[T, Tag('x')], the function called on the input, and itscustom_error_type/_message/_contextof strings),model_config str_min_length=/str_max_length=,StringConstraints(ascii_only=True)(pydantic 2.14). A str is checked as pydantic-core does: stripped, thenascii_only, the lengths and the pattern, and only then lowered or uppercased (str_to_upperwith a lowercasepatternaccepts lowercase input). Thestr_*anduse_enum_valuessettings of a model apply to the items and keys of its containers too.datetimeanddatebounds (Field(gt=datetime(2000, 1, 1)),annotated_types.Gt(...), a module constant;tzinfo=timezone.utcortimezone(timedelta(...))),AwareDatetime,NaiveDatetime: checked after the type in pydantic-core's order (le,lt,ge,gt, then the timezone), compared as speedate does (two aware values by their instants, otherwise by their wall times), written in the error as speedate writes them. A bound of another type,PastDate/FutureDatetimeand zone-aware bounds (ZoneInfo) are refused.- Strict mode (
model_config strict=True,Field(strict=)), with pydantic-core's python-mode rules (FastAPI validates a decoded JSON body as Python data): only the exact type passes, with the type's own error (int_type,date_type,tuple_type,is_instance_offorDecimal/UUID/enums...); anintis a validfloat. A model's strict config covers its fields' items, not the fields of another model it holds;Field(strict=True)covers the field's own type (a list's items keep the model's mode). Refused:Field(strict=True)on a union,Field(strict=False)on a container in a strict model. A strict URL refuses what theurlcrate reports as a syntax violation (url_syntax_violation), as pydantic-core does. Field(default_factory=lambda data: ...): called with the fields validated before it; after an error of an earlier field it is not called (default_factory_not_called; before pydantic 2.14 amissingfield does not count: the factory is called with what was validated, and what it raises is a 500).- Validators in a field's annotation (
Annotated[T, AfterValidator(f)],BeforeValidator,PlainValidator,WrapValidator, directly or through a type alias), run like@field_validators defined before the class's: a function, a lambda,info(data,field_name,context:None) in an after validator.@field_validator(mode="plain")andPlainValidatorreplace the type (and the validators before them);mode="wrap"andWrapValidatorget ahandlerthat validates against the type (aValidationErrorwith locations from the value, titleValidatorCallable; re-raised, its errors land under the field). Refused: a wrap validator after other validators of the field, anything butAfterValidatorafter aWrapValidatorin the annotation, a validator of an item inside a container. PydanticCustomError(type, template, context)raised by a validator: its own error (the template filled from the context: a str as is, a bool as an int, other values byrepr);PydanticUseDefaultraised by a before or wrap validator: the field's default (still inmodel_fields_set; no default is pydantic-core's schema error, a 500).model_dump(context=)reaches the serializers'info.context.- Serializers:
@field_serializer(*fields, mode="plain"|"wrap", when_used=)(instance, class or static method,infowithmode,field_name,context:None),PlainSerializer/WrapSerializerin the annotation,@model_serializer(mode="plain"|"wrap"),Field(exclude_if=),model_dump(exclude_defaults=True)(a field with a@field_serializeris never left out by it, as in pydantic-core). Serializers run while dumping and must not await; their result is dumped by its value's type (the return annotation is not used). - Types:
tuple[A, B, C],tuple[()](missingper position,too_longalone),collections.deque[T],pydantic.SecretStr(shown**********instr,reprand JSON dumps,get_secret_value()),UUID1...UUID8(uuid_version),fractions.Fraction(CPython 3.12+'s grammar, exact from a float; dumped as itsstr()in both modes),complex(CPython'scomplex()grammar;repr()like CPython, JSON like pydantic-core:1e20is written100000000000000000000),Callable(callable()),SkipValidation[T](the value as given),types.EllipsisTypeandMISSINGwithout a default (no JSON value is one), a plain class of the project (arbitrary_types_allowed: anisinstancecheck; one defining__get_pydantic_core_schema__: see below), a type alias assigned after the class that uses it (MyInt = intunderfrom __future__ import annotations),RootModel[T]/RootModelwithroot: T(the input and the dump are the root value; validators, serializers and aliases on it are refused),frozenset[T],collections.Counter[K],collections.OrderedDict[K, V],collections.abc.Sequence[T](a str refused withsequence_str, a tuple stays a tuple),type[Model],type[BaseModel],type[PlainClass](is_subclass_of). A mutable default evaluated once (deque(), a model instance) is copied for each instance, like pydantic'ssmart_deepcopy. - Dataclasses (
dataclasses.dataclass,pydantic.dataclasses.dataclass) as a request body, a parameter, aresponse_modelor a model's field: validated like pydantic validates a dataclass (typed fields, extra inputs ignored,dataclass_typefor anything but a dict or an instance, an instance kept as it is), into an instance of the class (its repr and methods). A stdlib dataclass called in the code does not validate, as in Python; calling a pydantic dataclass (which validates its arguments) and its decorator options are refused. A stdlib dataclass inside a model is validated with that model's config, as pydantic does (str_to_lower,str_strip_whitespace,str_to_upper,str_min_length,str_max_length,use_enum_valuesreach its fields, nested ones included), unless it has its own@with_config(...)(those keys only); under a model withstrict=orextra=it is refused (pydantic would validate the dataclass itself differently). Atyping.NamedTupleas a validated type is refused (pydantic validates it as call arguments). pydantic.JsonandJson[T]: a str or bytes input parsed by jiter, pydantic-core's own parser (its error messages and positions:json_invalid,Invalid JSON: expected ident at line 1 column 2;inf/nanaccepted), anything elsejson_type; the parsed value is then validated asT. An integer beyond 64 bits in it is the runtime's usual difference.__get_pydantic_core_schema__on a field's type (a classmethod) or on anAnnotatedmetadata (an instance of the class, a dataclass whose fields are constants, or the class), andGetPydanticSchema(lambda tp, handler: ...), on a field's top-level annotation: the hook is read at translation, never run, and the schema it returns becomes the equivalent annotation. Supported:handler(source),handler(int)(a builtin scalar), the scalar schemas with their constraints (str_schema(max_length=),int_schema(ge=)...), the after, before, wrap and plain validator functions (no_info_*/with_info_*, withserialization=),chain_schema([schema, plain validator...]),plain_serializer_function_ser_schema(f), assignments,schema['max_length'] = 10, andif/assertknown at translation (schema['type'] != 'str', a field of the metadata dataclass). The functions may be lambdas, functions of the same module, and the class's methods (cls._validate,self.validate, called through a lambda: a traceback shows that<lambda>frame first). Refused: unions,is_instance_schema,json_or_python_schema, the hook in a nested type (list[T]),model_json_schema()of such a model,info_arg=True, any other statement or constructor. A plain object left in a model field (an after validator's result under a scalar type, anarbitrary_types_allowedvalue) fails a JSON dump as in pydantic-core (PydanticSerializationError: Unable to serialize unknown type, a 500); the serializer warning pydantic prints before it is not reproduced.- Generic models (
class Response(BaseModel, Generic[T]),class Response[T](BaseModel), a subclass ofParent[T]): eachResponse[int]used is specialized at translation time, named like pydantic does (Response[int], in errors andtype(m).__name__); the generic class used as such validates a parameter as its default, else its bound, elseAny. The arguments may be written in another module than the model's (Response[Role]withRoleimported where it is used); TypeVar constraints are refused. - JSON dumps (responses,
model_dump(mode="json"),model_dump_json) write afloatfield holding an int (a default0) as a float, like pydantic-core's typed serializer;model_fields_setincludes the extras of anextra="allow"model. TypeAdapter(T):validate_python(ORM objects withfrom_attributes, iterables),validate_json,dump_python,dump_json, for types written in the source or known at run time.EmailStr(rule-by-rule port of email-validator, same messages, except IDNA encoding and NFC normalization of internationalized domains, which are accepted and lowercased),AnyUrl/AnyHttpUrl/HttpUrl/RedisDsn/AmqpDsn/KafkaDsn/MySQLDsn/MariaDBDsn/CockroachDsnand the multi-hostPostgresDsn/MongoDsn(parsed with theurlcrate like pydantic-core, type defaults, attributes, same errors). A multi-host DSN type holding more than one host (postgresql://h1:5432,h2:5432/db) is not supported: validating one answers 500, and the multi-host types havehosts()but nohost/port/username/password, as in pydantic.- Strict and constrained aliases:
StrictStr/StrictInt/StrictFloat/StrictBool/StrictBytes,PositiveInt/NegativeInt/NonNegativeInt/NonPositiveInt(and the*Floatones), and the call formsconstr(...),conint(...),confloat(...),condecimal(...),conlist(T, ...),conset(T, ...)translate as theAnnotated[...]they stand for (unique_items=,regex=,allow_inf_nan=are refused).pathlib.Pathfields accept astror aPath(path_typeotherwise) and dump as astrin JSON. - pydantic-settings
BaseSettings: environment variables (not.envfiles),validate_default=True,env_prefix,case_sensitive(names compared case-insensitively by default, the last of two names that differ only in case wins),env_parse_none_str(an environment value equal to it, case included, isNone; init keywords are not parsed),env_ignore_empty(an empty variable is as if unset), JSON values for container/model fields (a union with such a member keeps the raw string when the JSON does not parse); otherenv_*options and_env_*init keywords are refused; a class body run like a script (class-levelif, attributes reading earlier ones) is evaluated once. decimal.Decimalfields: lax validation like pydantic-core (a float through itsrepr, a string throughDecimal(str), finite values only),max_digits/decimal_placeson the normalized value, thenle/lt/ge/gt(int or float literals); dumped as a string in JSON mode.@computed_field(bare, over@propertyor alone): serialized after the fields and the extras,exclude_noneapplies,exclude_unsetdoes not,repr()shows them. The property must not await.- A
@classmethodoverride ofmodel_validate(called by name,Model.model_validate(x)) callingsuper().model_validate(...)orsuper(Model, cls): the parent's (a project model's override, else BaseModel's),clsstill the class called.super(cls, cls)is accepted when no project model subclasses the class (else its target depends on the class called: refused). - A model's own
def __init__(self, **data)callingsuper().__init__(**data): run byModel(...); validation from attributes (from_attributes, ORM objects) or of an existing instance skips it, as in pydantic-core. - Private attributes (
_name: T = PrivateAttr(default=/default_factory=), any_name): per instance, never validated nor dumped; a_nameassigned on an instance is not dumped either (unlessextra="allow"). A computed default (PrivateAttr(default_factory=datetime.now)) is set once the fields are valid. Model = create_model("Model", ...)at module level: compiled as the class it creates. Fields asname=type,name=(type, default)orname=(type, Field(...)),__base__=a class,__doc__=, and in__namespace__=(pydantic 2.14+: before, pydantic ignores it, and so does the binary) /__validators__=the entriesfield_validator(...)(f),model_validator(mode=...)(f)andcomputed_field(property(f))(fa module-level function), bound as pydantic binds them. Other options (__config__, a computed name...) are refused.def model_post_init(self, context): called once the fields are valid, after the private attributes' defaults and before the after validators, withcontextNone; aValueErroror anAssertionErrorit raises is a validation error of the model (loc()of the model, its input), anything else propagates. Inherited. An instance of the response model's own class is serialized as returned, private attributes included.
The binary does not read .env files: see Environment variables.
- model_config val_json_bytes= / ser_json_bytes= ("utf8", "base64", "hex"): a str input of a
bytes field of the model (inside containers too) decoded as pydantic-core does (URL-safe base64, the
standard alphabet when the input holds + or /, padding optional; its error messages, which differ
between pydantic-core 2.46 and 2.50), the JSON dump encoded (URL-safe base64 with padding, lowercase hex).
A nested model uses its own config.
What stays in Python¶
- Not supported:
@computed_field(...)options, nestedexclude/includedicts, core schemas outside the subset above,create_model,ImportString. - A union with a member that has validators (directly or in a nested model) is refused: Pydantic would try the next member when one raises.
Differences¶
- A plain Enum member whose value is not an integer, given to a constrained
int, reportsint_parsingwhere pydantic-core reportsint_parsing_size. - With
extra="allow", an extra key named like a computed field shadows it on attribute access and inmodel_dump_json(Pydantic writes both keys). jsonable_encoderof an integralDecimalbeyond 64 bits gives a float (Python: an int).- Validating a model that defines its own
__init__from a dict (request body,model_validate(dict), nested), where pydantic-core calls the__init__, raises a py2axum RuntimeError (500) instead. - A
Fractionorcomplexwritten with non-ASCII digits (CPython reads any Unicode decimal digit) is a 500. - Arithmetic on a
Fractionorcomplexvalue in the application's own code is not translated (a TypeError).