fastapi/fastapi
Parameters
Purpose
The "Param" family is how FastAPI knows whether a function argument should come from the path, the query string, headers, cookies, body, form data, or a file. Two files together define this surface:
fastapi/params.py— class hierarchy ofpydantic.FieldInfosubclasses.fastapi/param_functions.py— functional wrappers (Path(...),Query(...),Header(...),Cookie(...),Body(...),Form(...),File(...),Depends(...),Security(...)).
The functional wrappers are the names users import. Each calls into the corresponding class with the right defaults.
Key abstractions
Symbol (in fastapi/params.py) |
Purpose |
|---|---|
ParamTypes |
Enum of query, header, path, cookie. |
Param |
Base subclass of pydantic.FieldInfo. Carries in_: ParamTypes and include_in_schema. |
Path, Query, Header, Cookie |
Concrete Param subclasses. Header adds convert_underscores (translates user_agent → user-agent). |
Body |
FieldInfo subclass for JSON bodies. Carries media_type, embed, examples. |
Form, File |
Body subclasses with media types application/x-www-form-urlencoded and multipart/form-data. File ties into UploadFile. |
Depends |
Dataclass holding a callable plus use_cache: bool and scope. Recognised by the dependency resolver. |
Security |
Depends subclass that additionally carries scopes: Sequence[str] for OAuth2. |
The functional wrappers in fastapi/param_functions.py accept the same long parameter list (alias options, validation constraints, examples, deprecation flags, OpenAPI metadata, etc.) and return an instance of the appropriate class. The wrappers exist so that user code reads as Annotated[str, Query(min_length=3)] rather than Annotated[str, params.Query(min_length=3)].
How it works
graph LR
User["param: Annotated[str, Query(min_length=3)] = 'x'"]
User --> Sig[Function signature inspection]
Sig --> GetDep["get_dependant(call=endpoint)"]
GetDep --> Analyze[Walk parameters, classify by FieldInfo subclass]
Analyze --> Dependant["Dependant.path_params / .query_params / .header_params / .cookie_params / .body_params / .dependencies"]
Dependant --> Solve["solve_dependencies"]
Solve --> Validate["Pydantic ModelField.validate"]
Validate --> EndpointThe classification logic lives in analyze_param and surrounding code in fastapi/dependencies/utils.py. It walks each function parameter and decides:
- Is the metadata a
Param/Body/Form/File? Use that. - Is the metadata a
Depends/Security? Add it as a sub-dependency. - Is the parameter name in the path? Treat as
Path. - Is it a Pydantic model annotation? Treat as
Body(orQueryif the model is configured for query params). - Otherwise treat as
Query.
Each parameter becomes a ModelField (fastapi/_compat/v2.py), and the pile is grouped onto the route's Dependant. At request time solve_dependencies (fastapi/dependencies/utils.py) reads the request, applies validation, and produces kwargs for the endpoint.
Notable behaviours
Annotatedis the recommended style since FastAPI ≥0.95. The function default is the runtime default; metadata describes the parameter. Old-style defaults (def f(name: str = Query(default="x"))) still work and are extensively tested.Headerunderscore conversion. Whenconvert_underscores=True(default),user_agent: strreads from theUser-Agentheader. Setconvert_underscores=Falseto opt out.FormandFilerequirepython-multipartto be installed.fastapi/dependencies/utils.py:ensure_multipart_is_installedraises a clear error if it is missing or if the wrongmultipartpackage is installed instead.regex=is deprecated — usepattern=. The deprecated alias is plumbed through with atyping_extensions.deprecatedmarker that firesFastAPIDeprecationWarning.Depends()with no callable infers the callable from the parameter's type annotation. Sodb: Annotated[Database, Depends()]is shorthand forDepends(Database).
Integration points
- Read by
fastapi/dependencies/utils.py:get_dependantto build theDependanttree. - Read by
fastapi/openapi/utils.py:_get_openapi_operation_parameters(and related helpers) to populate the OpenAPI parameters and request body. - Used directly by user code through
fastapi/__init__.pyre-exports of the function wrappers.
Entry points for modification
- New parameter location: extend
ParamTypesand add a class infastapi/params.py, a wrapper infastapi/param_functions.py, classification infastapi/dependencies/utils.py:analyze_param, and OpenAPI plumbing infastapi/openapi/utils.py. - New validation option: add it to
Param.__init__(orBody.__init__), plumb it into the function wrappers infastapi/param_functions.py, and forward topydantic.fields.FieldInfo. - Deprecation: wrap the parameter with
typing_extensions.deprecated(...)in the function wrapper signature, raiseFastAPIDeprecationWarningwhen used.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.
Previous
Routing
Next
Security