Skip to content
tezvyn:

Pydantic: Required vs. Optional Fields

Source: pydantic.devEasyHow cards are made

Pydantic: Required vs. Optional Fields

In Pydantic, a field is required by default. To make it optional, you must provide a default value, like name: str = "guest" or age: int | None = None. This is key for flexible API request bodies.

Why it exists

To provide a clear, type-safe way to distinguish between data that a client must provide versus data that is optional or has a sensible fallback. This prevents runtime errors from missing keys and simplifies API contracts by making intent explicit.

The mental model

Think of a Pydantic model as a contract. By default, every field you declare is a mandatory clause. To make a clause optional, you must explicitly state what happens if it's not provided by giving it a default value. If a field has a default, Pydantic won't raise an error if it's missing from the input data.

How it works

Pydantic inspects the type annotations and assignments in your model definition. A field without a default value (e.g., username: str) is required. A field with a direct default value (e.g., items_in_cart: int = 0) is optional and will use 0 if not provided. A field typed with a union including None and given a default of None (e.g., middle_name: str | None = None) is also optional. To set a default while adding other metadata, use the Field() function's default argument: score: int = Field(default=0).

When to use it

Use default values for fields that have a logical fallback, like a status flag (is_enabled: bool = True). Use Optional[T] = None (or T | None = None) for fields that are genuinely optional and have no other sensible default, like a user's bio. This is fundamental to building almost any Pydantic model for an API.

When not to use it

Avoid setting defaults for fields that are essential for the object's identity or core function, such as a user's email or a product's id. Forcing the client to provide this data makes the API contract more robust and explicit. The footgun is assuming my_field: str = Field(...) sets a default; it does not, it only attaches metadata to a required field.

One canonical example

In a UserProfile model, username: str is required and will raise an error if missing. is_premium_member: bool = False is optional and defaults to False if not provided. bio: str | None = None is also optional, defaulting to None. Finally, follower_count: int = Field(default=0) shows how to set a default while also providing other configurations via Field().

Interview question

To define a Pydantic model field named "notes" that can be entirely absent from input data without causing a validation error, which definition should be used?

  • a.notes: str
  • b.notes: str | None = NoneCorrect
  • c.notes: str | None
  • d.notes: str = Field(max_length=200)
Why?

The definition "notes: str | None = None" correctly makes the field optional because it provides a default value of None. If the field is missing from the input, Pydantic will use None without raising an error. Option C, "notes: str | None", only indicates that None is an acceptable value if the field is provided, but without a default, the field is still considered required if entirely absent from the input data.

Just read this? Test yourself on what you have been reading.

Read the original → pydantic.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles