Skip to content

Conversation

@YuriiMotov
Copy link
Member

@YuriiMotov YuriiMotov commented Nov 12, 2025

Currently if form parameter model contains File field, the content mediatype for this path operation in openapi will be wrong:

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

app = FastAPI()

class MyModel(BaseModel):
    file_model: bytes = File()

@app.post("/file-model")
async def create_item(item: MyModel = Form()):
    return {"file_size": len(item.file_model)}

Request body content mediatype in openapi will be application/x-www-form-urlencoded instead of multipart/form-data:

        "requestBody": {
          "content": {
            "application/x-www-form-urlencoded": {
              "schema": {
                "$ref": "#/components/schemas/MyModel"
              }
            }
          },
          "required": true
        },

This PR enforces the content mediatype to be multipart/form-data for form parameter models that contain at least one File field.
I added if request_media_type == "application/x-www-form-urlencoded" condition to make it possible to override mediatype by passing a parameter to Form:

@app.post("/file-model")
async def create_item(item: MyModel = Form(media_type="whatever")):
    ...

@YuriiMotov YuriiMotov added the bug Something isn't working label Nov 12, 2025
@github-actions
Copy link
Contributor

@YuriiMotov YuriiMotov marked this pull request as ready for review November 13, 2025 10:20

You can also include **file fields** inside a form model by using `UploadFile` or `bytes` as field types and annotating them with `File`.

If there is at least one `File` field in the form model, **FastAPI** will automatically set the request content media type to `multipart/form-data` for this path operation.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add here a note to indicate from which version onwards, this is the case? Something like

Suggested change
If there is at least one `File` field in the form model, **FastAPI** will automatically set the request content media type to `multipart/form-data` for this path operation.
If there is at least one `File` field in the form model, **FastAPI** (from v.0.121.3 onwards) will automatically set the request content media type to `multipart/form-data` for this path operation.

@tiangolo
Copy link
Member

tiangolo commented Dec 2, 2025

I'm unsure about this. 🤔

I wouldn't expect people to create Pydantic models where the fields are declared with something other than Pydantic's regular Field(). I wouldn't expect them to use File() in a Pydantic model field.

@YuriiMotov

This comment was marked as resolved.

@YuriiMotov
Copy link
Member Author

Closing this as explained here and here

@YuriiMotov YuriiMotov closed this Dec 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants