Converting to a dictionary solves this:

>>> data = Data(info=Info(data='test'))
>>> dict(data)
{'info': Info(data='test')}
Answer from Yaakov Bressler on Stack Overflow
🌐
Pydantic
docs.pydantic.dev › latest › concepts › models
Models - Pydantic Validation
See Validating data. model_construct(): Creates models without running validation. See Creating models without validation. model_dump(): Returns a dictionary of the model's fields and values.
🌐
GitHub
github.com › pydantic › pydantic › issues › 8213
`model_dump` doesn't work recursively with subclasses as expected · Issue #8213 · pydantic/pydantic
November 23, 2023 - class Holder(BaseModel): type: Literal["sub1", "sub2"] common: Common def model_dump(self): data = super().model_dump() data["common"] = self.common.model_dump() return data · from abc import ABC from typing import Literal from pydantic import BaseModel, model_validator class Common(BaseModel, ABC): shared_value: int class SubCommon1(Common): sub1_value: str class SubCommon2(Common): sub2_value: str class Holder(BaseModel): type: Literal["sub1", "sub2"] common: Common h = Holder.model_validate( { "common": SubCommon1.model_validate({"sub1_value": "A", "shared_value": 1}), "type": "sub1", } ) h.model_dump() # {'type': 'sub1', 'common': {'shared_value': 1}} h.common.model_dump() # {'shared_value': 1, 'sub1_value': 'A'}
Author   ThomasMarwitzQC
Discussions

python - How to Control Recursion Depth in Pydantic’s model_dump Serialization? - Stack Overflow
I have the following classes: class Info: data: str class Data: info: Info When I call model_dump in Data class, pydantic will serialize the classe recursively as described here This is the More on stackoverflow.com
🌐 stackoverflow.com
A better way to `construct` models recursively
This is not recommended however, ... about Pydantic's stance on this. model_construct as it's currently implemented only constructs the root-most model, aligning with the above point. However, this still introduces a common issue where someone might expect (rightfully so, IMO) to be able to model_dump a deeply nested ... More on github.com
🌐 github.com
10
November 10, 2023
python - Defining recursive models in Pydantic? - Stack Overflow
How can I define a recursive Pydantic model? Here's an example of what I mean: from typing import List from pydantic import BaseModel class Task(BaseModel): name: str subtasks: List[Task] ... More on stackoverflow.com
🌐 stackoverflow.com
Recursively exclude all 'None' fields in a deeply nested Pydantic model?
No, mymodel.model_dump(exclude_none=True) works recursively. It is NOT a per-class option, much to my annoyance (but the new field-level exclude_if can be used for this). Docs: exclude_none: https://docs.pydantic.dev/latest/api/base_model/#pydantic.BaseModel.model_dump available per-class settings, note the absence of exclude_none: https://docs.pydantic.dev/latest/api/config/ Personally, I think exclude_none is a bit of an antipattern, because the shape of the resulting data will violate the promised JSON schema (if that's relevant for you). Instead, use the exclude-defaults feature. More on reddit.com
🌐 r/learnpython
1
0
October 24, 2025
🌐
Pydantic
docs.pydantic.dev › latest › concepts › serialization
Serialization - Pydantic Validation
When using the Python mode, Pydantic models (and model-like types such as dataclasses) (1) will be (recursively) converted to dictionaries. This is achievable by using the model_dump() method:
🌐
Pydantic
docs.pydantic.dev › 1.10 › usage › exporting_models
Exporting models - Pydantic
Pydantic's commercial roadmap is ... and exported in a number of ways: This is the primary way of converting a model to a dictionary. Sub-models will be recursively ......
🌐
GitHub
github.com › pydantic › pydantic › issues › 8084
A better way to `construct` models recursively · Issue #8084 · pydantic/pydantic
November 10, 2023 - This is not recommended however, ... about Pydantic's stance on this. model_construct as it's currently implemented only constructs the root-most model, aligning with the above point. However, this still introduces a common issue where someone might expect (rightfully so, IMO) to be able to model_dump a deeply nested ...
Author   redruin1
🌐
Kev's Robots
kevsrobots.com › learn › pydantic › 06_recursive_models.html
Managing Recursive Models - Pydantic
Explore how to effectively define and work with recursive models in Pydantic, enabling sophisticated data structures like trees and nested objects.
Find elsewhere
🌐
Pydantic
pydantic.com.cn › en › concepts › serialization
Serialization - Pydantic documentation (en)
This is also done recursively. ... You can use computed fields to include property and cached_property data in the model.model_dump(...) output. ... from typing import Any, List, Optional from pydantic import BaseModel, Field, Json class BarModel(BaseModel): whatever: int class FooBarModel(BaseModel): banana: Optional[float] = 1.1 foo: str = Field(serialization_alias='foo_alias') bar: BarModel m = FooBarModel(banana=3.14, foo='hello', bar={'whatever': 123}) # returns a dictionary: print(m.model_dump()) #> {'banana': 3.14, 'foo': 'hello', 'bar': {'whatever': 123}} print(m.model_dump(include={'f
🌐
Pydantic
docs.pydantic.dev › 2.1 › usage › models
Models - Pydantic
More details on pydantic's coercion logic can be found in Data Conversion. Fields of a model can be accessed as normal attributes of the user object. The string '123' has been converted into an int as per the field type. ... The fields which were supplied when user was initialized. assert user.model_dump() == {'id': 123, 'name': 'Jane Doe'}
🌐
GitHub
github.com › pydantic › pydantic › discussions › 8257
How do I do a "shallow" or non-recursive dump of a model instance in v2? · pydantic/pydantic · Discussion #8257
November 29, 2023 - class InnerModel(MyBaseModel): foo: int = pydantic.Field(1) class OuterModel(MyBaseModel): inner: InnerModel = pydantic.Field(...) bar: str = pydantic.Field(...) model = OuterModel(inner=InnerModel(), bar='bacon') print(model.shallow_dict()) # {'inner': InnerModel(foo=1), 'bar': 'bacon'} So, to migrate my project to v2, I'd like to make a shallow_dump method. In v2 I'm glad to see the options for ._iter (the underlying method for .dict) are exposed publicly in model_dump. However, it's not clear to me how I can prevent model_dump in v2 from being recursive.
Author   pydantic
🌐
Orchestra
getorchestra.io › guides › pydantic-recursive-models-in-fastapi-a-detailed-tutorial
Pydantic Recursive Models in FastAPI: A Detailed Tutorial | Orchestra
April 2, 2024 - Pydantic is a data validation and settings management library in Python, often used in FastAPI to define data structures and perform data validation. Recursive models are a unique feature in Pydantic, where a model can contain instances of itself. ...
🌐
Pydantic
docs.pydantic.dev › 2.7 › concepts › serialization
Serialization - Pydantic
This is also done recursively. ... You can use computed fields to include property and cached_property data in the model.model_dump(...) output. ... from typing import Any, List, Optional from pydantic import BaseModel, Field, Json class BarModel(BaseModel): whatever: int class FooBarModel(BaseModel): banana: Optional[float] = 1.1 foo: str = Field(serialization_alias='foo_alias') bar: BarModel m = FooBarModel(banana=3.14, foo='hello', bar={'whatever': 123}) # returns a dictionary: print(m.model_dump()) #> {'banana': 3.14, 'foo': 'hello', 'bar': {'whatever': 123}} print(m.model_dump(include={'f
🌐
Pydantic
pydantic.com.cn › en › concepts › models
Models - Pydantic documentation (en)
More details on pydantic's coercion logic can be found in Data Conversion. Fields of a model can be accessed as normal attributes of the user object. The string '123' has been converted into an int as per the field type. ... The fields which were supplied when user was initialized. assert user.model_dump() == {'id': 123, 'name': 'Jane Doe'}
🌐
Tortoise
tortoise.github.io › examples › pydantic.html
Pydantic Examples - Tortoise ORM v1.1.5 Documentation
# ──────────..._pydantic.model_dump_json(indent=4)) # employee_count returns 0 gracefully instead of crashing print(f"\nemployee_count (graceful, no prefetch): {dept_pydantic.employee_count}") print( "employee_names would raise NoValuesFetched in the same scenario, " "but employee_count handles it and returns 0." ) if __name__ == "__main__": run_async(run()) """ This example demonstrates pydantic serialisation of a recursively cycled ...
🌐
Pydantic
docs.pydantic.dev › latest › api › root_model
RootModel - Pydantic Validation
Generally, this method will have a return type of RootModelRootType, assuming that RootModelRootType is not a BaseModel subclass. If RootModelRootType is a BaseModel subclass, then the return type will likely be dict[str, Any], as model_dump calls are recursive.
🌐
Tortoise
tortoise.github.io › contrib › pydantic.html
Pydantic serialisation - Tortoise ORM v0.25.3 Documentation
This is only useful for recursive/self-referential models. A value of False (the default) will prevent any and all backtracking. ... Sort the parameters alphabetically instead of Field-definition order. ... Returns a serializable pydantic model instance that contains a list of models, from the provided queryset.
🌐
Netlify
field-idempotency--pydantic-docs.netlify.app › usage › exporting_models
Exporting models - pydantic
This is the primary way of converting a model to a dictionary. Sub-models will be recursively converted to dictionaries.