Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Pydantic · Function

Field

Field configures an annotated Pydantic model field with validation constraints and metadata. It can also participate in aliases and generated schema.

How it works

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.

Syntax

Field(...)

Returns

A field configuration object used in a Pydantic model declaration.

Key points

  • Import Field from pydantic and assign it to an annotated model attribute to configure that field.
  • Model configuration can enable attribute docstrings as field descriptions.
  • Field metadata is available through the model's model_fields mapping.
  • An alias generator can derive aliases from field names when configured at the model level.

Common mistakes

  • An alias changes the field name accepted for population; configure validation behavior carefully when combining aliases and attribute names.
  • At least one of alias-based or name-based population must remain enabled, or the attribute cannot be populated.

Example: Constrained field

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.

When to use it

Constrain input values

Add validation rules such as minimum string length directly to a model field.

Document fields

Attach descriptions or titles used by generated schemas and documentation.

Handle aliases

Accept external names while retaining Python-friendly attribute names.

Interview questions

QWhat is Field used for?

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.

QWhat kinds of metadata can Field provide?

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.

QHow do Field constraints affect a model?

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.

  • BaseModel
  • ConfigDict
  • AliasGenerator
  • model_fields

Source: Configuration - Pydantic. Checked 2026-10-15.