tiangolo/fastapi · critical · RuntimeError

Form data requires "python-multipart" to be installed. You

Error message

Form data requires "python-multipart" to be installed. 
You can install "python-multipart" with: 

pip install python-multipart

What it means

RuntimeError raised by ensure_multipart_is_installed() at dependencies/utils.py:129 when form-data handling is required and NEITHER `python-multipart` nor `multipart` is importable. The function tries python_multipart (with version > 0.0.12 assertion), then falls back to `multipart`; if both imports fail it logs multipart_not_installed_error and raises RuntimeError. It is triggered the first time a Form/File/UploadFile parameter is processed.

Source

Thrown at fastapi/dependencies/utils.py:129

            # __version__ is available in both multiparts, and can be mocked
            from multipart import (  # type: ignore[no-redef,import-untyped]
                __version__,
            )

            assert __version__
            try:
                # parse_options_header is only available in the right multipart
                from multipart.multipart import (  # type: ignore[import-untyped]
                    parse_options_header,
                )

                assert parse_options_header
            except ImportError:
                logger.error(multipart_incorrect_install_error)
                raise RuntimeError(multipart_incorrect_install_error) from None
        except ImportError:
            logger.error(multipart_not_installed_error)
            raise RuntimeError(multipart_not_installed_error) from None


def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> Dependant:
    assert callable(depends.dependency), (
        "A parameter-less dependency must have a callable dependency"
    )
    own_oauth_scopes: list[str] = []
    if isinstance(depends, params.Security) and depends.scopes:
        own_oauth_scopes.extend(depends.scopes)
    return get_dependant(
        path=path,
        call=depends.dependency,
        scope=depends.scope,
        own_oauth_scopes=own_oauth_scopes,
    )


def _get_flat_body_params(dependant: Dependant) -> list[ModelField]:

View on GitHub (pinned to 42a41db11f)

Solutions

  1. Install python-multipart: pip install python-multipart (or pip install 'fastapi[standard]').
  2. Add python-multipart to requirements.txt/pyproject.toml dependencies.
  3. Rebuild the virtualenv/image after updating requirements.
  4. Verify: python -c 'import python_multipart; print(python_multipart.__version__)'.

Example fix

# before
pip install fastapi
# after
pip install python-multipart
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util, sys, subprocess
if importlib.util.find_spec('python_multipart') is None:
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'python-multipart'])

Type guard

def multipart_installed() -> bool:
    import importlib.util
    return importlib.util.find_spec('python_multipart') is not None

Try / catch

from fastapi.dependencies.utils import ensure_multipart_is_installed
try:
    ensure_multipart_is_installed()
except RuntimeError:
    import subprocess, sys
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'python-multipart'])

Prevention

When it happens

Trigger: Declaring a Form(), File(), or UploadFile parameter while python-multipart is not installed at all. The dependency is detected when FastAPI builds the body model for the path operation, so the error surfaces on first form-handling request (or at app setup in stricter modes).

Common situations: Bare `pip install fastapi` (python-multipart is an optional extra); fresh env where the form library was forgotten; production image that strips optional deps; upgrading FastAPI without re-adding python-multipart.

Related errors


AI-assisted analysis of tiangolo/fastapi@42a41db11f (2026-08-04). Data as JSON: /data/errors/3ccff8d0c7715291.json. Report an issue: GitHub.