This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use a pydantic dataclass
nested in a BaseModel
as a response_model
#4402
Comments
As far as I understand, FastAPI doesn't let us to mix import dataclasses
from pydantic.dataclasses import dataclass
@dataclass # or @dataclasses.dataclass
class Item:
name: str
@dataclass # or @dataclasses.dataclass
class Box:
item: Item
@app.get("/item", response_model=Item)
async def item() -> Item:
return Item(name="test")
@app.get("/box", response_model=Box)
async def box() -> Box:
return Box(item=await item()) |
This isn't what
ref: https://fastapi.tiangolo.com/advanced/dataclasses/?h=dataclasses#learn-more
Also creating an instance of >>> Box.validate(Box(item=Item(name="test")))
Box(item=Item(name="test"))) What makes you think mixing the two is not supported? |
dataclass
nested in a BaseModel
as a model_response
dataclass
nested in a BaseModel
as a response_model
Neither pydantic nor FastAPI documentation made me think that mixing different types of models is not supported. I have only tried to mix I have checked the documentation but I couldn't see anything that says you could mix a Lines 75 to 80 in 672c55a
Since If you call as I showed you above your response will be a Lines 101 to 102 in 672c55a
Eventually, this will also be a I might be wrong. I hope there will be someone else that can have a better explanation. |
Thanks for the pointers in the codebase! 👍 I am not sure this is the whole story, but it is definitely a step forward.
Well, there is the part that I posted in my first response that says:
With a link to that sentence. For the record, editing a post does not trigger any notification. |
Yes, I did. Like I explained above, those were my findings and I believe |
this is a bug pydantic/pydantic#3764 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
The
item
endpoint works fine, but thebox
one fails with aValidationError
:Operating System
Linux
Operating System Details
No response
FastAPI Version
0.71.0
Python Version
3.10.1
Additional Context
No response
The text was updated successfully, but these errors were encountered: