Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining recursive models in Pydantic?

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] = []

but when I run that I get the following error:

NameError                                 Traceback (most recent call last)
<ipython-input-1-c6dca1d390fe> in <module>
      2 from pydantic import BaseModel
      3
----> 4 class Task(BaseModel):
      5     name: str
      6     subtasks: List[Task] = []

<ipython-input-1-c6dca1d390fe> in Task()
      4 class Task(BaseModel):
      5     name: str
----> 6     subtasks: List[Task] = []
      7

NameError: name 'Task' is not defined

I looked through the documentation but couldn't find anything. For example, at the page on "Recursive Models", but it seems to be about nesting subtypes of BaseModel not about a recursive type definition.

Thanks for your help!

like image 209
A Poor Avatar asked Nov 17 '25 08:11

A Poor


2 Answers

Either put from __future__ import annotations at the top of the file, or change your annotation to List['Task'].

The relevant pydantic documentation is here: https://pydantic-docs.helpmanual.io/usage/postponed_annotations/

But the error in your question is not pydantic specific, that's just how type annotations work.

like image 96
Alex Hall Avatar answered Nov 18 '25 21:11

Alex Hall


Expanding on the accepted answer from Alex Hall:

From the Pydantic docs, it appears the call to update_forward_refs() is still required whether or not annotations is imported.

from typing import List
from pydantic import BaseModel

class Task(BaseModel):
    name: str
    subtasks: List['Task'] = []


Task.update_forward_refs()

or

# python3.7+
from __future__ import annotations
from typing import List
from pydantic import BaseModel

class Task(BaseModel):
    name: str
    subtasks: List[Task] = []


Task.update_forward_refs()

https://pydantic-docs.helpmanual.io/usage/postponed_annotations/#self-referencing-models

like image 45
ryantuck Avatar answered Nov 18 '25 22:11

ryantuck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!