new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Pydantic · all subjects

custom validators & serializers

11 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Field serializers and computed fields in V2

Pydantic V2 introduces @field_serializer, @model_serializer, and @computed_field decorators to customize serialization. These replace the V1 json_encoders config setting, which is now deprecated due to performance overhead and implementation complexity.

@validator decorator deprecated, use @field_validator

In Pydantic V2, the @validator decorator is deprecated and should be replaced with @field_validator, which provides new features and improvements.

@field_validator does not have each_item argument

In Pydantic V2, the @field_validator decorator does not have an `each_item` keyword argument. To apply validators to items within a generic container, annotate the type argument instead: `list[Annotated[int, Field(ge=0)]]`.

Deprecated @validator cannot access field or config arguments

In Pydantic V2, even if continuing to use the deprecated @validator decorator, you can no longer add `field` or `config` arguments to the validator function signature. Migrate to @field_validator if you need access to these.

Validator with always=True applies standard validators to defaults

In Pydantic V2, when using `always=True` on a validator, standard validators for the annotated type are also applied to defaults, not just custom validators. Use `validate_default=True` in Field instead for better control.

@root_validator deprecated, use @model_validator

In Pydantic V2, the @root_validator decorator is deprecated and should be replaced with @model_validator, which provides new features and improvements. Allowed signatures have changed.

@model_validator may receive instance or dict

In Pydantic V2, under some circumstances (such as assignment when `model_config['validate_assignment'] is True`), the @model_validator decorator receives an instance of the model rather than a dict of values. Be prepared to handle both cases.

Deprecated @root_validator cannot use skip_on_failure=False

In Pydantic V2, when using the deprecated @root_validator decorator, you can no longer run with `skip_on_failure=False`. This must be explicitly set to `True` if needed.

@field_validator info parameter provides config and field_name

In Pydantic V2, the @field_validator decorator receives an info parameter (ValidationInfo) that provides access to configuration via `info.config` and field name via `info.field_name`. Use these to access field information from `cls.model_fields[info.field_name]`.

TypeError not converted to ValidationError in validators

In Pydantic V2, when a TypeError is raised within a validator function, it is no longer converted to a ValidationError. This applies to all validation decorators.

allow_reuse keyword argument no longer necessary

In Pydantic V2, the `allow_reuse` keyword argument for validators is no longer necessary. The approach to detecting repeatedly defined functions has been overhauled to only error for redefinition within a single class. Simply delete the `allow_reuse=True` argument.

Give your agent this brain