[QUESTION] Use UploadFile in Pydantic model
See original GitHub issueDescription
Is it possible to use UploadFile
in a Pydantic model? The FastAPI docs say “FastAPI’s UploadFile inherits directly from Starlette’s UploadFile, but adds some necessary parts to make it compatible with Pydantic and the other parts of FastAPI.”
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
class PydanticFile(BaseModel):
file: UploadFile = File(...)
app = FastAPI()
@app.post("/uploadfile/")
async def create_upload_file(file: PydanticFile):
return {"filename": file.filename}
However, UploadFile
does not seem to be compatible with Pydantic. The code above – derived from the example in FastAPI’s Request Files tutorial – raises a value error:
ValueError: Value not declarable with JSON Schema, field: file type=UploadFile required
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:28 (3 by maintainers)
Top Results From Across the Web
Pydantic params validation with file upload - Stack Overflow
You can't have them at the same time, since pydantic models validate json body but file uploads is sent in form-data . So...
Read more >How to use FastAPI UploadFile with Pydantic model
This is a short tutorial to guide you on how to use Pydantic models and UploadFile together in FastAPI. I had to figure...
Read more >Create products and explore File upload handling
If you curious about this discussion please read on here -> Using UploadFile and Pydantic model in one request (opens new window) and...
Read more >Request Files - FastAPI
You can define files to be uploaded by the client using File . To receive uploaded files, first install python-multipart . E.g. pip...
Read more >tiangolo/fastapi - Gitter
It has also pydantic support which i think the equivalent of your schemas. janvdvegt ... I ha e a question on how to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
Hello, This is giving an validation Error (422)
Thanks for replying back @tiangolo
And congrats on your great job in FastAPI!