question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE REQUEST] KeyError using Pydantic dataclass as request body parameter

See original GitHub issue

Describe 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:

  1. 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
  1. Start the app server
  2. Open the OpenAPI docs in a browser (e.g. http://127.0.0.1:8000/docs)
  3. 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:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
MatanRubincommented, Jan 31, 2020

+1 for supporting pydantic dataclasses as request bodies!

4reactions
tiangolocommented, Jul 21, 2021

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/ 🎉

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found