filmov
tv
FastAPI error when handling file together with form-data defined in a Pydantic model

Показать описание
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!FastAPI error when handling file together with form-data defined in a Pydantic model
For some reason handling Form Data and File Upload at the same time raises an error.
Form
File
from typing import Annotated
from typing import Annotated
from pydantic import BaseModel, StringConstraints, EmailStr
from pydantic import BaseModel, StringConstraints, EmailStr
class RouteBody(BaseModel):
email: Annotated[EmailStr, StringConstraints(
max_length = 255
)]
password: Annotated[str, StringConstraints(
max_length = 60
)]
class RouteBody(BaseModel):
email: Annotated[EmailStr, StringConstraints(
max_length = 255
)]
password: Annotated[str, StringConstraints(
max_length = 60
)]
And this enforces that the routes body is correct. Super nice.
from fastapi import UploadFile, File
from fastapi import UploadFile, File
async def handleRoute(routeBody: RouteBody = Form(), profilePicture: UploadFile = File(...)):
return {"msg": "Route"}
async def handleRoute(routeBody: RouteBody = Form(), profilePicture: UploadFile = File(...)):
return {"msg": "Route"}
And I test it out using SwaggerUI docs:
I get the following error:
error [{'type': 'model_attributes_type', 'loc': ('body', 'routeBody'), 'msg': 'Input should be a valid dictionary or object to extract fields from', 'input': '{"email":"[email protected]","password":"string"}'}]
error [{'type': 'model_attributes_type', 'loc': ('body', 'routeBody'), 'msg': 'Input should be a valid dictionary or object to extract fields from', 'input': '{"email":"[email protected]","password":"string"}'}]
[email protected]
Tested it out on a separate route without the "RouteBody" and it worked perfectly.
Rewrote Route Handler from Scratch
Changed the order of the parameters (don't know why I thought this mattered ... maybe it did?)
Instead of using a Pydantic Model type for the RouteBody, I opted for individual parameters to make it functional. However, this isn’t an ideal solution, as it requires listing out all the parameters if you have many of them.
Asked ChatGPT for guidance
Tested it out on a separate route without the "RouteBody" and it worked perfectly.
Rewrote Route Handler from Scratch
Changed the order of the parameters (don't know why I thought this mattered ... maybe it did?)
Instead of using a Pydantic Model type for the RouteBody, I opted for individual parameters to make it functional. However, this isn’t an ideal solution, as it requires listing out all the parameters if you have many of them.
Asked ChatGPT for guidance
Tags: python,swagger,fastapi,multipartform-data,openapiSource of the question:
Question and source license information: