Skip to content

Releases: fastapi/fastapi

0.115.0

17 Sep 19:17
Compare
Choose a tag to compare

Highlights

Now you can declare Query, Header, and Cookie parameters with Pydantic models. 🎉

Query Parameter Models

Use Pydantic models for Query parameters:

from typing import Annotated, Literal

from fastapi import FastAPI, Query
from pydantic import BaseModel, Field

app = FastAPI()


class FilterParams(BaseModel):
    limit: int = Field(100, gt=0, le=100)
    offset: int = Field(0, ge=0)
    order_by: Literal["created_at", "updated_at"] = "created_at"
    tags: list[str] = []


@app.get("/items/")
async def read_items(filter_query: Annotated[FilterParams, Query()]):
    return filter_query

Read the new docs: Query Parameter Models.

Header Parameter Models

Use Pydantic models for Header parameters:

from typing import Annotated

from fastapi import FastAPI, Header
from pydantic import BaseModel

app = FastAPI()


class CommonHeaders(BaseModel):
    host: str
    save_data: bool
    if_modified_since: str | None = None
    traceparent: str | None = None
    x_tag: list[str] = []


@app.get("/items/")
async def read_items(headers: Annotated[CommonHeaders, Header()]):
    return headers

Read the new docs: Header Parameter Models.

Cookie Parameter Models

Use Pydantic models for Cookie parameters:

from typing import Annotated

from fastapi import Cookie, FastAPI
from pydantic import BaseModel

app = FastAPI()


class Cookies(BaseModel):
    session_id: str
    fatebook_tracker: str | None = None
    googall_tracker: str | None = None


@app.get("/items/")
async def read_items(cookies: Annotated[Cookies, Cookie()]):
    return cookies

Read the new docs: Cookie Parameter Models.

Forbid Extra Query (Cookie, Header) Parameters

Use Pydantic models to restrict extra values for Query parameters (also applies to Header and Cookie parameters).

To achieve it, use Pydantic's model_config = {"extra": "forbid"}:

from typing import Annotated, Literal

from fastapi import FastAPI, Query
from pydantic import BaseModel, Field

app = FastAPI()


class FilterParams(BaseModel):
    model_config = {"extra": "forbid"}

    limit: int = Field(100, gt=0, le=100)
    offset: int = Field(0, ge=0)
    order_by: Literal["created_at", "updated_at"] = "created_at"
    tags: list[str] = []


@app.get("/items/")
async def read_items(filter_query: Annotated[FilterParams, Query()]):
    return filter_query

This applies to Query, Header, and Cookie parameters, read the new docs:

Features

  • ✨ Add support for Pydantic models for parameters using Query, Cookie, Header. PR #12199 by @tiangolo.

Translations

  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/security/http-basic-auth.md. PR #12195 by @ceb10n.

Internal

0.114.2

13 Sep 20:47
Compare
Choose a tag to compare

Fixes

Translations

  • 🌐 Add Portuguese translation for docs/pt/docs/tutorial/request-form-models.md. PR #12175 by @ceb10n.
  • 🌐 Add Chinese translation for docs/zh/docs/project-generation.md. PR #12170 by @waketzheng.
  • 🌐 Add Dutch translation for docs/nl/docs/python-types.md. PR #12158 by @maxscheijen.

Internal

  • 💡 Add comments with instructions for Playwright screenshot scripts. PR #12193 by @tiangolo.
  • ➕ Add inline-snapshot for tests. PR #12189 by @tiangolo.

0.114.1

11 Sep 07:47
Compare
Choose a tag to compare

Refactors

  • ⚡️ Improve performance in request body parsing with a cache for internal model fields. PR #12184 by @tiangolo.

Docs

  • 📝 Remove duplicate line in docs for docs/en/docs/environment-variables.md. PR #12169 by @prometek.

Translations

  • 🌐 Add Portuguese translation for docs/pt/docs/virtual-environments.md. PR #12163 by @marcelomarkus.
  • 🌐 Add Portuguese translation for docs/pt/docs/environment-variables.md. PR #12162 by @marcelomarkus.
  • 🌐 Add Portuguese translation for docs/pt/docs/tutorial/testing.md. PR #12164 by @marcelomarkus.
  • 🌐 Add Portuguese translation for docs/pt/docs/tutorial/debugging.md. PR #12165 by @marcelomarkus.
  • 🌐 Add Korean translation for docs/ko/docs/project-generation.md. PR #12157 by @BORA040126.

Internal

0.114.0

06 Sep 17:42
Compare
Choose a tag to compare

You can restrict form fields to only include those declared in a Pydantic model and forbid any extra field sent in the request using Pydantic's model_config = {"extra": "forbid"}:

from typing import Annotated

from fastapi import FastAPI, Form
from pydantic import BaseModel

app = FastAPI()


class FormData(BaseModel):
    username: str
    password: str
    model_config = {"extra": "forbid"}


@app.post("/login/")
async def login(data: Annotated[FormData, Form()]):
    return data

Read the new docs: Form Models - Forbid Extra Form Fields.

Features

  • ✨ Add support for forbidding extra form fields with Pydantic models. PR #12134 by @tiangolo.

Docs

  • 📝 Update docs, Form Models section title, to match config name. PR #12152 by @tiangolo.

Internal

  • ✅ Update internal tests for latest Pydantic, including CI tweaks to install the latest Pydantic. PR #12147 by @tiangolo.

0.113.0

05 Sep 15:32
Compare
Choose a tag to compare

Now you can declare form fields with Pydantic models:

from typing import Annotated

from fastapi import FastAPI, Form
from pydantic import BaseModel

app = FastAPI()


class FormData(BaseModel):
    username: str
    password: str


@app.post("/login/")
async def login(data: Annotated[FormData, Form()]):
    return data

Read the new docs: Form Models.

Features

  • ✨ Add support for Pydantic models in Form parameters. PR #12129 by @tiangolo.

Internal

0.112.4

05 Sep 15:06
Compare
Choose a tag to compare

This release is mainly a big internal refactor to enable adding support for Pydantic models for Form fields, but that feature comes in the next release.

This release shouldn't affect apps using FastAPI in any way. You don't even have to upgrade to this version yet. It's just a checkpoint. 🤓

Refactors

  • ♻️ Refactor deciding if embed body fields, do not overwrite fields, compute once per router, refactor internals in preparation for Pydantic models in Form, Query and others. PR #12117 by @tiangolo.

Internal

  • ⏪️ Temporarily revert "✨ Add support for Pydantic models in Form parameters" to make a checkpoint release. PR #12128 by @tiangolo.
  • ✨ Add support for Pydantic models in Form parameters. PR #12127 by @tiangolo. Reverted to make a checkpoint release with only refactors.

0.112.3

05 Sep 09:16
Compare
Choose a tag to compare

This release is mainly internal refactors, it shouldn't affect apps using FastAPI in any way. You don't even have to upgrade to this version yet. There are a few bigger releases coming right after. 🚀

Refactors

  • ♻️ Refactor internal check_file_field(), rename to ensure_multipart_is_installed() to clarify its purpose. PR #12106 by @tiangolo.
  • ♻️ Rename internal create_response_field() to create_model_field() as it's used for more than response models. PR #12103 by @tiangolo.
  • ♻️ Refactor and simplify internal data from solve_dependencies() using dataclasses. PR #12100 by @tiangolo.
  • ♻️ Refactor and simplify internal analyze_param() to structure data with dataclasses instead of tuple. PR #12099 by @tiangolo.
  • ♻️ Refactor and simplify dependencies data structures with dataclasses. PR #12098 by @tiangolo.

Docs

  • 📝 Add External Link: Techniques and applications of SQLAlchemy global filters in FastAPI. PR #12109 by @TheShubhendra.
  • 📝 Add note about time.perf_counter() in middlewares. PR #12095 by @tiangolo.
  • 📝 Tweak middleware code sample time.time() to time.perf_counter(). PR #11957 by @domdent.
  • 🔧 Update sponsors: Coherence. PR #12093 by @tiangolo.
  • 📝 Fix async test example not to trigger DeprecationWarning. PR #12084 by @marcinsulikowski.
  • 📝 Update docs_src/path_params_numeric_validations/tutorial006.py. PR #11478 by @MuhammadAshiqAmeer.
  • 📝 Update comma in docs/en/docs/async.md. PR #12062 by @Alec-Gillis.
  • 📝 Update docs about serving FastAPI: ASGI servers, Docker containers, etc.. PR #12069 by @tiangolo.
  • 📝 Clarify response_class parameter, validations, and returning a response directly. PR #12067 by @tiangolo.
  • 📝 Fix minor typos and issues in the documentation. PR #12063 by @svlandeg.
  • 📝 Add note in Docker docs about ensuring graceful shutdowns and lifespan events with CMD exec form. PR #11960 by @GPla.

Translations

  • 🌐 Add Dutch translation for docs/nl/docs/features.md. PR #12101 by @maxscheijen.
  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/testing-events.md. PR #12108 by @ceb10n.
  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/security/index.md. PR #12114 by @ceb10n.
  • 🌐 Add Dutch translation for docs/nl/docs/index.md. PR #12042 by @svlandeg.
  • 🌐 Update Chinese translation for docs/zh/docs/how-to/index.md. PR #12070 by @synthpop123.

Internal

0.112.2

24 Aug 19:37
Compare
Choose a tag to compare

Fixes

  • 🐛 Fix allow_inf_nan option for Param and Body classes. PR #11867 by @giunio-prc.
  • 🐛 Ensure that app.include_router merges nested lifespans. PR #9630 by @Lancetnik.

Refactors

Docs

  • 📝 Fix a typo in docs/en/docs/virtual-environments.md. PR #12064 by @aymenkrifa.
  • 📝 Add docs about Environment Variables and Virtual Environments. PR #12054 by @tiangolo.
  • 📝 Add Asyncer mention in async docs. PR #12037 by @tiangolo.
  • 📝 Move the Features docs to the top level to improve the main page menu. PR #12036 by @tiangolo.
  • ✏️ Fix import typo in reference example for Security. PR #11168 by @0shah0.
  • 📝 Highlight correct line in tutorial docs/en/docs/tutorial/body-multiple-params.md. PR #11978 by @svlandeg.
  • 🔥 Remove Sentry link from Advanced Middleware docs. PR #12031 by @alejsdev.
  • 📝 Clarify management tasks for translations, multiples files in one PR. PR #12030 by @tiangolo.
  • 📝 Edit the link to the OpenAPI "Responses Object" and "Response Object" sections in the "Additional Responses in OpenAPI" section. PR #11996 by @VaitoSoi.
  • 🔨 Specify email-validator dependency with dash. PR #11515 by @jirikuncar.
  • 🌐 Add Spanish translation for docs/es/docs/project-generation.md. PR #11947 by @alejsdev.
  • 📝 Fix minor typo. PR #12026 by @MicaelJarniac.
  • 📝 Several docs improvements, tweaks, and clarifications. PR #11390 by @nilslindemann.
  • 📝 Add missing compresslevel parameter on docs for GZipMiddleware. PR #11350 by @junah201.
  • 📝 Fix inconsistent response code when item already exists in docs for testing. PR #11818 by @lokomilo.
  • 📝 Update docs/en/docs/tutorial/body.md with Python 3.10 union type example. PR #11415 by @rangzen.

Translations

  • 🌐 Add Portuguese translation for docs/pt/docs/tutorial/request_file.md. PR #12018 by @Joao-Pedro-P-Holanda.
  • 🌐 Add Japanese translation for docs/ja/docs/learn/index.md. PR #11592 by @ukwhatn.
  • 📝 Update Spanish translation docs for consistency. PR #12044 by @alejsdev.
  • 🌐 Update Chinese translation for docs/zh/docs/tutorial/dependencies/dependencies-with-yield.md. PR #12028 by @xuvjso.
  • 📝 Update FastAPI People, do not translate to have the most recent info. PR #12034 by @tiangolo.
  • 🌐 Update Urdu translation for docs/ur/docs/benchmarks.md. PR #10046 by @AhsanSheraz.

Internal

0.112.1

15 Aug 21:09
Compare
Choose a tag to compare

Upgrades

Docs

  • 📝 Update docs section about "Don't Translate these Pages". PR #12022 by @tiangolo.
  • 📝 Add documentation for non-translated pages and scripts to verify them. PR #12020 by @tiangolo.
  • 📝 Update docs about discussions questions. PR #11985 by @tiangolo.

Translations

  • 🌐 Add Portuguese translation for docs/pt/docs/tutorial/bigger-applications.md. PR #11971 by @marcelomarkus.
  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/testing-websockets.md. PR #11994 by @ceb10n.
  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/testing-dependencies.md. PR #11995 by @ceb10n.
  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/using-request-directly.md. PR #11956 by @ceb10n.
  • 🌐 Add French translation for docs/fr/docs/tutorial/body-multiple-params.md. PR #11796 by @pe-brian.
  • 🌐 Update Chinese translation for docs/zh/docs/tutorial/query-params.md. PR #11557 by @caomingpei.
  • 🌐 Update typo in Chinese translation for docs/zh/docs/advanced/testing-dependencies.md. PR #11944 by @bestony.
  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/sub-applications.md and docs/pt/docs/advanced/behind-a-proxy.md. PR #11856 by @marcelomarkus.
  • 🌐 Add Portuguese translation for docs/pt/docs/tutorial/cors.md and docs/pt/docs/tutorial/middleware.md. PR #11916 by @wesinalves.
  • 🌐 Add French translation for docs/fr/docs/tutorial/path-params-numeric-validations.md. PR #11788 by @pe-brian.

Internal

0.112.0

02 Aug 06:13
Compare
Choose a tag to compare

Breaking Changes

  • ♻️ Add support for pip install "fastapi[standard]" with standard dependencies and python -m fastapi. PR #11935 by @tiangolo.

Summary

Install with:

pip install "fastapi[standard]"

Other Changes

  • This adds support for calling the CLI as:
python -m fastapi
  • And it upgrades fastapi-cli[standard] >=0.0.5.

Technical Details

Before this, fastapi would include the standard dependencies, with Uvicorn and the fastapi-cli, etc.

And fastapi-slim would not include those standard dependencies.

Now fastapi doesn't include those standard dependencies unless you install with pip install "fastapi[standard]".

Before, you would install pip install fastapi, now you should include the standard optional dependencies (unless you want to exclude one of those): pip install "fastapi[standard]".

This change is because having the standard optional dependencies installed by default was being inconvenient to several users, and having to install instead fastapi-slim was not being a feasible solution.

Discussed here: #11522 and here: #11525

Docs

Translations

  • 🌐 Update Portuguese translation for docs/pt/docs/alternatives.md. PR #11931 by @ceb10n.
  • 🌐 Add Russian translation for docs/ru/docs/tutorial/dependencies/sub-dependencies.md. PR #10515 by @AlertRED.
  • 🌐 Add Portuguese translation for docs/pt/docs/advanced/response-change-status-code.md. PR #11863 by @ceb10n.
  • 🌐 Add Portuguese translation for docs/pt/docs/reference/background.md. PR #11849 by @lucasbalieiro.
  • 🌐 Add Portuguese translation for docs/pt/docs/tutorial/dependencies/dependencies-with-yield.md. PR #11848 by @Joao-Pedro-P-Holanda.
  • 🌐 Add Portuguese translation for docs/pt/docs/reference/apirouter.md. PR #11843 by @lucasbalieiro.

Internal

  • 🔧 Update sponsors: add liblab. PR #11934 by @tiangolo.
  • 👷 Update GitHub Action label-approved permissions. PR #11933 by @tiangolo.
  • 👷 Refactor GitHub Action to comment docs deployment URLs and update token. PR #11925 by @tiangolo.
  • 👷 Update tokens for GitHub Actions. PR #11924 by @tiangolo.
  • 👷 Update token permissions to comment deployment URL in docs. PR #11917 by @tiangolo.
  • 👷 Update token permissions for GitHub Actions. PR #11915 by @tiangolo.
  • 👷 Update GitHub Actions token usage. PR #11914 by @tiangolo.
  • 👷 Update GitHub Action to notify translations with label approved-1. PR #11907 by @tiangolo.
  • 🔧 Update sponsors, remove Reflex. PR #11875 by @tiangolo.
  • 🔧 Update sponsors: remove TalkPython. PR #11861 by @tiangolo.
  • 🔨 Update docs Termynal scripts to not include line nums for local dev. PR #11854 by @tiangolo.