Streaming LLM answers from FastAPI with SSE: sources first, then tokens
An SSE endpoint sent sources after 52 ms instead of a 1,664 ms JSON wait. One blocking time.sleep in the generator pushed first byte to 3,280 ms with 5 clients.
Build an MCP server in Python for your docs: stdio and HTTP, with measured payload sizes
A working MCP docs server (mcp 2.3.0, 2 tools) over stdio and HTTP: a k=5 search returns 1,818 bytes of text, but the full result is 2.19x larger.
FastAPI headers: read, validate and set them (and the underscore trap)
Read request headers in FastAPI with Header(), handle repeated headers, group them in a Pydantic model, require API keys and set response headers. All tested.
FastAPI: API design & OpenAPI
Code can be refactored in an afternoon. A public API cannot: other teams, mobile apps already in users' pockets, and partners' scripts depend on every field name…
FastAPI: async def vs def: the concurrency model
FastAPI runs async def endpoints on the event loop , one thread juggling every request, and plain def endpoints in a thread pool . Both can be fast.
FastAPI: Authentication: passwords, JWTs & API keys
Authentication answers 'who is this?'; authorization (part 18) answers 'may they do this?'.
FastAPI: Background work & task queues
Some work should not make the client wait: sending an email, calling a webhook, generating a report.
FastAPI: Request bodies & Pydantic v2
A request body is declared as a Pydantic model . FastAPI reads the JSON, Pydantic validates it against the model, and your function receives a typed object, or the…
FastAPI: Pagination, concurrency & migrations
Four patterns show up in every serious API. Cursor pagination stays fast and consistent on huge tables. Safe filtering and sorting never puts user input into SQL.
FastAPI: Dependency injection in depth
A dependency is a function (or any callable) that FastAPI runs before your endpoint and whose result it passes in: the current user, a database session, pagination…
FastAPI: Deployment & graceful shutdown
A deployment replaces running processes that are in the middle of serving customers.
FastAPI: Errors & a consistent error contract
Out of the box, FastAPI answers failures in three different shapes : {'detail': '...'} for HTTPException , {'detail': [...]} for validation errors, and a plain-text…
FastAPI: Lifespan, state & shared resources
Some things should exist exactly once per process: the database engine and its connection pool, an HTTP client with keep-alive connections, a loaded ML model, a…
FastAPI: Middleware
Middleware wraps the whole application: it sees every request on the way in and every response on the way out, including 404s and errors.
FastAPI: Logging, metrics & tracing
At 2 a.m. the alert says 'orders API error rate 8%'. Can you tell which endpoint, since when, for which customers, and why? That depends entirely on what you built…
FastAPI: Path, query & header parameters
Every value in a URL or header arrives as text . FastAPI decides where each function parameter comes from and what type it should be from the signature: a name that…
FastAPI: Performance & scaling
'FastAPI is fast' is true of the framework overhead, which is rarely your bottleneck.
FastAPI: The production pitfalls checklist
Experience is mostly a list of mistakes you no longer make. This part collects thirty that FastAPI teams have paid for in production, grouped by area, each with…
FastAPI: Project: a production-grade orders API
Every part of this guide, in one service a senior team would be comfortable putting in front of customers.
FastAPI: Responses & serialization
What your endpoint returns and what the client receives are not the same thing.
FastAPI: Authorization & API security
Most API breaches are not clever cryptography attacks. Someone changes /orders/1042 to /orders/1043 and gets another customer's order.
FastAPI: Configuration with pydantic-settings
The same code runs on a laptop, in CI, in staging and in production. Only configuration differs: database URLs, secrets, feature flags, allowed origins.
FastAPI: Setup, tooling & the dev loop
A productive FastAPI setup is small: a virtual environment, fastapi with a server, auto-reload while you code, the interactive docs at /docs , and a test client so…
FastAPI: Databases with async SQLAlchemy 2.0
Most FastAPI services are a thin layer over a database, and most of their production incidents come from how that database is used: sessions shared between…
FastAPI: Streaming, SSE, WebSockets & files
Not every response is a small JSON document produced all at once. Exports have millions of rows, LLM answers arrive token by token, dashboards want live updates…
FastAPI: Routers, layers & project structure
Tutorials put everything in main.py . Real services outgrow that within weeks.
FastAPI: Testing FastAPI apps
FastAPI is unusually easy to test: the app is a callable, clients run it in-process, and dependency overrides replace any collaborator.
FastAPI: What FastAPI really is
FastAPI is a thin, clever layer on top of two other libraries. Starlette provides the web toolkit: routing, requests, responses, middleware, WebSockets.
Stop fake citations in RAG: validate them server-side in 25 lines of Python
LLMs will cite passage [7] when you gave them 5. A tested validator that strips invented citations and returns a grounded flag your UI can trust.