skip to content

Pydantic

Python validation driven by type hints: models and adapters that parse, coerce and serialize, with settings built on the same machinery. Asked because FastAPI and many data tools run on Pydantic.

on this pageshow

explore

questions

page 1 of 2

In Pydantic 2, how do required, optional and defaulted fields differ on a BaseModel, and does `X | None` make a field optional?

level: juniorimportance: must knowfreq 75%

answer

  1. type versus default
  2. None allowed is not omittable
  3. error type missing
  4. version 1 implicit None default

basics

~20 s

In Pydantic 2 a field is required unless it has a default. note: str | None is still required but accepts None; only note: str | None = None may be omitted. Version 1 gave Optional fields an implicit None default.

solid answer

~40 s

In Pydantic 2, **requiredness comes from the default, and nullability comes from the type**. `order_id: int` is required and rejects `None`; `currency: str = 'EUR'` may be omitted; `note: str | None` must still be present in the input, but `null` is an accepted value; `coupon: str | None = None` may be omitted and also accepts an explicit `null`. A missing required field produces a `ValidationError` entry with type `missing`. The trap is version 1, where an `Optional` or `Any` field without a default silently defaulted to `None`; version 2 dropped that so the rules more closely match dataclasses. `Field(...)` without a `default` or `default_factory` keeps a field required even though something is assigned.

code

python · 21 lines
python
from pydantic import BaseModel, ValidationError


class Order(BaseModel):
    order_id: int
    note: str | None
    coupon: str | None = None
    currency: str = 'EUR'


try:
    Order(order_id=1)
except ValidationError as exc:
    print([(e['loc'], e['type']) for e in exc.errors()])
#> [(('note',), 'missing')]

order = Order(order_id=1, note=None)
print(order)
#> order_id=1 note=None coupon=None currency='EUR'
print(sorted(order.model_fields_set))
#> ['note', 'order_id']

go deeper

for a junior

Recall the rule: no default means required, and the type decides whether None is allowed. Be ready to name the error type missing.

for a middle

Explain the required-but-nullable case, why Field() alone keeps a field required, and why defaults are not validated unless validate_default is set.

for a senior

Show how the version 1 implicit None default breaks silently after an upgrade, and how model_fields_set separates an explicit null from an omitted key.

for a principal

Argue when a contract should force clients to send an explicit null versus allowing omission, and how that choice is written into shared request models.

## Two separate questions: may it be omitted, may it be null? A Pydantic 2 model is a class that inherits from `BaseModel` and declares its **fields** as annotated class attributes. Every field answers two independent questions, and Pydantic takes each answer from a different place: - **Required or not** is decided by whether the field has a **default** (a plain assignment, `Field(default=...)` or `Field(default_factory=...)`). No default means the key must be present in the input. - **Nullable or not** is decided by the **type annotation**. `str | None` (or `Optional[str]`, which means the same thing) accepts `None` as a valid value; plain `str` rejects it with the error type `string_type`. Mixing the two up is the most common modelling mistake on this subject. `Optional` in the annotation reads like "optional to send", but in Pydantic 2 it only means "may be null". ## The cases side by side For an order received from a partner: | Declaration | May the key be omitted? | Is `null` accepted? | |---|---|---| | `order_id: int` | no | no | | `currency: str = 'EUR'` | yes, becomes `'EUR'` | no | | `note: str \| None` | **no** | yes | | `coupon: str \| None = None` | yes, becomes `None` | yes | | `gift_wrap: str \| None = 'plain'` | yes, becomes `'plain'` | yes | | `metadata: Any` | no | yes, anything is | The third row surprises people: a nullable field without a default is **required but nullable**. The client must send the key, even if the value is `null`. That is useful when "the partner explicitly had no note" and "the partner forgot the field" should not be confused. ## What a missing field looks like When a required field is absent, validation does not stop at the first problem: it carries on through the other fields, and the `ValidationError` it raises reports each failure found. Its `errors()` method returns a list of dictionaries. For a missing field the entry has: - `type`: `'missing'` - `loc`: a tuple naming the field, for example `('note',)` - `msg`: `'Field required'` Fields keep their declaration order, and so do the errors, which makes the list easy to read. ## `Field()` does not by itself create a default `Field()` attaches constraints and metadata to a field. Assigning it does **not** make the field optional: ```python from pydantic import BaseModel, Field class Customer(BaseModel): name: str = Field(max_length=40) # still required tier: str = Field(default='standard') # optional, defaults to 'standard' ``` You may see `Field(...)` with an ellipsis to stress "required"; the documentation discourages it because static type checkers handle it poorly. The **annotated pattern**, `name: Annotated[str, Field(max_length=40)]`, avoids the confusion entirely: there is no assignment, so nobody reads a default into it. Note one limit: type checkers only understand `default`, `default_factory` and `alias` in the assignment form, so put those there. Two more facts worth knowing: 1. Pydantic **does not validate defaults** unless you ask (`validate_default=True`), so `coupon: str = None` lets an omitted coupon become `None`, yet an explicit `null` from the client fails with `string_type`. Write `str | None = None` instead. 2. Unlike a standard dataclass, a Pydantic model may declare a required field after defaulted ones; field order matters for errors and dumps, not for legality. ## What changed from version 1 In Pydantic 1, a field annotated `Optional[X]` or `Any` received an **implicit default of `None`**, so it could be omitted. Version 2 removed that, and the migration guide calls it a breaking change. Code written for version 1 that relied on omitting an `Optional` field now raises a `missing` error, and the fix is to add `= None` explicitly where omission is really allowed. ## Rules of thumb - Decide omission and nullability separately for every field, then write the declaration that says both. - Use `X | None = None` for "may be left out, and null means nothing". - Use `X | None` with no default when the client must state the value, even as `null`. - After validation, `model_fields_set` tells you which fields were explicitly provided, which is how you separate "sent null" from "left out" once both have become `None`.

  • In Pydantic 2, after validation both a sent `null` and an omitted `coupon` are `None`. How do you tell them apart?
    Read `model_fields_set` on the instance: it holds the names of fields that were explicitly provided, including ones provided as `None`. So `'coupon' in order.model_fields_set` is true for an explicit null and false for an omitted key. Dumping only the provided fields is a separate output option.
  • Why might a team prefer `name: Annotated[str, Field(max_length=40)]` to `name: str = Field(max_length=40)` in Pydantic 2?
    The assignment form looks like a default even though the field stays required, which misleads readers. The annotated form has no assignment, can carry several metadata items, and can be reused as a named type. Keep `default`, `default_factory` and `alias` in the assignment form, because static type checkers only read them there.

  • Annotating a field Optional means the client may leave it out.
  • Assigning Field() to a field always gives it a default value.
  • A str field defaulting to None rejects the model at class definition time.
  • Required fields must be declared before defaulted ones, as in dataclasses.
  • A missing required field raises only the first error and stops validating.

Assumes Pydantic 2; in version 1 an Optional or Any field without a default silently defaulted to None, and version 2 made such fields required.

open as a page

In Pydantic 2, how do model_dump(), model_dump(mode='json') and model_dump_json() differ, and when would you use each?

level: juniorimportance: must knowfreq 74%

basics

~10 s

In Pydantic 2, model_dump() returns a dict that keeps Python values such as datetime, Decimal and tuples; model_dump(mode='json') returns a dict of JSON-safe values; model_dump_json() returns the encoded JSON string itself.

open as a page

In Pydantic 2, how does a pydantic-settings BaseSettings class fill its fields from environment variables, and what happens when a required one is missing?

level: juniorimportance: must knowfreq 72%

basics

~20 s

Instantiating a BaseSettings subclass reads every field not passed as a keyword argument from an environment variable named after the field, matched case-insensitively, and validates it by type; a missing required field raises ValidationError on the spot.

open as a page

In Pydantic 2, what replaced the version 1 model methods parse_obj, parse_raw, dict, json and from_orm, and do the old names still work?

level: juniorimportance: must knowfreq 70%

basics

~10 s

Pydantic 2 put the model API under a model_ prefix: parse_obj became model_validate, parse_raw model_validate_json, dict and json model_dump and model_dump_json, and from_orm model_validate with from_attributes. Old names remain as warning shims.

open as a page

In Pydantic 2, what should a custom validator raise to reject a value, and what does the resulting ValidationError contain?

level: juniorimportance: must knowfreq 66%

basics

~10 s

A Pydantic 2 validator should raise ValueError (or PydanticCustomError); Pydantic collects it with other failures into one ValidationError whose errors() list gives each problem's type, loc, msg and input.

open as a page

In Pydantic 2, how do exclude_unset, exclude_none and exclude_defaults differ, and which one lets a PATCH handler tell absent fields from explicit nulls?

level: middleimportance: must knowfreq 70%

basics

~20 s

exclude_unset drops fields the caller never supplied, tracked in model_fields_set; exclude_none drops every None, including an explicit null; exclude_defaults drops values equal to the default. A PATCH handler needs exclude_unset, so a sent null still clears the field.

open as a page

In pydantic-settings, how do env_prefix and env_nested_delimiter decide which environment variable fills a nested database host field?

level: middleimportance: must knowfreq 62%

basics

~10 s

env_prefix is prepended to every variable name and env_nested_delimiter splits a name into nested keys, so with APP_ and __ the variable APP_DATABASE__HOST fills settings.database.host.

open as a page

In Pydantic 2's default lax mode, which conversions does it apply to int, bool and str fields, and which inputs does it refuse?

level: middleimportance: must knowfreq 66%

basics

~20 s

Pydantic 2's lax mode turns digit strings and whole-valued floats into int and a fixed word list ('true', 'yes', 'on', '1' and opposites) into bool, but rejects fractional floats for int and, since version 2, numbers for str.

open as a page

When a Pydantic 1 model moves to Pydantic 2, how do @validator, @root_validator, class Config and orm_mode translate, and what breaks if they stay?

level: middleimportance: must knowfreq 66%

basics

~10 s

@validator becomes @field_validator, with pre=True as mode='before'; @root_validator becomes @model_validator; class Config becomes model_config = ConfigDict(...); orm_mode becomes from_attributes. Leftover decorators warn or fail at import; a leftover orm_mode is ignored after a warning.

open as a page

In Pydantic 2, when do you use field_validator versus model_validator, and how would you enforce a check-out-after-check-in rule?

level: middleimportance: must knowfreq 74%

basics

~10 s

Pydantic 2's field_validator checks one field's value; model_validator sees the whole object. A check-out-after-check-in rule fits model_validator(mode='after'), which compares typed dates and reports its error with an empty loc.

open as a page

In Pydantic 2, how do you make an order model accept a partner's camelCase keys while your code keeps snake_case field names?

level: middleimportance: should knowfreq 60%

basics

~10 s

Give fields aliases: Field(alias=...) or validation_alias per field, or alias_generator=to_camel in model_config for all of them. Pydantic 2 then validates by alias only, so add validate_by_name=True if code must also construct with snake_case names.

open as a page

In Pydantic 2, why is `items: list[LineItem] = []` safe on a BaseModel, and when do you still need `default_factory`?

level: middleimportance: should knowfreq 55%

basics

~20 s

Pydantic 2 deep-copies an unhashable default such as [] for each instance, so instances never share it. Use default_factory when the default must be computed per instance, like an id or a timestamp, or derived from earlier fields.

open as a page

In Pydantic 2, when would you validate with a TypeAdapter instead of a BaseModel, and how should you create it?

level: middleimportance: should knowfreq 50%

basics

~20 s

Use a TypeAdapter in Pydantic 2 to validate a type that is not a BaseModel, such as list[LineItem], a union or a TypedDict. Building it compiles a schema, so create it once at module level and reuse it.

open as a page

In Pydantic 2, why does model_dump() emit snake_case keys for a model whose fields have camelCase aliases, and how do you publish camelCase JSON?

level: middleimportance: should knowfreq 58%

basics

~10 s

Pydantic 2 validates by alias by default but serializes by field name: by_alias is False unless you pass by_alias=True on the dump call or set serialize_by_alias=True in model_config, available since 2.11.

open as a page

In Pydantic 2, when do you reach for field_serializer, model_serializer or computed_field to shape output, and what does each cost you?

level: middleimportance: should knowfreq 52%

basics

~10 s

In Pydantic 2, field_serializer changes how one field's value is written, model_serializer changes the whole model's output, and computed_field adds a property to the output. Each makes the dump differ from what validation accepts.

open as a page

In pydantic-settings, what does each Settings() call cost, and how should a service load its settings once per process?

level: middleimportance: should knowfreq 50%

basics

~10 s

Each Settings() call re-reads the environment, the .env file and secret files and revalidates every field, so a service builds one instance at startup, often behind functools.lru_cache, and reuses it.

open as a page

In Pydantic 2, what does typing a settings field as SecretStr keep out of logs, and where can the secret still leak?

level: middleimportance: should knowfreq 58%

basics

~10 s

SecretStr masks the value as ********** in repr, str and JSON dumps, so printing settings is safe; the secret still leaks through get_secret_value() output you log, ValidationError input values, and the settings debug log.

open as a page

In Pydantic 2, how do you turn on strict mode for a whole model, for one field, or for a single validation call?

level: middleimportance: should knowfreq 50%

basics

~10 s

Pydantic 2 offers three scopes: ConfigDict(strict=True) for a model, Field(strict=True) or StrictInt-style types for one field, and strict=True on model_validate or a TypeAdapter method for one call.

open as a page

In Pydantic 2, how do you replace a version 1 model whose only field was __root__ holding a list, and what changes for its callers?

level: middleimportance: should knowfreq 25%

basics

~10 s

Subclass RootModel[list[Item]]: the value moves from root to .root, and validation and dumping take and return the bare list. A field named root now fails at class definition; TypeAdapter(list[Item]) suits lists needing no class.

open as a page

In Pydantic 2, how do you make a phone-number validator reusable across models, and when is @field_validator still the better choice?

level: middleimportance: should knowfreq 42%

basics

~10 s

In Pydantic 2, define an alias such as PhoneNumber = Annotated[str, AfterValidator(normalise_phone)] and use it in any model or list; keep @field_validator for one rule over several fields of one model.

open as a page

Why does a Pydantic 2 order model with `extra='forbid'` silently drop a misspelled key inside a nested line item, and how do you fix it?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Pydantic 2 does not propagate model_config into nested Pydantic models: each model keeps its own configuration, and the line item still uses the default extra='ignore'. Put extra='forbid' on a shared base class that every model inherits.

open as a page

In Pydantic 2, an event dumped with model_dump_json() fails or comes back different when read with model_validate_json(); what breaks the round trip, and how do you guard it?

level: seniorimportance: should knowfreq 36%

basics

~20 s

A Pydantic 2 dump need not validate back: input-only aliases, computed fields under extra='forbid', custom serializers, NaN written as null, bytes encodings, Json[T] and subclass fields all differ. Guard it with a round-trip test per published model.

open as a page

In pydantic-settings, why does an unknown line in the .env file stop startup with extra_forbidden, while an unknown environment variable is ignored?

level: seniorimportance: should knowfreq 38%

basics

~20 s

The environment source looks up only declared fields' names, but the dotenv source passes .env entries no field uses into the model as input, and BaseSettings defaults to extra='forbid', so an undeclared entry fails validation.

open as a page

In pydantic-settings, a database password moved to a file in secrets_dir, yet production still connects with an old value - what in the source priority explains it?

level: seniorimportance: should knowfreq 52%

basics

~10 s

By default pydantic-settings ranks initialiser arguments, then environment variables, then the .env file, then secrets_dir files, then defaults; a leftover password variable or .env entry therefore shadows the new secret file.

open as a page

In Pydantic 2, why would you validate three payment event kinds as a discriminated union on a Literal tag rather than a plain union?

level: seniorimportance: should knowfreq 42%

basics

~10 s

A Pydantic 2 discriminated union reads one tag field, typed as a Literal on each member, and validates only the member it names, so selection is predictable, faster, and errors describe just that member.

open as a page

Why can one strict Pydantic 2 model accept a payload through model_validate_json yet reject the same data passed to model_validate as a dict?

level: seniorimportance: should knowfreq 30%

basics

~10 s

Pydantic 2 validates JSON and Python input under different rules: in JSON mode, strict still accepts strings for dates, UUIDs and Decimals, because JSON has no such types, while Python mode demands real instances.

open as a page

In Pydantic 2, how does the default smart union mode choose a member, and when would you set union_mode='left_to_right' instead?

level: seniorimportance: should knowfreq 30%

basics

~20 s

Smart mode, the Pydantic 2 default, picks the best match: for plain types an exact match first, then the leftmost strict, then the leftmost lax match. left_to_right takes the first member that validates, so order decides.

open as a page

After a Pydantic 1 codebase moves to Pydantic 2 and every renamed API is fixed, which validation behaviours still change with no code change, and how do you catch them?

level: seniorimportance: should knowfreq 48%

basics

~20 s

Optional fields without a default become required, numbers no longer coerce to str, floats with a fraction no longer truncate to int, int | str keeps '1' a string, and pairs stop becoming dicts. Replaying real payloads through both versions catches them.

open as a page

In Pydantic 2, what does importing BaseModel from the pydantic.v1 namespace buy a team mid-migration, and where does that shim stop helping?

level: seniorimportance: should knowfreq 35%

basics

~20 s

pydantic.v1 is a copy of the version 1 library bundled inside Pydantic 2, so unmigrated models keep version 1 behaviour on the new install. It stops at the boundary: version 1 and version 2 models must not be nested or mixed.

open as a page

A Pydantic 2 booking model's check_out validator reads info.data['check_in'] and crashes with KeyError when check_in is invalid. Why, and how do you fix it?

level: seniorimportance: should knowfreq 32%

basics

~20 s

In Pydantic 2, a field that fails validation is left out of ValidationInfo.data, and a KeyError is not converted into a validation error, so it escapes. Read with info.data.get('check_in') and skip the rule when it is missing.

open as a page

showing 1–30 of 35