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.

[QUESTION] Use UploadFile in Pydantic model

See original GitHub issue

Description

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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:28 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
divyeshwendorsecommented, May 4, 2020

Thanks a lot for your helpful comment. So I guess I’d have to explicitly separate the file from the JSON part of the multipart form body, as in:

class Properties(BaseModel):
    language: str = None
    author: str = None

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...),
                             properties: Properties):
    return {"filename": file.filename}

This seems to be working, and maybe query parameters would ultimately make more sense here.

Hello, This is giving an validation Error (422)

10reactions
perezzinicommented, Jun 6, 2020

Thanks for replying back @tiangolo

And congrats on your great job in FastAPI!

Read more comments on GitHub >

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

github_iconTop Related Medium Post

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