[FEATURE REQUEST] KeyError using Pydantic dataclass as request body parameter
See original GitHub issueDescribe the bug If a user attempts to use the dataclass from Pydantic to decorate the class used in a request body, a KeyError is thrown when loading the OpenAPI docs.
To Reproduce Steps to reproduce the behavior:
- Create a file with this example:
from fastapi import FastAPI
from pydantic.dataclasses import dataclass
@dataclass()
class Item:
name: str
price: float
description: str = None
tax: float = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
- Start the app server
- Open the OpenAPI docs in a browser (e.g. http://127.0.0.1:8000/docs)
- See error in terminal
KeyError: <class 'src.main.Item'>
Expected behavior
I expect this to work the same as if I had declared my Item
class with Pydantic’s BaseModel.
Here’s an example that works as expected:
from fastapi import FastAPI
from pydantic import BaseModel
class Item(BaseModel):
name: str
price: float
description: str = None
tax: float = None
app = FastAPI()
@app.post("/items/")
async def create_item(item: Item):
return item
Environment:
- OS: macOS
- FastAPI Version:
0.42.0
- Python version, get it with:
3.7.3
Additional context
I stumbled across this while trying to adapt this example from the docs to use Pydantic.dataclasses
instead of Pydantic.BaseModel
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:6
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Using Dataclasses - FastAPI
FastAPI is built on top of Pydantic, and I have been showing you how to use Pydantic models to declare requests and responses....
Read more >Dataclasses - pydantic
You can use all the standard pydantic field types, and the resulting dataclass will be identical to the one created by the standard...
Read more >FastAPI keyerror on class - python - Stack Overflow
I am trying to implement a put api using fastAPI. ... from sa2schema.to.pydantic import sa_model ReleaseGetSchema = sa_model(model.
Read more >pydantic [python-library] - Occam :: Details
Install using pip install -U pydantic or conda install pydantic -c conda-forge . ... Call validator with the correct values parameter type in...
Read more >Idempotency - AWS Lambda Powertools for Python
Each function invocation will generally make 2 requests to DynamoDB. ... When using idempotent_function , you must tell us which keyword parameter in...
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
+1 for supporting pydantic dataclasses as request bodies!
Thanks for the report and for the discussion everyone!
This is now supported in FastAPI
0.67.0
, the new docs are here: https://fastapi.tiangolo.com/advanced/dataclasses/ 🎉