Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump Generate a dictionary representation of the model, optionally specifying which fields to include or exclude. Args: mode: The mode in which `to_python` should run.
(
self,
*,
mode: Literal["json", "python"] | str = "python",
include: IncEx | None = None,
exclude: IncEx | None = None,
by_alias: bool = False,
exclude_unset: bool = False,
exclude_defaults: bool = False,
exclude_none: bool = False,
round_trip: bool = False,
warnings: bool | Literal["none", "warn", "error"] = True,
context: dict[str, Any] | None = None,
serialize_as_any: bool = False,
)
| 251 | |
| 252 | @override |
| 253 | def model_dump( |
| 254 | self, |
| 255 | *, |
| 256 | mode: Literal["json", "python"] | str = "python", |
| 257 | include: IncEx | None = None, |
| 258 | exclude: IncEx | None = None, |
| 259 | by_alias: bool = False, |
| 260 | exclude_unset: bool = False, |
| 261 | exclude_defaults: bool = False, |
| 262 | exclude_none: bool = False, |
| 263 | round_trip: bool = False, |
| 264 | warnings: bool | Literal["none", "warn", "error"] = True, |
| 265 | context: dict[str, Any] | None = None, |
| 266 | serialize_as_any: bool = False, |
| 267 | ) -> dict[str, Any]: |
| 268 | """Usage docs: https://docs.pydantic.dev/2.4/concepts/serialization/#modelmodel_dump |
| 269 | |
| 270 | Generate a dictionary representation of the model, optionally specifying which fields to include or exclude. |
| 271 | |
| 272 | Args: |
| 273 | mode: The mode in which `to_python` should run. |
| 274 | If mode is 'json', the dictionary will only contain JSON serializable types. |
| 275 | If mode is 'python', the dictionary may contain any Python objects. |
| 276 | include: A list of fields to include in the output. |
| 277 | exclude: A list of fields to exclude from the output. |
| 278 | by_alias: Whether to use the field's alias in the dictionary key if defined. |
| 279 | exclude_unset: Whether to exclude fields that are unset or None from the output. |
| 280 | exclude_defaults: Whether to exclude fields that are set to their default value from the output. |
| 281 | exclude_none: Whether to exclude fields that have a value of `None` from the output. |
| 282 | round_trip: Whether to enable serialization and deserialization round-trip support. |
| 283 | warnings: Whether to log warnings when invalid fields are encountered. |
| 284 | |
| 285 | Returns: |
| 286 | A dictionary representation of the model. |
| 287 | """ |
| 288 | if mode not in {"json", "python"}: |
| 289 | raise ValueError("mode must be either 'json' or 'python'") |
| 290 | if round_trip != False: |
| 291 | raise ValueError("round_trip is only supported in Pydantic v2") |
| 292 | if warnings != True: |
| 293 | raise ValueError("warnings is only supported in Pydantic v2") |
| 294 | if context is not None: |
| 295 | raise ValueError("context is only supported in Pydantic v2") |
| 296 | if serialize_as_any != False: |
| 297 | raise ValueError("serialize_as_any is only supported in Pydantic v2") |
| 298 | dumped = super().dict( # pyright: ignore[reportDeprecated] |
| 299 | include=include, |
| 300 | exclude=exclude, |
| 301 | by_alias=by_alias, |
| 302 | exclude_unset=exclude_unset, |
| 303 | exclude_defaults=exclude_defaults, |
| 304 | exclude_none=exclude_none, |
| 305 | ) |
| 306 | |
| 307 | return cast("dict[str, Any]", json_safe(dumped)) if mode == "json" else dumped |
| 308 | |
| 309 | @override |
| 310 | def model_dump_json( |