Skip to main content

Field

Usage of Field to create a FieldInfo object, providing additional information about a field for validation and serialization.

def Field(
default_factory: Callable[[], Any] | Callable[[dict[str, Any]], Any],
alias: str | None,
alias_priority: int | None,
validation_alias: str | AliasPath | AliasChoices | None,
serialization_alias: str | None,
title: str | None,
field_title_generator: Callable[[str, FieldInfo], str] | None,
description: str | None,
examples: list[Any] | None,
exclude: bool | None,
exclude_if: Callable[[Any], bool] | None,
discriminator: str | types.Discriminator | None,
deprecated: Deprecated | str | bool | None,
json_schema_extra: JsonDict | Callable[[JsonDict], None] | None,
frozen: bool | None,
validate_default: Literal[True],
repr: bool,
init: bool | None,
init_var: bool | None,
kw_only: bool | None,
pattern: str | re.Pattern[str] | None,
strict: bool | None,
coerce_numbers_to_str: bool | None,
gt: annotated_types.SupportsGt | None,
ge: annotated_types.SupportsGe | None,
lt: annotated_types.SupportsLt | None,
le: annotated_types.SupportsLe | None,
multiple_of: float | None,
allow_inf_nan: bool | None,
max_digits: int | None,
decimal_places: int | None,
min_length: int | None,
max_length: int | None,
union_mode: Literal['smart', 'left_to_right'],
fail_fast: bool | None
) - > Any

Configures metadata and validation constraints for a Pydantic model field. Use this to define default values, aliases, schema information, and numeric or string constraints.

Parameters

NameTypeDescription
default_factory`Callable[[], Any]Callable[[dict[str, Any]], Any]`
alias`strNone`
alias_priority`intNone`
validation_alias`strAliasPath
serialization_alias`strNone`
title`strNone`
field_title_generator`Callable[[str, FieldInfo], str]None`
description`strNone`
examples`list[Any]None`
exclude`boolNone`
exclude_if`Callable[[Any], bool]None`
discriminator`strtypes.Discriminator
deprecated`Deprecatedstr
json_schema_extra`JsonDictCallable[[JsonDict], None]
frozen`boolNone`
validate_defaultLiteral[True]Forces Pydantic to run validation logic on the default value or default_factory result.
reprboolDetermines if the field should be included in the string representation of the model.
init`boolNone`
init_var`boolNone`
kw_only`boolNone`
pattern`strre.Pattern[str]
strict`boolNone`
coerce_numbers_to_str`boolNone`
gt`annotated_types.SupportsGtNone`
ge`annotated_types.SupportsGeNone`
lt`annotated_types.SupportsLtNone`
le`annotated_types.SupportsLeNone`
multiple_of`floatNone`
allow_inf_nan`boolNone`
max_digits`intNone`
decimal_places`intNone`
min_length`intNone`
max_length`intNone`
union_modeLiteral['smart', 'left_to_right']Controls how Pydantic attempts to match data against types in a Union.
fail_fast`boolNone`

Returns

TypeDescription
AnyA FieldInfo instance that Pydantic uses to define the field's behavior and schema.