Replies: 8 comments
-
Kudo's on the comprehensive report and research! (Y) I did some digging as well as to your last question (where is this is happening) and I am very confused. The response_fields are created upon app startup (when However, when the application is being called (for example, because you are going to /docs), it is running at some point I am wrecking my brain on how that is possible... |
Beta Was this translation helpful? Give feedback.
-
Thanks for the explanation. After filing this issue, I took a look at FastAPI's code once more, and even though I claimed FastAPI wasn't caching the converted dataclasses, it seems to be caching them. So, yeah, I'm confused as well. |
Beta Was this translation helpful? Give feedback.
-
Another person on SO has a simpler form to test this issue: import dataclasses
from fastapi import FastAPI
app = FastAPI()
@dataclasses.dataclass
class Response1:
yo: str
@app.post('/one', response_model=Response1)
def get_responses():
pass
@app.post('/two', response_model=Response1) # When I remove this "Response", or I create a second class it works.
def send_responses():
pass
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) Stacktrace: Traceback (most recent call last):
File "/<FOLDER>.venv/lib/python3.10/site-packages/uvicorn/protocols/http/httptools_impl.py", line 401, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/applications.py", line 269, in __call__
await super().__call__(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/applications.py", line 124, in __call__
await self.middleware_stack(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/exceptions.py", line 93, in __call__
raise exc
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/exceptions.py", line 82, in __call__
await self.app(scope, receive, sender)
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/routing.py", line 670, in __call__
await route.handle(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/routing.py", line 266, in handle
await self.app(scope, receive, send)
File "/<FOLDER>.venv/lib/python3.10/site-packages/starlette/routing.py", line 65, in app
response = await func(request)
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/applications.py", line 224, in openapi
return JSONResponse(self.openapi())
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/applications.py", line 199, in openapi
self.openapi_schema = get_openapi(
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/openapi/utils.py", line 414, in get_openapi
definitions = get_model_definitions(
File "/<FOLDER>.venv/lib/python3.10/site-packages/fastapi/utils.py", line 39, in get_model_definitions
model_name = model_name_map[model]
KeyError: <class 'pydantic.dataclasses.Response1'> |
Beta Was this translation helpful? Give feedback.
-
Any progress on this issue? I've noticed the behavior with |
Beta Was this translation helpful? Give feedback.
-
Same error here |
Beta Was this translation helpful? Give feedback.
-
Same error as well |
Beta Was this translation helpful? Give feedback.
-
I got the same error, and sidestepped the issue by switching from a
to
|
Beta Was this translation helpful? Give feedback.
-
The error you were encountering was due to a bug in FastAPI that was causing an error when the Swagger UI was trying to generate the OpenAPI definitions for the application. In your case, it was failing specifically on the OAuth2PasswordRequestForm class from FastAPI security utilities. I had the same problem, and it was solved by updating the FastAPI version. While it's unclear exactly which version resolved this issue, by the time I upgraded from 0.95.2 to version 0.98.0, the problem no longer persisted. |
Beta Was this translation helpful? Give feedback.
-
First Check
Commit to Help
Example Code
Description
http://localhost:8000/docs
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.78.0
Python Version
3.8.13
Additional Context
This bug happens because whatever is converting bare dataclasses to pydantic dataclasses isn't caching the output of the conversion, which means that, in dataclass
B
, the dataclassA
referenced in fielda
is different to the target of the mapping in fieldb
.In a naïve converter, this is what happens:
I don't think this is a bug in pydantic, though, as FastAPI is the library that claims dataclasses are supported.
I could try contributing a PR, if someone could point me to where the pydantic dataclass is generated.
As an additional note, if I don't fill the
responses
dict, this bug doesn't happen.Beta Was this translation helpful? Give feedback.
All reactions