Skip to content
tezvyn:

What is the difference between a Pydantic default and Optional field?

Source: pydantic.devEasyHow cards are made

What is the difference between a Pydantic default and Optional field?
Summary

Separation of type constraints from requiredness.

Key points

Both forms are non-required; str = 'guest' rejects None, Optional[str] = None accepts it.

What's really being asked

This question tests whether you confuse type system nullability with Pydantic field requiredness. In Pydantic, a field is required only when it has no default value. The type annotation controls what values are valid, while the presence of a default controls what happens when the field is missing from input data.

The full answer

First, state that both name: str = 'guest' and name: Optional[str] = None make the field non-required because both supply a default value. Second, explain that the plain string version restricts valid input to strings only; if the caller passes None, Pydantic raises a validation error. Third, explain that the Optional version widens the accepted types to str or None; passing None is valid, and omitting the field causes the attribute to be set to None. Fourth, note that on the resulting model instance both attributes are always present, but their types and values differ, which affects downstream serialization and type checking.

The mistakes people make

A major red flag is claiming that Optional[str] by itself makes a field non-required. In Pydantic v2, name: Optional[str] without a default is still required; it simply allows None as an explicit input. Another red flag is saying that name: str = 'guest' will accept None and fall back to the default. Pydantic does not coerce None to the default; it validates None against the str type and fails. Candidates also sometimes claim that omitted optional fields are absent from the model instance, but Pydantic sets them to their default.

What usually comes next

The interviewer may ask what happens if you write name: Optional[str] with no default, or how to make a field truly required that accepts None. They might also ask about Field(default_factory=list) versus default=[] to probe understanding of mutable defaults, or how exclude_none=True affects model_dump() output.

A concrete example

Consider class User(BaseModel): role: str = 'guest'; status: Optional[str] = None. Instantiating User() yields role='guest' and status=None. Instantiating User(role=None) raises a validation error because None is not a valid string. Instantiating User(status='active') sets status='active'. Calling model_dump() on the default instance produces {'role': 'guest', 'status': None}.

Interview question

What is the key difference between a Pydantic field defined as name: str = 'guest' and one defined as name: Optional[str] = None?

  • a.Omitting the second field leaves it absent from the model instance.
  • b.The first field is required, while the second is optional.
  • c.Passing None to the first field causes it to fall back to 'guest'.
  • d.The first rejects None while the second accepts it, but both may be omitted from input.Correct
Why?

Both fields have defaults so neither is required, yet str = 'guest' rejects None while Optional[str] = None accepts it. Distractor A is tempting because Optional sounds optional, but requiredness is determined solely by the presence or absence of a default.

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