FastAPI Lifespan vs on_event: A Practical Decision Guide
Compare FastAPI's lifespan context manager with legacy on_event hooks. Learn how to safely initialize DB pools and shared resources using app.state for better testability and ASGI compliance.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Compare FastAPI's lifespan context manager with legacy on_event hooks. Learn how to safely initialize DB pools and shared resources using app.state for better testability and ASGI compliance.
Learn how to diagnose and fix 422 Unprocessable Entity errors in FastAPI by analyzing Pydantic validation failures, request headers, and schema mismatches.
Stop leaking database connections in FastAPI. Learn how to use 'yield' dependencies to automate session setup and teardown for every request.
In a local development environment, a FastAPI dependency can be overridden using app.dependency_overrides[SomeDependency] = lambda: mock_service to inject a test double. When the same application is built as a Docker image and run in production, the override appears to be ignored and the real dependency is used instead. The service is launched with Uvicorn v
The goal is to verify that FastAPI’s dependency injection mechanism can safely replace a broken endpoint with a fallback implementation when an upgrade introduces breaking changes, while middleware error handlers keep the service running. This recovery strategy relies on DI to swap implementations at runtime and on middleware to catch exceptions and return g
I'm trying to enable CORS in this very basic FastAPI example, however it doesn't seem to be working. from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=['*'] ) @app.get('/') def read_main(): return {'message': 'Hello World!'} This is the response I get: curl -v http
When protecting an endpoint with FastAPI's OAuth2PasswordBearer dependency, developers often rely on the token's scope claim to enforce least‑privilege access while also expecting the library to reject expired credentials via the exp claim. The exact point at which FastAPI validates scopes relative to expiration is not documented, leaving uncertainty about w
The goal is to clarify where request validation cost sits in FastAPI and how validation error responses can be controlled without changing the default 422 Unprocessable Entity behavior. Validation is performed by the request parsing layer before the path operation function is invoked. Errors are raised by the framework rather than user code and are not visib
FastAPI parses incoming request bodies synchronously through Pydantic’s BaseModel and Python’s standard json module. Under high concurrency, this synchronous step can block the event loop and inflate response latency for large payloads. One documented integration point is FastAPI’s json_loads hook, which allows substituting an alternative parser such as orjs