FastAPI
High-performance Python web framework for building APIs, based on Starlette and Pydantic. Current version is 0.135.x (Mar 2026). Requires Python >=3.10 and Pydantic >=2.7.0. Pydantic v1 support fully removed.
Warnings
- breaking Pydantic v1 support fully removed in 0.128.0. from pydantic.v1 import BaseModel no longer works with FastAPI — raises FastAPIError at route decoration time. Minimum Pydantic is now >=2.7.0.
- breaking Python 3.8 and 3.9 support dropped. fastapi>=0.125.0 requires Python >=3.10.
- breaking Strict Content-Type checking added. FastAPI now rejects JSON requests missing a valid Content-Type: application/json header by default.
- breaking bare pip install fastapi does NOT include uvicorn or python-multipart. LLM-generated setups frequently omit these, causing ImportError on first run or silent failure on form/file endpoints.
- deprecated @app.on_event('startup') and @app.on_event('shutdown') deprecated since 0.93. Still functional but emits FastAPIDeprecationWarning. Will be removed in a future version.
- gotcha python-multipart is required for Form() and File() parameters but is not installed by bare fastapi. The error message when it's missing is: 'Form data requires python-multipart to be installed'.
- gotcha response_model_exclude_unset, response_model_exclude_none etc. are route decorator params, not model config. LLMs frequently place them in the Pydantic model instead.
Install
-
pip install "fastapi[standard]" -
pip install fastapi -
pip install "fastapi[all]"
Imports
- FastAPI
from fastapi import FastAPI from pydantic import BaseModel
- lifespan
from contextlib import asynccontextmanager from fastapi import FastAPI @asynccontextmanager async def lifespan(app: FastAPI): # startup yield # shutdown app = FastAPI(lifespan=lifespan)
Quickstart
from contextlib import asynccontextmanager
from fastapi import FastAPI
from pydantic import BaseModel
@asynccontextmanager
async def lifespan(app: FastAPI):
# startup logic here
yield
# shutdown logic here
app = FastAPI(lifespan=lifespan)
class Item(BaseModel):
name: str
price: float
@app.get('/')
def read_root():
return {'status': 'ok'}
@app.post('/items')
def create_item(item: Item) -> Item:
return item
# Run: fastapi dev main.py (dev)
# Run: fastapi run main.py (prod)