网页 综合 更新时间 2026-11-14 05:19:07

Release Notes - FastAPI

自动蜘蛛 fastapi.tiangolo.com 正文约 6000 字
Use the official FastAPI skill for your installed version. Install it from your project directory:
See Develop with AI.
Omitted bool and bool | None form fields now use their declared defaults or produce missing-field errors, consistent with other fields and Pydantic behavior. FastAPI no longer infers False for omitted booleans. This applies to individual form parameters, form models, and unions of form models.
Omitted boolean model fields are excluded from model_dump(exclude_unset=True), and their defaults no longer count as submitted fields during union selection. Default factories and validate_default follow the usual rules.
For HTML checkboxes, declare a boolean with a default of False. See Boolean Form Fields.
Query, header, cookie, and form parameter models now apply their extra configuration to all submitted fields in the same parameter location, consistent with Pydantic behavior. This includes fields declared in other models, individual parameters, or dependencies, which were previously excluded.
With extra="forbid", fields outside the model are rejected even if another parameter declares them. With extra="allow", those fields are included as model extras. With the default extra="ignore", they are ignored.
The deprecated **extra keyword arguments have been removed from Path, Query, Header, Cookie, Body, Form, and File. Unknown keyword arguments now raise TypeError. For additional JSON Schema properties, replace, for example, Query(custom="value") with Query(json_schema_extra={"custom": "value"}).
Unions of form models now apply the same browser form handling as individual models. Empty scalar fields use defaults or produce missing-field errors, empty list items are preserved, omitted boolean fields become False, and unselected files are treated as missing. JSON request bodies are unchanged.
Validation errors for header parameter models now report the HTTP header name, such as int-val, instead of the Python field name, int_val. Explicit aliases, Header(convert_underscores=False), and Pydantic models with loc_by_alias=False keep their existing behavior.
JSON request bodies now distinguish an explicit null from an omitted value. For individual Body parameters, null is validated as None: nullable types accept it, and non-nullable types reject it even when a default is defined. Defaults and default factories apply only when the value is omitted. This applies both to parameters inside JSON objects and to parameters representing the entire body. Clients that used null to request a default should omit the value instead.
When a JSON object is expected because of Body(embed=True) or multiple body parameters, sending null as the entire body now returns a validation error.
When no file is selected, browsers submit a file part with an empty filename and empty content. FastAPI now treats this as missing: required file fields return a validation error, and fields with defaults or default factories use them. Unselected inputs are ignored in lists of files. Named zero-byte files are still accepted.
Browsers submit empty text inputs as empty strings (""). FastAPI now treats empty scalar form values as missing in form models, matching individual Form parameters. Required fields return a validation error. Fields with defaults or default factories use them and remain unset in model_dump(exclude_unset=True).
Empty strings inside lists are preserved. Omitted boolean form fields still become False. JSON requests and direct Pydantic validation are unchanged.
Defaults for omitted Query, Header, Cookie, and Form parameters and model fields are no longer treated as user input. Defaults are not validated unless validate_default=True is enabled. If you rely on validators running for omitted values, enable default validation with Field(validate_default=True) for model fields or, for example, Query(validate_default=True) for individual parameters. Body(validate_default=True) is now honored too.
For parameter models, applying a default no longer marks a field as explicitly set, so model_dump(exclude_unset=True) excludes it. Default factories run once, and validation errors no longer include injected defaults in input.
Omitted boolean form fields still become False and count as explicitly set.
Omitted bool and bool | None form fields are now interpreted as False, matching unchecked HTML checkboxes. This applies to individual Form parameters and form models, including required fields and fields with defaults of True or None. The inferred value is considered explicitly set, including in model_dump(exclude_unset=True).
JSON requests and direct Pydantic validation are unchanged. See Boolean Form Fields.
The root_path parameter in FastAPI() is deprecated. Configure the server with fastapi run --root-path /api/v1 instead.
Automatic OpenTelemetry exporter setup is now opt-in. If you rely on FastAPI to configure exporters from OTEL_* environment variables, set FASTAPI_OTEL_AUTO_CONFIGURE=true or use FastAPI(telemetry={"auto_configure": True}) to retain the previous behavior.
Applications that configure their own providers and exporters require no changes. FastAPI Cloud enables automatic setup for you.
Unblocks ✨ SO MANY THINGS ✨
Before this, router.include_router(other_router) would take each path operation from other_router and "clone" it, or recreate it from scratch.
This would mean that in the end there was only one top level router, part of the app.
The way it is structured here is that there are a few additional classes to handle intermediate metadata for router and route inclusion. That way the information of "router X includes Y and Y includes Z" is stored somewhere, without affecting (recreating / cloning) the final route.
Dependencies for 404: previously I intended to support dependencies that would be executed even for 404, but that would conflict with the fact that a router could not find a match, but the next router did find a match. Executing dependencies in the router that did not find a match would not mak
访问原文 返回搜索 https://fastapi.tiangolo.com/./release-notes/
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。