<p dir="auto">Here's a <a href="https://github.com/thomasleveil/fastapi/tree/file_and_form_together">branch</a> with my use case:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from starlette.testclient import TestClient

from fastapi import FastAPI, File, Form

app = FastAPI()


@app.post(&quot;/file_before_form&quot;)
def file_before_form(
    file: bytes = File(...), city: str = Form(...),
):
    return {&quot;file_content&quot;: file, &quot;city&quot;: city}


@app.post(&quot;/file_after_form&quot;)
def file_after_form(
    city: str = Form(...), file: bytes = File(...),
):
    return {&quot;file_content&quot;: file, &quot;city&quot;: city}


client = TestClient(app)


def test_file_before_form():
    response = client.post(
        &quot;/file_before_form&quot;, data={&quot;city&quot;: &quot;Thimphou&quot;}, files={&quot;file&quot;: &quot;&lt;file content&gt;&quot;}
    )
    assert response.status_code == 200, response.text
    assert response.json() == {&quot;file_content&quot;: &quot;&lt;file content&gt;&quot;, &quot;city&quot;: &quot;Thimphou&quot;}


def test_file_after_form():
    response = client.post(
        &quot;/file_after_form&quot;, data={&quot;city&quot;: &quot;Thimphou&quot;}, files={&quot;file&quot;: &quot;&lt;file content&gt;&quot;}
    )
    assert response.status_code == 200, response.text  # &lt;-------------- this fails since ab2b86f
    assert response.json() == {&quot;file_content&quot;: &quot;&lt;file content&gt;&quot;, &quot;city&quot;: &quot;Thimphou&quot;}
"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">starlette</span>.<span class="pl-s1">testclient</span> <span class="pl-k">import</span> <span class="pl-v">TestClient</span>

<span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>, <span class="pl-v">File</span>, <span class="pl-v">Form</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/file_before_form"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">file_before_form</span>(
    <span class="pl-s1">file</span>: <span class="pl-s1">bytes</span> <span class="pl-c1">=</span> <span class="pl-v">File</span>(...), <span class="pl-s1">city</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-v">Form</span>(...),
):
    <span class="pl-k">return</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s1">file</span>, <span class="pl-s">"city"</span>: <span class="pl-s1">city</span>}


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">post</span>(<span class="pl-s">"/file_after_form"</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">file_after_form</span>(
    <span class="pl-s1">city</span>: <span class="pl-s1">str</span> <span class="pl-c1">=</span> <span class="pl-v">Form</span>(...), <span class="pl-s1">file</span>: <span class="pl-s1">bytes</span> <span class="pl-c1">=</span> <span class="pl-v">File</span>(...),
):
    <span class="pl-k">return</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s1">file</span>, <span class="pl-s">"city"</span>: <span class="pl-s1">city</span>}


<span class="pl-s1">client</span> <span class="pl-c1">=</span> <span class="pl-v">TestClient</span>(<span class="pl-s1">app</span>)


<span class="pl-k">def</span> <span class="pl-en">test_file_before_form</span>():
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-en">post</span>(
        <span class="pl-s">"/file_before_form"</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span>{<span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}, <span class="pl-s1">files</span><span class="pl-c1">=</span>{<span class="pl-s">"file"</span>: <span class="pl-s">"&lt;file content&gt;"</span>}
    )
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-s1">status_code</span> <span class="pl-c1">==</span> <span class="pl-c1">200</span>, <span class="pl-s1">response</span>.<span class="pl-s1">text</span>
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-en">json</span>() <span class="pl-c1">==</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s">"&lt;file content&gt;"</span>, <span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}


<span class="pl-k">def</span> <span class="pl-en">test_file_after_form</span>():
    <span class="pl-s1">response</span> <span class="pl-c1">=</span> <span class="pl-s1">client</span>.<span class="pl-en">post</span>(
        <span class="pl-s">"/file_after_form"</span>, <span class="pl-s1">data</span><span class="pl-c1">=</span>{<span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}, <span class="pl-s1">files</span><span class="pl-c1">=</span>{<span class="pl-s">"file"</span>: <span class="pl-s">"&lt;file content&gt;"</span>}
    )
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-s1">status_code</span> <span class="pl-c1">==</span> <span class="pl-c1">200</span>, <span class="pl-s1">response</span>.<span class="pl-s1">text</span>  <span class="pl-c"># &lt;-------------- this fails since ab2b86f</span>
    <span class="pl-k">assert</span> <span class="pl-s1">response</span>.<span class="pl-en">json</span>() <span class="pl-c1">==</span> {<span class="pl-s">"file_content"</span>: <span class="pl-s">"&lt;file content&gt;"</span>, <span class="pl-s">"city"</span>: <span class="pl-s">"Thimphou"</span>}</pre></div>
<h3 dir="auto">Description</h3>
<p dir="auto">Upgrading FastApi from 0.42.0 to 0.61.0 on one of my project, I stumbled upon a regression in which FastApi will respond with HTTP Error 422 when ever posting a form-data request to an endpoint where a <code class="notranslate">File</code> is declared <strong>after</strong> a <code class="notranslate">Form</code>.</p>
<p dir="auto">Searching the Internet, I found another user suffering from this issue : <a href="https://stackoverflow.com/questions/61376754/fastapi-files-must-be-loaded-before-form-in-function-parameters" rel="nofollow">https://stackoverflow.com/questions/61376754/fastapi-files-must-be-loaded-before-form-in-function-parameters</a></p>
<p dir="auto">I've created a branch with tests to showcase the issue : <a href="https://github.com/thomasleveil/fastapi/tree/file_and_form_together">https://github.com/thomasleveil/fastapi/tree/file_and_form_together</a></p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: Linux</li>
<li>FastAPI Version 0.44.1 and later</li>
<li>Python version: 3.8.5</li>
</ul>
<h3 dir="auto">Additional context</h3>
<p dir="auto">I've bisected the repo and figured out the regression was introduced in commit <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/tiangolo/fastapi/commit/ab2b86fe2ce8fe15e91aaec179438e24ff7b7ed0/hovercard" href="https://github.com/tiangolo/fastapi/commit/ab2b86fe2ce8fe15e91aaec179438e24ff7b7ed0"><tt>ab2b86f</tt></a></p>
<h3 dir="auto">First check</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I added a very descriptive title to this issue.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I used the GitHub search to find a similar issue and didn't find it.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I searched the FastAPI documentation, with the integrated search.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already searched in Google "How to X in FastAPI" and didn't find any information.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already read and followed all the tutorial in the docs and didn't find an answer.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/samuelcolvin/pydantic">Pydantic</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/swagger-api/swagger-ui">Swagger UI</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I already checked if it is not related to FastAPI but to <a href="https://github.com/Redocly/redoc">ReDoc</a>.</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> After submitting this, I commit to one of:
<ul dir="auto">
<li>Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.</li>
<li>I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.</li>
<li>Implement a Pull Request for a confirmed bug.</li>
</ul>
</li>
</ul>

<h3 dir="auto">Description + Example</h3>
<p dir="auto">I have an endpoint that returns an instance of this <code class="notranslate">Container</code> class, which has a list as one of its attributes, called <code class="notranslate">items</code>.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import logging
from pydantic import BaseModel, Field
from typing import List

class Container(BaseModel):
    items: List[int] = Field(default_factory=list)

    def __init__(self, **data):
        logging.debug(f'Init-ing Container with {data}')
        super().__init__(**data)

        # At runtime, compute some value and add to the list
        self.items.append(99)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-s1">logging</span>
<span class="pl-k">from</span> <span class="pl-s1">pydantic</span> <span class="pl-k">import</span> <span class="pl-v">BaseModel</span>, <span class="pl-v">Field</span>
<span class="pl-k">from</span> <span class="pl-s1">typing</span> <span class="pl-k">import</span> <span class="pl-v">List</span>

<span class="pl-k">class</span> <span class="pl-v">Container</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">items</span>: <span class="pl-v">List</span>[<span class="pl-s1">int</span>] <span class="pl-c1">=</span> <span class="pl-v">Field</span>(<span class="pl-s1">default_factory</span><span class="pl-c1">=</span><span class="pl-s1">list</span>)

    <span class="pl-k">def</span> <span class="pl-en">__init__</span>(<span class="pl-s1">self</span>, <span class="pl-c1">**</span><span class="pl-s1">data</span>):
        <span class="pl-s1">logging</span>.<span class="pl-en">debug</span>(<span class="pl-s">f'Init-ing Container with <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-s1">data</span><span class="pl-kos">}</span></span>'</span>)
        <span class="pl-en">super</span>().<span class="pl-en">__init__</span>(<span class="pl-c1">**</span><span class="pl-s1">data</span>)

        <span class="pl-c"># At runtime, compute some value and add to the list</span>
        <span class="pl-s1">self</span>.<span class="pl-s1">items</span>.<span class="pl-en">append</span>(<span class="pl-c1">99</span>)</pre></div>
<p dir="auto">As shown, I overrode the <code class="notranslate">__init__</code> because I have to "internally" modify the list after instantiation (same as in the <a href="https://pydantic-docs.helpmanual.io/usage/models/#private-model-attributes" rel="nofollow">Private Model Attributes</a> section of the Pydantic docs). I'm also using the <a href="https://pydantic-docs.helpmanual.io/usage/models/#field-with-dynamic-default-value" rel="nofollow"><code class="notranslate">Field(default_factory=list)</code></a> to make sure each instance creates its own list.</p>
<p dir="auto">Testing the class on its own works as expected:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="container_1 = Container(items=[1, 2, 3])
logging.debug(container_1.items)
container_2 = Container(items=[4, 5, 6])
logging.debug(container_2.items)

logging.debug(f'{id(container_1.items)}, {id(container_2.items)}')
assert id(container_1.items) != id(container_2.items)"><pre class="notranslate"><span class="pl-s1">container_1</span> <span class="pl-c1">=</span> <span class="pl-v">Container</span>(<span class="pl-s1">items</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>])
<span class="pl-s1">logging</span>.<span class="pl-en">debug</span>(<span class="pl-s1">container_1</span>.<span class="pl-s1">items</span>)
<span class="pl-s1">container_2</span> <span class="pl-c1">=</span> <span class="pl-v">Container</span>(<span class="pl-s1">items</span><span class="pl-c1">=</span>[<span class="pl-c1">4</span>, <span class="pl-c1">5</span>, <span class="pl-c1">6</span>])
<span class="pl-s1">logging</span>.<span class="pl-en">debug</span>(<span class="pl-s1">container_2</span>.<span class="pl-s1">items</span>)

<span class="pl-s1">logging</span>.<span class="pl-en">debug</span>(<span class="pl-s">f'<span class="pl-s1"><span class="pl-kos">{</span><span class="pl-en">id</span>(<span class="pl-s1">container_1</span>.<span class="pl-s1">items</span>)<span class="pl-kos">}</span></span>, <span class="pl-s1"><span class="pl-kos">{</span><span class="pl-en">id</span>(<span class="pl-s1">container_2</span>.<span class="pl-s1">items</span>)<span class="pl-kos">}</span></span>'</span>)
<span class="pl-k">assert</span> <span class="pl-en">id</span>(<span class="pl-s1">container_1</span>.<span class="pl-s1">items</span>) <span class="pl-c1">!=</span> <span class="pl-en">id</span>(<span class="pl-s1">container_2</span>.<span class="pl-s1">items</span>)</pre></div>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[2020-11-17 08:58:24.713] root:DEBUG    Init-ing Container with {'items': [1, 2, 3]}
[2020-11-17 08:58:24.713] root:DEBUG    [1, 2, 3, 99]
[2020-11-17 08:58:24.713] root:DEBUG    Init-ing Container with {'items': [4, 5, 6]}
[2020-11-17 08:58:24.713] root:DEBUG    [4, 5, 6, 99]
[2020-11-17 09:43:22.566] root:DEBUG    4481341504, 4481418560"><pre class="notranslate">[2020-11-17 08:58:24.713] root:DEBUG    Init-ing Container with {<span class="pl-s"><span class="pl-pds">'</span>items<span class="pl-pds">'</span></span>: [1, 2, 3]}
[2020-11-17 08:58:24.713] root:DEBUG    [1, 2, 3, 99]
[2020-11-17 08:58:24.713] root:DEBUG    Init-ing Container with {<span class="pl-s"><span class="pl-pds">'</span>items<span class="pl-pds">'</span></span>: [4, 5, 6]}
[2020-11-17 08:58:24.713] root:DEBUG    [4, 5, 6, 99]
[2020-11-17 09:43:22.566] root:DEBUG    4481341504, 4481418560</pre></div>
<p dir="auto">Now, I set it as a <a href="https://fastapi.tiangolo.com/tutorial/response-model/" rel="nofollow">response_model</a> to one of my endpoints:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@api.get('/things', response_model=Container)
def get_things():
    return Container(items=[1, 2, 3])"><pre class="notranslate"><span class="pl-en">@<span class="pl-s1">api</span>.<span class="pl-en">get</span>(<span class="pl-s">'/things'</span>, <span class="pl-s1">response_model</span><span class="pl-c1">=</span><span class="pl-v">Container</span>)</span>
<span class="pl-k">def</span> <span class="pl-en">get_things</span>():
    <span class="pl-k">return</span> <span class="pl-v">Container</span>(<span class="pl-s1">items</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>])</pre></div>
<p dir="auto">The response I get however has a duplicate <code class="notranslate">99</code> value:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/6091865/99323517-c2f59c80-28b5-11eb-80fe-bbfb2291a732.png"><img src="https://user-images.githubusercontent.com/6091865/99323517-c2f59c80-28b5-11eb-80fe-bbfb2291a732.png" alt="Screen Shot 2020-11-17 at 9 17 23" style="max-width: 100%;"></a></p>
<p dir="auto">This seems to indicate that the <code class="notranslate">__init__</code> was called <strong>twice</strong> on the same object? It's somehow related to the <a href="https://stackoverflow.com/q/1132941/2745495" rel="nofollow">Mutable Default Argument</a> problem. Hitting the endpoint multiple times does not keep adding <code class="notranslate">99</code>, so it seems it is still partially correct that it instantiates a new object, but seems to call <code class="notranslate">__init__</code> twice.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="[2020-11-17 09:23:08.501] root:DEBUG    Init-ing Container with {'items': [1, 2, 3]}
[2020-11-17 09:23:08.502] root:DEBUG    Init-ing Container with {'items': [1, 2, 3, 99]}"><pre class="notranslate">[2020-11-17 09:23:08.501] root:DEBUG    Init-ing Container with {<span class="pl-s"><span class="pl-pds">'</span>items<span class="pl-pds">'</span></span>: [1, 2, 3]}
[2020-11-17 09:23:08.502] root:DEBUG    Init-ing Container with {<span class="pl-s"><span class="pl-pds">'</span>items<span class="pl-pds">'</span></span>: [1, 2, 3, 99]}</pre></div>
<p dir="auto">But testing the class on its own does not have this issue, and my endpoint is instantiating the class only once here. So, what am I doing wrong here?</p>
<p dir="auto">Now, if I use instead the <a href="https://fastapi.tiangolo.com/advanced/additional-responses/" rel="nofollow">responses</a> parameter:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@api.get('/things', responses={200: {'model': Container}})
def get_things():
    return Container(items=[1, 2, 3])"><pre class="notranslate"><span class="pl-en">@<span class="pl-s1">api</span>.<span class="pl-en">get</span>(<span class="pl-s">'/things'</span>, <span class="pl-s1">responses</span><span class="pl-c1">=</span>{<span class="pl-c1">200</span>: {<span class="pl-s">'model'</span>: <span class="pl-v">Container</span>}})</span>
<span class="pl-k">def</span> <span class="pl-en">get_things</span>():
    <span class="pl-k">return</span> <span class="pl-v">Container</span>(<span class="pl-s1">items</span><span class="pl-c1">=</span>[<span class="pl-c1">1</span>, <span class="pl-c1">2</span>, <span class="pl-c1">3</span>])</pre></div>
<p dir="auto">I get the correct response:<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/6091865/99324030-b9b8ff80-28b6-11eb-8258-92921c28ba5b.png"><img src="https://user-images.githubusercontent.com/6091865/99324030-b9b8ff80-28b6-11eb-8258-92921c28ba5b.png" alt="Screen Shot 2020-11-17 at 9 24 10" style="max-width: 100%;"></a></p>
<p dir="auto">And the logs correctly show only one instantiation:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="[2020-11-17 09:23:59.786] root:DEBUG    Init-ing Container with {'items': [1, 2, 3]}"><pre class="notranslate"><code class="notranslate">[2020-11-17 09:23:59.786] root:DEBUG    Init-ing Container with {'items': [1, 2, 3]}
</code></pre></div>
<p dir="auto">So, what is wrong with my usage of the <code class="notranslate">response_model</code> parameter?</p>
<p dir="auto">As for the Swagger/OpenAPI schema, using either <code class="notranslate">response_model</code> or <code class="notranslate">responses</code> show the current schema in the docs, so no problem/difference there. Also, I'm actually not sure if this is a Pydantic or FastAPI issue, but using just the class <em>outside of an endpoint</em> that does not show this problem. So I figure, it's related to being used as a <code class="notranslate">response_model</code>.</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>OS: <strong>macOS 10.15.7</strong></li>
<li>FastAPI Version: <strong>0.61.2</strong></li>
<li>Pydantic Version: <strong>1.7.2</strong></li>
<li>Python Version: <strong>3.8.6</strong></li>
</ul>