Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pydantic · Pipe

validate_assignment

validate_assignment enables validation when fields are changed after a Pydantic model is created. It defaults to False, so post-construction assignments are otherwise not revalidated.

How it works

Set validate_assignment=True in a model’s configuration to validate values assigned after initialization. Pydantic then applies validation when model attributes change, rather than validating only during model creation.

Syntax

class User(BaseModel, validate_assignment=True):
    ...

# or
model_config = ConfigDict(validate_assignment=True)

Key points

  • validate_assignment controls whether Pydantic checks values assigned after the model has been created.
  • The option is disabled unless you explicitly configure it.
  • Initial model construction is validated independently of assignment-validation configuration.
  • Without this option, mutating a model does not trigger another validation pass.
  • The configuration can be supplied through the model class declaration or through model_config.
  • When enabled, later attribute changes receive validation in addition to construction-time checks.

Common mistakes

  • Assignment validation is disabled by default, so changing an attribute otherwise bypasses model validation.

Example: Validate assignments after model creation

from pydantic import BaseModel, ValidationError

class User(BaseModel, validate_assignment=True):
    name: str
    age: int

user = User(name="Ada", age=36)
user.age = "37"
assert user.age == 37

try:
    user.age = "not an integer"
except ValidationError:
    print("assignment rejected")

The first assignment is converted to an integer; the second raises a validation error.

When to use it

Keep updates type-safe

Mutable models whose fields may change after initialization.

Preserve validation guarantees

Applications that modify validated state over time.

Reject invalid mutations

Models where accidental invalid assignments should fail immediately.

Interview questions

QWhat does validate_assignment change?

By default, Pydantic validates data when the model is created, but not when an attribute is changed. Enabling validate_assignment makes later assignments undergo validation too.

Follow-up: How can it be enabled? Use validate_assignment=True as a model class keyword argument or set it in model_config.

QHow do you enable assignment validation?

It can be enabled directly in the class definition with class User(BaseModel, validate_assignment=True). This causes assignments such as user.age = ... to be validated after construction.

Follow-up: What happens without the setting? An invalid value can be assigned without revalidation.

QWhat is the default behavior?

The setting is a configuration option whose default is False. With the default, validation occurs only during model creation; with it enabled, validation also occurs when model data changes.

Follow-up: Why might this matter? It preserves the model’s validation guarantees across its full mutable lifetime.

  • BaseModel
  • ConfigDict
  • model_config
  • ValidationError

Source: Configuration - Pydantic. Checked 2026-10-15.