FastAPI
Routing, dependencies, validation and running it in production.
17 articles
FastAPI 422 Unprocessable Entity: Read the Error Body Instead of Guessing
FastAPI already tells you exactly which field is wrong in the 422 response body. Stop relaxing validation. Read detail[].loc, and fix the three payload mistakes that cause almost every 422.
Pydantic v1 to v2 Migration Gotchas the Official Guide Leaves Out
The Pydantic v2 migration guide misses two real traps: a moved ModelMetaclass import and a silent datetime coercion change that breaks tests without raising. Here is both.
Pydantic mypy plugin: the false 'Unexpected keyword argument' on forbid-extra models
The pydantic mypy plugin can report Unexpected keyword argument on correct code that mixes type aliases with extra='forbid'. It is a known plugin limitation, not your bug. Here is the scoped fix.
pytest-asyncio 'event loop is already running': there is no patch coming
request.getfixturevalue() on an async fixture crashes pytest-asyncio, nest_asyncio no longer saves you, and upstream has no fix. Here is the test architecture that works instead.
Pydantic v2 'MockValSer object cannot be converted to SchemaSerializer': The Real Cause
This cryptic Pydantic v2 TypeError means your model schema never finished building, usually an unresolved forward reference. Here is the fix, not a workaround.
QueuePool Limit Reached Under FastAPI Load: pool_size Is Not the Fix
The SQLAlchemy QueuePool timeout under FastAPI load is about how long connections are held, not how many exist. Why raising pool_size fails, and the async session scoping that works.
Uvicorn's 'uvicorn.error' Logger Is Not About Errors: Stop the False Alarms
Uvicorn tags routine INFO logs with the logger name uvicorn.error, tripping monitors that pattern-match on 'error'. Here is why, and how to alert on severity instead.
Error loading ASGI app: why uvicorn cannot import your FastAPI module
Uvicorn resolves 'module:app' against sys.path, not your file's location. The real cause, the right command for flat, package, src and Docker layouts, and how to verify it.
FastAPI Cross-Site Cookies Silently Not Set: SameSite and Secure
Your FastAPI set_cookie returns 200 but the browser never stores it and there is no error. Here is why cross-site cookies need SameSite=None; Secure, and how to fix it.
FastAPI CORS Silently Failing: the allow_credentials Plus Wildcard Trap
FastAPI CORS with allow_credentials=True and allow_origins=['*'] passes testing then breaks once cookies flow. The browser blocks it silently. Here is why and the fix.
Turn FastAPI Validation Errors Into Clean Per-Field Messages
FastAPI's default 422 body is a nested Pydantic list no frontend can use. Here is a RequestValidationError handler that flattens it to clean per-field messages without breaking on upgrade.
FastAPI Memory Leaks in Production: A Root-Cause Checklist
FastAPI workers whose memory climbs to OOM usually are not leaking, they are blocking the event loop or queuing unbounded work. A checklist to find the real cause before blaming the framework.
Why Your FastAPI Logs Vanish Under Gunicorn and Uvicorn (and the Config That Works Everywhere)
FastAPI logs work in local dev but disappear in Docker under gunicorn+uvicorn, especially below WARNING. The cause is disable_existing_loggers and uvicorn's own loggers. Here is the fix.
FastAPI yield Dependencies: How Cleanup Works and Where It Silently Breaks
FastAPI 0.106 moved yield-dependency teardown to run before the response, silently breaking background tasks and middleware commits. Here is the fix that survives upgrades.
ModuleNotFoundError: No module named 'fastapi' - the Three Unrelated Causes
One error message, three completely different causes: wrong virtualenv, package not installed, or a stale editable install left behind by switching between pip, poetry and uv.
'No module named pydantic_core._pydantic_core' in Docker or Lambda Is an Architecture Mismatch
This error is not a missing package. pydantic-core ships a compiled Rust wheel, and installing it for the wrong CPU or glibc means the files exist but cannot load.
Why Your FastAPI 'async' Endpoints Run in Serial: The Blocking Event Loop Trap
FastAPI processing requests one at a time? async def does not make blocking code async. Here is why the event loop serialises, and the def vs async fix.