Skip to main content

ModelField

This class is a TypedDict that defines the configuration and metadata for a specific field within a model. It specifies essential properties such as the core schema and field type, while providing optional configurations for validation aliases, serialization behavior, and immutability.

Attributes

AttributeTypeDescription
typeLiteral['model-field']A fixed literal string 'model-field' used to identify the schema type within the Pydantic core schema structure.
schemaCoreSchemaThe underlying Pydantic core schema that defines the validation and serialization logic for the field's value.
validation_alias`strlist[str
serialization_aliasstrThe key name used for this field when exporting the model to a dictionary or JSON string.
serialization_excludebool = FalseDetermines whether this field should be omitted entirely from the output during serialization.
serialization_exclude_ifCallable[[Any], bool] = NoneA callable that takes the field value and returns a boolean to determine if the field should be excluded from serialization dynamically.
frozenboolIndicates if the field is immutable, preventing its value from being changed after the model instance is initialized.
metadatadict[str, Any]A dictionary of arbitrary data associated with the field, often used for custom validation logic or third-party integrations.

Methods


type()

def type() - > string

Specifies the fixed identifier for this schema component, used to distinguish it as a model field within the Pydantic core schema.

Returns

TypeDescription
stringThe literal string 'model-field' identifying the schema type.

schema()

def schema() - > CoreSchema

Defines the inner Pydantic core schema used to validate and serialize the data within this specific field.

Returns

TypeDescription
CoreSchemaThe core schema definition for the field's value.

validation_alias()

def validation_alias() - > string | list[str | int]| list[list[str | int]]

Configures an alternative name or path used to locate the field value during the validation process.

Returns

TypeDescription
`stringlist[str

serialization_alias()

def serialization_alias() - > string

Sets the name used for the field when the model is serialized into a dictionary or JSON string.

Returns

TypeDescription
stringThe output key name for the field.

serialization_exclude()

def serialization_exclude() - > boolean

Determines whether this field should be omitted entirely from the serialized output of the model.

Returns

TypeDescription
booleanTrue if the field should be excluded, False otherwise.

serialization_exclude_if()

def serialization_exclude_if() - > Callable[[Any], bool]

Provides a predicate function to conditionally exclude the field from serialization based on its current value.

Returns

TypeDescription
Callable[[Any], bool]A function that returns True if the field should be excluded for the given value.

frozen()

def frozen() - > boolean

Indicates whether the field is immutable; if True, the field cannot be assigned a new value after the model is initialized.

Returns

TypeDescription
booleanTrue if the field is read-only, False otherwise.

metadata()

def metadata() - > dict[str, Any]

Stores a dictionary of arbitrary metadata associated with the field, often used by third-party tools or custom validation logic.

Returns

TypeDescription
dict[str, Any]A dictionary containing custom metadata key-value pairs.