model with a field that is a root model - a tree #9322
marekro
started this conversation in
Pydantic V2
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to use Pydantic v2 to manage a tree structure. The basic idea is to have a "node" model, which is a container for the actual item object, and has a field which represents the children. I could use a
list
for the children, but I would like to use a Pydantic root model to be able to add some helper methods to this model, which list does not have.See the code below for my draft; the issue I am having is with the constructor, which throws an unexpectedly large number of validation errors on the children field. I tried to debug that, but this seems to be part of the core, which is compiled. I am running this with Python 3.10.14, pydantic 2.7.1.
Is there anything wrong with my code, or is this a bug in Pydantic v2? Any guidance is appreciated!
Here is the exception:
Beta Was this translation helpful? Give feedback.
All reactions