[FEATURE] Add support for Pydantic's dataclasses
See original GitHub issueDescribe the question I am using a pydantic.dataclasses.dataclass as a response_model of my fast api route.
from fastapi import FastAPI
from pydantic.dataclasses import dataclass
@dataclass
class Foo:
bar: int
app = FastAPI()
@app.get('/foo', response_model=Foo)
def get_bar() -> Foo:
return Foo(bar=1)
To Reproduce Steps to reproduce the behavior:
- Create a file app.py with the above code.
- Start the app:
uvicorn app:app
- Open the browser and go to localhost:8000/foo
- See error:
pydantic.error_wrappers.ValidationError: 1 validation error
response
__init__() got an unexpected keyword argument '__initialised__' (type=type_error)
Expected behavior
No validation error and serialized json does not contain any keys __initialised__
.
Environment:
- OS: Linux
- FastAPI Version 0.25.0
- Python version 3.7.3
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:20 (8 by maintainers)
Top Results From Across the Web
Dataclasses - pydantic
After v1.10, pydantic dataclasses support Config.extra but some default behaviour of stdlib dataclasses may prevail. For example, when print ing a pydantic ......
Read more >Pydantic or dataclasses? Why not both? Convert Between Them
Pydantic's arena is data parsing and sanitization, while dataclasses a is a fast and memory-efficient (especially using slots, Python 3.10+) ...
Read more >Do we still need dataclasses? // PYDANTIC tutorial - YouTube
Here's my FREE 7-step guide to help you consistently design great software: https://arjancodes.com/designguide. Pydantic is a very useful ...
Read more >Attrs, Dataclasses and Pydantic - Stefan Scherfke
Attrs supports both ways. The default is to allow positional and keyword arguments like data classes. You can enable kw-only arguments by ...
Read more >Hidden powers of pydantic - Kevin Tewouda
For those who don't know pydantic, it is a library for data validation leveraging type annotations feature added in python3.5. Here is an...
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
@justinttl It looks like pydantic dataclasses work as long as you provide a config with
from_orm=True
:@tiangolo I feel like there is room for improvement around the handling and/or error messages here. Maybe we should require
orm_mode
for any model used by FastAPI? Or turn it on automatically?@samuelcolvin Any ideas about how we might more clearly indicate to users that the problem is coming from
orm_mode=False
?This does not work with nested dataclassed. The inner class record will have
...,__intialized__ : true,...