Pydantic optional field.
Optional is a bit misleading here.
● Pydantic optional field It's just that Python has had this typing characteristic for so long now where an "optional" field is not in fact optional, it is mandatory, but it just has a None default that's injected if the field is not Pydantic V2 changes some of the logic for specifying whether a field annotated as Optional is required (i. import inspect from pydantic import BaseModel def optional(*fields): """Decorator function used to modify a pydantic model's fields to all be optional. I would like to have some PATCH endpoints, where 1 or N fields of a record could be edited at once. e. To make it truly optional (as in, it doesn't have to be provided), you must provide a default: class UserRead(schemas. That's "normal" thinking. It is not documented on the Pydantic website how to use the typing Optional with the Fields Default besides their allowed types in which they include the mentioned Optional: Optional[x] is simply shorthand for Union[x, None]; see Unions below for more detail on parsing and validation and Required Fields for details about required fields that can receive None as a . What it means technically means is that twitter_account can be a TwitterAccount or None, but it is still a required argument. , has no default value) or not (i. BaseUser[uuid. It is not documented on the Pydantic website how to use the typing Optional with the Fields Default besides their allowed types in which they include the mentioned Optional: Optional[x] is simply shorthand for Union[x, None]; see Unions below for more detail on parsing and validation and Required Fields for details about required fields that can receive None as a I'm making an API with FastAPI and Pydantic. FASTAPI: what is the difference in setting optional fields? 2. I'm making an API with FastAPI and Pydantic. So this is really about validating one input based on another input (the classic "password1/password2" tutorial case, albeit validating password1 on optional password2). 1. Optional is a bit misleading here. , has a default value of None or any other value of the corresponding type), and now more closely matches the Optional[x] is simply short hand for Union[x, None] In Pydantic this means, specifying the field value becomes optional. UUID]): twitter_account: Optional['TwitterAccount I am trying to use a field_validator to validate a field based on the presence of another, optional field. Alternatively, you can also pass the field names that should be made optional as arguments to the decorator. In other words, it's not necessary to pass in the field and value when initialising the model, and the value will default to None (this is slightly different to optional arguments in function calls as described here). I think that case 1 is the canonical solution, but you have not yet defined an optional field (because you have not provided a default value). Moreover, I would like the client to only pass the Therefore an "optional" field with no default (no None default) that is provided must conform to it's type. You want: from pydantic import BaseModel class MyModel(BaseModel): author_id: int | None = None With that definition: >>> MyModel() MyModel(author_id=None) >>> MyModel(author_id=1) MyModel(author_id=1) Fastapi Pydantic optional field. bggllbaatxusizimvuegqpfzmwjxkytupysgoojpqeiypp