[BUG] RecursionError from response model in 0.47.1
See original GitHub issueDescribe the bug
FastAPI 0.47.1 will not be able to start due to a RecursionError
when there is a circular reference among models. The issue seems to originate from https://github.com/tiangolo/fastapi/pull/889. This works fine in 0.46.0.
Environment
- OS: Windows
- FastAPI Version: 0.47.1
- Python version: 3.7.0
To Reproduce
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel, Field
class Group(BaseModel):
representative: Optional['Person'] = Field(None)
class Person(BaseModel):
group: Optional[Group] = Field(None)
Group.update_forward_refs()
app = FastAPI()
@app.get('/group/{group_id}', response_model=Group)
def get_group(group_id):
return []
Expected behavior
No exception
Actual output
Traceback (most recent call last):
File "test.py", line 21, in <module>
@app.get('/group/{group_id}', response_model=Group)
File "D:\virtualenvs\test\lib\site-packages\fastapi\routing.py", line 494, in decorator
callbacks=callbacks,
File "D:\virtualenvs\test\lib\site-packages\fastapi\routing.py", line 438, in add_api_route
callbacks=callbacks,
File "D:\virtualenvs\test\lib\site-packages\fastapi\routing.py", line 275, in __init__
] = create_cloned_field(self.response_field)
File "D:\virtualenvs\test\lib\site-packages\fastapi\utils.py", line 100, in create_cloned_field
use_type.__fields__[f.name] = create_cloned_field(f)
File "D:\virtualenvs\test\lib\site-packages\fastapi\utils.py", line 100, in create_cloned_field
use_type.__fields__[f.name] = create_cloned_field(f)
File "D:\virtualenvs\test\lib\site-packages\fastapi\utils.py", line 100, in create_cloned_field
use_type.__fields__[f.name] = create_cloned_field(f)
[Previous line repeated 981 more times]
File "D:\virtualenvs\test\lib\site-packages\fastapi\utils.py", line 97, in create_cloned_field
original_type.__name__, __config__=original_type.__config__
File "D:\virtualenvs\test\lib\site-packages\pydantic\main.py", line 773, in create_model
return type(model_name, (__base__,), namespace)
File "D:\virtualenvs\test\lib\site-packages\pydantic\main.py", line 152, in __new__
if issubclass(base, BaseModel) and base != BaseModel:
File "D:\virtualenvs\test\lib\abc.py", line 143, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
RecursionError: maximum recursion depth exceeded in comparison
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:17 (13 by maintainers)
Top Results From Across the Web
RecursionError: maximum recursion depth exceeded in ...
The recursion depth is around 1000, so the value is too large. – Willem Van Onsem · Just guessing: the comparison is n==0...
Read more >Odd recursion error : Forums - PythonAnywhere
Odd recursion error. I have 2 websites that use exactly the same git repo. One works fine and the other one works for...
Read more >[VTA] Recursion error - Troubleshooting - Apache TVM Discuss
I'm following this guide https://tvm.apache.org/docs/vta/install.html to install VTA, but I get I get a recursion error (RecursionError: maximum recursion ...
Read more >1670996 – nova api (in wsgi) returns RecursionError
Bug 1670996 - nova api (in wsgi) returns RecursionError: maximum recursion depth exceeded while calling a Python object [NEEDINFO].
Read more >Maximum recursion bug? - PyTorch Forums
nodesInLevels I ll get max recursion error: File "C:\Users\mk23\AppData\Local\Continuum\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 1042, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the discussion here everyone!
This was fixed by @voegtlel in https://github.com/tiangolo/fastapi/pull/1164 🚀 🎉
It will be available in the next release (today in a couple of hours).
I’ll re-open this issue to give @ysmu a chance to confirm it’s fixed and close it.
Ah thanks for fixing this, our front-end team really appreciates it 😉