Incorrect response with nested Pydantic models
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI, UploadFile
from datetime import datetime
from typing import List
from pydantic import BaseModel, Field
app = FastAPI()
class BoundingBox(BaseModel):
x1: int
y1: int
x2: int
y2: int
class ImageMeta(BaseModel):
timestamp: datetime
bounding_boxes: List[BoundingBox]
class Config:
orm_mode = True
@app.post('/fetch_image')
def fetch_image(content: UploadFile, metadata: ImageMeta):
return {
'filename': content.filename,
'timestamp': metadata.timestamp.isoformat(timespec='seconds'),
'bounding_boxes': metadata.bounding_boxes
}
Description
The Pydantic models are stated in example code, and this is the request body as shown below:
But receive the following response, stated that the field timestamp
is missing. For elimating this issue, I’ve done with following attempts:
- Reduce the field
timestamp
and only fieldbounding_boxes
: got the same response stated that the fieldbounding_boxes
is missing. - Cancelled out
orm_mode
: No changes. - Assign the default value for
timestamp
andbounding_boxes
: the value of fields will always be default, never be modified by the incoming request.
I’m struggling with this situation for few hours and still seeking for the issue. Does anyone confront the same issue?
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.75.0
Python Version
3.10.1
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:7
Top Results From Across the Web
pydantic nested model response - python - Stack Overflow
I want to pass nested models into the response. I am trying by making object of 1 model then storing the related values...
Read more >Body - Nested Models - FastAPI
Body - Nested Models¶. With FastAPI, you can define, validate, document, and use arbitrarily deeply nested models (thanks to Pydantic).
Read more >pydantic/Lobby - Gitter
I haven't found solution how to map deep nested fields to pydantic field and try to use validator. So when i try to...
Read more >Dataclasses - pydantic
Nested dataclasses are supported both in dataclasses and normal models. Python 3.7 and above. from pydantic import AnyUrl ...
Read more >How to Make the Most of Pydantic - Towards Data Science
The structure defines a cat entry with a nested definition of an address. So then, defining a Pydantic model to tackle this could...
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
@Pandede, Above solution 😃
https://github.com/dotX12/pyfa-converter