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.

Incorrect response with nested Pydantic models

See original GitHub issue

First 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: image

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 field bounding_boxes: got the same response stated that the field bounding_boxes is missing.
  • Cancelled out orm_mode: No changes.
  • Assign the default value for timestamp and bounding_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? image

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:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
dotX12commented, Aug 23, 2022

@Pandede, Above solution 😃

1reaction
dotX12commented, Aug 23, 2022

https://github.com/dotX12/pyfa-converter

from datetime import datetime
from typing import Optional

from fastapi import FastAPI, UploadFile, File, Form
from pydantic import BaseModel, Field

from pyfa_converter import FormDepends, PyFaDepends

app = FastAPI()


class PostContractBodySchema(BaseModel):
    title: str = Field(..., description="Description title")
    date: Optional[datetime] = Field(
        None, description="Example: 2021-12-14T09:56:31.056Z"
    )


@app.post("/form-data-body")
async def example_foo_body_handler(
    data: PostContractBodySchema = FormDepends(PostContractBodySchema),
    # data1: PostContractBodySchema = PyFaDepends( # OR
    #         model= PostContractBodySchema, _type=Form
    #     ),
    document: UploadFile = File(...),
):
    return {"title": data.title, "date": data.date, "file_name": document.filename}

Read more comments on GitHub >

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

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