You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting the serialize_as_any parameter for the BaseModel.model_dump[_xxx]() methods to True causes an error to be thrown on all models that have a recursive attribute /type/, even if they do not have a recursive /value/.
The SerializeAsAny annotation continues to work fine, but the dump call still fails if the keyword is specified.
This kw param was introduced in 2.7, so it didn't exist in 2.6. I have tested with 2.7.0 - 2.7.4, all have the same problem.
In the example below, all four fail with a ValueError: Circular reference detected (id repeated), e.g.:
Traceback (most recent call last):
File "/home/mjog/project/test.py", line 24, in <module>
print(Model1(recursive = None).model_dump(serialize_as_any = True))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/mjog/.cache/pypoetry/virtualenvs/project-HUCGNwqs-py3.12/lib/python3.12/site-packages/pydantic/main.py", line 328, in model_dump
return self.__pydantic_serializer__.to_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Circular reference detected (id repeated)
Example Code
from __future__ importannotationsimporttracebackimportpydanticclassModel1(pydantic.BaseModel):
recursive: Model1|NoneclassModel2(pydantic.BaseModel):
recursive: pydantic.SerializeAsAny[Model2] |None# All work fine:print(Model1(recursive=None).model_dump())
print(Model1(recursive=None).model_dump_json())
print(Model2(recursive=None).model_dump())
print(Model2(recursive=None).model_dump_json())
# All fail:try:
print(Model1(recursive=None).model_dump(serialize_as_any=True))
except:
traceback.print_exc()
try:
print(Model1(recursive=None).model_dump_json(serialize_as_any=True))
except:
traceback.print_exc()
try:
print(Model2(recursive=None).model_dump(serialize_as_any=True))
except:
traceback.print_exc()
try:
print(Model2(recursive=None).model_dump_json(serialize_as_any=True))
except:
traceback.print_exc()
mjog
changed the title
BaseModel.model_dump[_xxx](serialize_as_any = True) breaks models with recursive attribute types (not values)
BaseModel.model_dump[_xxx](serialize_as_any = True) breaks with models with recursive attribute types (not values)
Jun 16, 2024
Initial Checks
Description
Setting the
serialize_as_any
parameter for theBaseModel.model_dump[_xxx]()
methods toTrue
causes an error to be thrown on all models that have a recursive attribute /type/, even if they do not have a recursive /value/.The
SerializeAsAny
annotation continues to work fine, but the dump call still fails if the keyword is specified.This kw param was introduced in 2.7, so it didn't exist in 2.6. I have tested with 2.7.0 - 2.7.4, all have the same problem.
In the example below, all four fail with a
ValueError: Circular reference detected (id repeated)
, e.g.:Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: