RootModel
RootModel class and type definitions.
RootModel ¶
RootModel(
root: RootModelRootType = PydanticUndefined, **data
)
Bases: BaseModel, Generic[RootModelRootType]
Usage Documentation
A Pydantic BaseModel for the root object of the model.
Attributes:
| Name | Type | Description |
|---|---|---|
root |
RootModelRootType
|
The root object of the model. |
__pydantic_root_model__ |
Whether the model is a RootModel. |
|
__pydantic_private__ |
Private fields in the model. |
|
__pydantic_extra__ |
Extra fields in the model. |
Source code in pydantic/root_model.py
62 63 64 65 66 67 68 69 70 | |
model_construct
classmethod
¶
Create a new model using the provided root object and update fields set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
RootModelRootType
|
The root object of the model. |
required |
_fields_set
|
set[str] | None
|
The set of fields to be updated. |
None
|
Returns:
| Type | Description |
|---|---|
Self
|
The new model. |
Raises:
| Type | Description |
|---|---|
NotImplemented
|
If the model is not a subclass of |
Source code in pydantic/root_model.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
model_dump ¶
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
Usage Documentation
Generate a dictionary representation of the model, optionally specifying which fields to include or exclude.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
Literal['json', 'python'] | str
|
The mode in which |
'python'
|
include
|
IncEx | None
|
A set of fields to include in the output. |
None
|
exclude
|
IncEx | None
|
A set of fields to exclude from the output. |
None
|
context
|
Any | None
|
Additional context to pass to the serializer. |
None
|
by_alias
|
bool | None
|
Whether to use the field's alias in the dictionary key if defined. |
None
|
exclude_unset
|
bool
|
Whether to exclude fields that have not been explicitly set. |
False
|
exclude_defaults
|
bool
|
Whether to exclude fields that are set to their default value. |
False
|
exclude_none
|
bool
|
Whether to exclude fields that have a value of |
False
|
exclude_computed_fields
|
bool
|
Whether to exclude computed fields.
While this can be useful for round-tripping, it is usually recommended to use the dedicated
|
False
|
round_trip
|
bool
|
If True, dumped values should be valid as input for non-idempotent types such as Json[T]. |
False
|
warnings
|
bool | Literal['none', 'warn', 'error']
|
How to handle serialization errors. False/"none" ignores them, True/"warn" logs errors,
"error" raises a |
True
|
fallback
|
Callable[[Any], Any] | None
|
A function to call when an unknown value is encountered. If not provided,
a |
None
|
serialize_as_any
|
bool
|
Whether to serialize fields with duck-typing serialization behavior. |
False
|
polymorphic_serialization
|
bool | None
|
Whether to use model and dataclass polymorphic serialization for this call. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
A dictionary representation of the model. |
Source code in pydantic/root_model.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |