Skip to content
You're viewing docs for Dev. See the latest version →

RootModel

RootModel class and type definitions.

RootModel

Bases: BaseModel, Generic[RootModelRootType]

A Pydantic BaseModel for the root object of the model.

Attributes

root

The root object of the model.

Type: RootModelRootType

__pydantic_root_model__

Whether the model is a RootModel.

__pydantic_private__

Private fields in the model.

__pydantic_extra__

Extra fields in the model.

Methods

model_construct

@classmethod

def model_construct(
    cls,
    root: RootModelRootType,
    _fields_set: set[str] | None = None,
) -> Self

Create a new model using the provided root object and update fields set.

Returns

Self — The new model.

Parameters

root : RootModelRootType

The root object of the model.

_fields_set : set[str] | None Default: None

The set of fields to be updated.

Raises
  • NotImplemented — If the model is not a subclass of RootModel.

model_dump

def model_dump(
    *,
    mode: Literal['json', 'python'] | str = 'python',
    include: IncEx | None = None,
    exclude: IncEx | None = None,
    context: Any | None = None,
    by_alias: bool | None = None,
    exclude_unset: bool = False,
    exclude_defaults: bool = False,
    exclude_none: bool = False,
    exclude_computed_fields: bool = False,
    round_trip: bool = False,
    warnings: bool | Literal['none', 'warn', 'error'] = True,
    fallback: Callable[[Any], Any] | None = None,
    serialize_as_any: bool = False,
    polymorphic_serialization: bool | None = None,
) -> Any

Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.

Returns

Any — A dictionary representation of the model.

Parameters

mode : Literal[‘json’, ‘python’] | str Default: 'python'

The mode in which to_python should run. If mode is ‘json’, the output will only contain JSON serializable types. If mode is ‘python’, the output may contain non-JSON-serializable Python objects.

include : IncEx | None Default: None

A set of fields to include in the output.

exclude : IncEx | None Default: None

A set of fields to exclude from the output.

context : Any | None Default: None

Additional context to pass to the serializer.

by_alias : bool | None Default: None

Whether to use the field’s alias in the dictionary key if defined.

exclude_unset : bool Default: False

Whether to exclude fields that have not been explicitly set.

exclude_defaults : bool Default: False

Whether to exclude fields that are set to their default value.

exclude_none : bool Default: False

Whether to exclude fields that have a value of None.

exclude_computed_fields : bool Default: False

Whether to exclude computed fields. While this can be useful for round-tripping, it is usually recommended to use the dedicated round_trip parameter instead.

round_trip : bool Default: False

If True, dumped values should be valid as input for non-idempotent types such as Json[T].

warnings : bool | Literal[‘none’, ‘warn’, ‘error’] Default: True

How to handle serialization errors. False/“none” ignores them, True/“warn” logs errors, “error” raises a PydanticSerializationError.

fallback : Callable[[Any], Any] | None Default: None

A function to call when an unknown value is encountered. If not provided, a PydanticSerializationError error is raised.

serialize_as_any : bool Default: False

Whether to serialize fields with duck-typing serialization behavior.

polymorphic_serialization : bool | None Default: None

Whether to use model and dataclass polymorphic serialization for this call.