Constrain input values
Add validation rules such as minimum string length directly to a model field.
Pydantic · Function
Field configures an annotated Pydantic model field with validation constraints and metadata. It can also participate in aliases and generated schema.
Field returns a field configuration object that Pydantic uses with an annotated model attribute. It can supply constraints and metadata such as descriptions, aliases, and titles, which affect validation or schema generation.
Field(...)A field configuration object used in a Pydantic model declaration.
from pydantic import BaseModel, Field
class User(BaseModel):
name: str = Field(min_length=1, description="Display name")
user = User(name="Ada")
print(user.name)Defines a required string field with a minimum length and description.
Add validation rules such as minimum string length directly to a model field.
Attach descriptions or titles used by generated schemas and documentation.
Accept external names while retaining Python-friendly attribute names.
Field is used as a default-value declaration for a model attribute. It supplies validation constraints and metadata that Pydantic applies to that field.
Follow-up: How would you expose a different input name? Use an alias-related Field argument and configure whether validation accepts the alias, the Python attribute name, or both.
A Field declaration can include metadata such as a description, title, or alias. Descriptions can also be sourced from attribute docstrings when the relevant model configuration is enabled.
Follow-up: Which source wins when both are supplied? Inspect the generated field metadata and schema rather than assuming that a docstring replaces explicit Field metadata.
Field constraints are declared alongside the type annotation, so Pydantic can validate values and use the same information when producing a schema. For example, a string field can specify a minimum length.
Follow-up: What should be tested beyond successful construction? Test invalid boundary values and inspect the generated JSON schema when consumers depend on the constraints.
BaseModelConfigDictAliasGeneratormodel_fields