Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

38.5 KB
Raw Blame

Playbook: migrating a production Flask/WSGI app to FastAPI/ASGI

This is a reference for teams starting a Flask (or other WSGI framework) to FastAPI (or other ASGI framework) migration on an application that is already serving production traffic. It is deliberately generic — written to be useful before you start, not as a record of one migration after the fact.

This playbook was distilled from the work and review around PR #3299; it was not a checklist followed prospectively by that migration. The durable test and source accounting is in ADR-0010 and ADR-0011. This document is the reusable sequence, plus the traps and idioms a team needs before starting, independent of any one branch's outcome. The "what this repo learned" section near the end draws on the same migration, but the rest of this page is not about this repository.

When not to do this migration

Say this part out loud before anyone opens a PR, because the migration has a real cost and "FastAPI is more modern" is not, by itself, enough to clear it.

  • You don't have async I/O to exploit. FastAPI's throughput advantage comes from not blocking a worker thread on network waits. If your app's bottleneck is CPU-bound work, a slow ORM query with no concurrent competition, or a low-traffic internal tool, you will pay the full migration cost for a performance gain nobody will notice. Benchmarks showing FastAPI at 5-10x Flask's requests/second are benchmarking concurrent I/O-bound workloads specifically; they do not transfer to every app.
  • Your test suite is thin or you can't tell if it's green. Everything below assumes you can answer "did this change behavior" empirically. If you can't, the migration is not "FastAPI vs Flask", it's "rewrite a production app's behavior from reading old code", which is a much riskier project wearing a framework-migration costume. Fix the test gap first, or accept that you are taking on that additional risk with eyes open.
  • The team doesn't know asyncio. async def handlers that call blocking code are worse than the def handlers you started with — a single blocking call on the event loop stalls every concurrent request, not just the slow one. If nobody on the team can explain why that's true, budget time to learn it before you budget time to migrate, or migrate everything to plain def routes and skip async entirely at first (FastAPI still runs those in a thread pool, correctly, with none of the footgun).
  • You depend heavily on a Flask extension with no ASGI story. Check your extensions before you check anything else. Flask-Login, Flask-WTF, Flask-Limiter, Flask-SQLAlchemy and Flask-SocketIO each need a distinct ASGI-native replacement (see the idiom table below), and some replacements have a smaller maintenance and compatibility surface than the Flask extension they replace. If your extension list is long and obscure, prototype the riskiest one first, in isolation, before committing the whole team.
  • You need this app to keep running on a WSGI-only host. Some managed PaaS and some corporate deployment pipelines assume a WSGI callable and a wsgi.py entrypoint. Confirm your deployment target can run an ASGI server (uvicorn, Hypercorn, Daphne) before you invest in the migration itself.
  • The honest alternative is often "don't." Flask supports async def views, but under WSGI each request still occupies one worker while Flask runs the view's event loop. That can still be a smaller, reversible change than a full framework swap when limited concurrent I/O inside one request is all you need. Migrate when you specifically want ASGI-native middleware, first-class WebSockets, Pydantic-validated request/response models, or a materially higher ceiling under concurrent I/O load — not merely to add the async keyword. See Flask's async documentation.

If none of these apply, the rest of this document is for you.

The playbook

This is deliberately close to the sequence practiced at Forethought — the most detailed public write-up of a real Flask→FastAPI migration found in researching this document — and at Open Library, whose migration is not from Flask (its backend is Infogami, built on web.py) but whose cutover mechanics are documented in unusual detail, plus what Starlette's and FastAPI's own docs specify for the mechanical steps. Treat it as ordered: each step assumes the ones before it are done, and skipping one tends to resurface as a debugging session three steps later rather than as an obviously missing prerequisite.

0. Put a safety net under yourself before touching code

Two things, in this order, before the first migration commit:

  1. Production error tracking (Sentry, Rollbar, an OTel error pipeline — anything that tells you about a new exception class within minutes, not when a user files a ticket). The entire value of everything that follows is that regressions become visible. Without this, a cutover that breaks one rarely-hit code path is discovered by a customer, weeks later, and by then nobody remembers which of forty commits touched it.
  2. A captured, dated baseline of test-suite health: pass/fail count, coverage, and — if you have it — a list of known-flaky tests, saved somewhere durable (not just "CI was green that day," which nobody can later verify). Without this you cannot answer "did the migration lose test coverage" except by reconstruction after the fact, which is strictly harder and less trustworthy than having captured it up front.

1. Do a formatting-only pre-pass, as its own commit, before any logic changes

Run your formatter and import-sorter across the codebase in a commit that touches nothing else. The reason isn't cosmetic: a migration diff is already large and hard to review; burying real changes inside formatter churn makes reviewers approve things they didn't actually read. Forethought's write-up specifically calls out autoflake as a hazard here — it strips imports that exist only to trigger Flask blueprint registration as side effects, and their fix was to guard each one with an assert module_name line so the "unused import" tooling leaves it alone. If your repo already enforces a formatter in CI (ruff-format, black) before you start, this step is close to a no-op — confirm that rather than skipping it on assumption.

2. Choose your sequencing: big-bang or strangler-fig, deliberately

Two shapes, and the deciding factor is usually operational, not technical:

  • Big-bang: one branch replaces the whole stack; one PR is the cutover. Higher risk per unit time, shorter total exposure window, no dual-stack maintenance burden while it's in flight. Right for smaller apps, apps with strong test coverage, and — as below — apps whose isolation model makes running both stacks in the same process actively dangerous.
  • Strangler-fig: routes migrate incrementally, with the two stacks live side by side, until nothing points at the old one. Lower risk per step, much longer exposure window, and it forces you to keep the old stack's dependencies alive and working for the whole migration — which, for a framework migration specifically, may include keeping two different versions of your ORM's session-management pattern correct simultaneously.

If you choose strangler-fig, prefer putting the two stacks behind a proxy instead of inside the same process. FastAPI's WSGIMiddleware can mount a WSGI application as a sub-application, but that couples two frameworks' context, extension, dependency, and lifecycle assumptions in one process. fastapi#6749 records a real request-context failure in such a deployment; it is evidence to test this boundary, not proof that every modern Flask application will fail identically. Separate processes keep those assumptions isolated and make rollback a routing change: point a path prefix (or parallel port) at the new app while the old app continues serving the rest. If in-process mounting is still attractive, test every context-dependent extension under concurrent traffic before relying on it as the migration seam.

3. If you're strangling, stand up the routing seam before migrating a single route

The proxy (or, within the app, a single dispatcher) needs to exist and be provably correct — tested against both a hit and a miss — before route one moves. Migrating a route into a routing layer that doesn't reliably reach it just moves the bug from "wrong framework" to "wrong reverse-proxy rule," which is harder to spot because it looks like the migration succeeded.

4. Migrate the cross-cutting seams before any individual route

Auth, sessions, CSRF, rate limiting, error handling, CORS, and request-scoped state each have exactly one Flask extension or pattern behind them today, and they should have exactly one ASGI-native replacement each — installed once, at the app level — before the first route is ported. If you instead let each route reimplement its own auth check or its own error shape as you migrate it, you'll have as many auth implementations as migrated routes by the time you're a third of the way through, and unifying them afterward is strictly more work than getting the seam right once, first. See the idiom table below for the specific replacements.

One seam deserves a specific warning: if your Flask app uses url_for in templates or redirects, and your FastAPI migration doesn't use Starlette's own named-route reversal (request.url_for(name)) end to end, a hand-maintained name→path mapping will drift from the actual route table the moment someone renames or moves a route. A hand-rolled map's failure mode is worse than a broken link: a fallback guess (e.g. naively converting a dot name to a path) can produce a URL that resolves to the wrong live route rather than erroring, which a "does this link 404" test will not catch. Prefer the framework's own reversal mechanism over a parallel lookup table; if you must keep one, test that every entry matches a currently mounted route on every CI run, not just that it doesn't 404.

5. Decide def vs async def per route, deliberately, as you port it

This is, by a wide margin, the most frequently cited cause of post-migration production incidents in every migration write-up researched for this document, and the rule is simple to state and easy to violate by accident: an async def route that calls anything blocking — a synchronous DB driver, requests, time.sleep, CPU-bound work — blocks the entire event loop, stalling every other concurrent request on that worker, not just the slow one. A def route, by contrast, is automatically run in Starlette's thread pool, exactly like a WSGI handler was, and is always safe by this measure even though it can't await.

The practical rule: default new or ported routes to plain def unless the route body is genuinely await-only I/O (an async DB driver, an async HTTP client, asyncio.sleep). If a route needs both — some async I/O and one blocking call — offload the blocking part explicitly (await run_in_threadpool(blocking_fn) or your own equivalent), rather than calling it inline. Write an automated check for this before you need it: an AST sweep over your route modules that flags known-synchronous calls (requests., time.sleep, a sync DB session's .execute) appearing inside an async def body is cheap to write and catches the class of bug that a human code reviewer reliably misses, because the code runs fine locally under low concurrency and only misbehaves under load.

6. Write parity or contract tests before cutover — this is the step teams skip and regret

Published practice — and every real migration write-up found for this document agrees on this specifically — is to compare old-endpoint output against new-endpoint output on real and adversarial inputs before routing production traffic to the new code, not after. Concretely, this looks like one of:

  • A parity test harness: for each migrated route, a test that calls both the old and new implementations (same input, same auth state) and asserts the response bodies, status codes, and headers match, run either in CI against both apps running simultaneously, or manually during the migration window and then deleted. Open Library's runbook does exactly this — the old app on one port, the new one on another, comparison scripts written per-endpoint and deleted before merge.
  • Shadow traffic: mirror real production requests to both stacks, log divergences, without the shadow response ever reaching a user. Higher setup cost, but it tests against the actual input distribution instead of whatever edge cases a human thought to write down — which matters most for endpoints with organically complex input history nobody remembers documenting.
  • A written contract, if you have neither: at minimum, before migrating a route, write down its actual current behavior — status codes on bad input, exact response shape, what it does with a malformed auth header — as an artifact separate from "what the old code happens to do," because a contract you can name is a contract you can test the new code against. Legacy Flask behavior is often fuzzier than anyone remembers; writing it down is itself where a migration finds most of its surprises.

Watch for the trap of writing tests that look like parity tests but never actually execute the old framework or compare against it — a self-consistency test suite that only checks the new app against itself will pass even when the new app's behavior has silently diverged from the old one on every axis that matters. (The "traps that pass CI" section below has a live example of exactly this shape.) The signal to check for, on any test file that claims to be a migration safety net, is a literal invocation of the old stack, or an explicitly captured pre-migration snapshot it diffs against — not a plausible-sounding name.

7. Configure the ASGI server for production, not for --reload

A handful of settings matter specifically because ASGI servers default to values chosen for developer convenience, not production safety:

  • Use lifespan handlers for startup/shutdown, not the deprecated @app.on_event.
  • Set server_header=False — don't advertise the exact server version.
  • Set an explicit timeout_keep_alive and a timeout_graceful_shutdown (see §8, "Stage the cutover", for the trade-off the latter encodes).
  • Gate proxy_headers / forwarded_allow_ips behind an explicit flag, never trust X-Forwarded-* unconditionally — an ASGI server that trusts proxy headers by default from any peer is a client-IP-spoofing vector the moment it's reachable directly, and this is a common regression versus a WSGI setup where the proxy-trust logic often lived in application code that got deleted along with everything else Flask.
  • If you use BackgroundTasks, do not rely on them surviving a deploy: tasks still running when the process goes down are simply lost, with no warning and no exception. There is also a separate, easy-to-hit footgun where injected BackgroundTasks are silently discarded if the endpoint returns a Response that already has its own background set (fastapi#15111). For anything that must survive a deploy, use a real background worker/queue, not BackgroundTasks.

8. Stage the cutover — canary, flag, or blue/green, not "merge is the cutover"

Have a way to route a slice of traffic to the new stack and watch it before committing all of it: a feature flag toggling which app handles a request, a canary deployment at the infrastructure level, or blue/green with fast rollback. This matters even (especially) for big-bang migrations, where "the merge is the cutover" is the default if nobody builds anything else. Whatever mechanism you pick, write down the rollback procedure before you need it — "redeploy the previous image" is a valid rollback plan, but only if someone has confirmed the previous image is still buildable and its data migrations (if any) are reversible, and that confirmation belongs in the plan, not assumed at 2am.

9. Decommission the old stack only after a burn-in period — and know what you're trading away if you don't

The safer default is: keep the old routes (or the whole old app) reachable and deployable for some period after cutover, so a regression found in production can be fixed by routing back rather than by reverting a rewrite. Deleting the old stack in the same change that adds the new one is a valid choice for a small, self-hosted, single-tenant app where the operator controls upgrade timing — but it is a choice with a cost, and the cost is specific: no live in-process or routing fallback. Rollback then means redeploying a previously verified image, as described in step 8. A long-lived migration branch also carries the separate burden of reconciling both frameworks across every merge from trunk until cutover. If you don't have that branch-lifetime problem, burn-in is close to free insurance; take it.

10. Do a post-launch hardening pass, deliberately, not by waiting for incidents

A short, specific checklist, because these are the failure modes that don't show up in development traffic and specifically bite ASGI apps:

  • SSE / long-lived streaming routes need Cache-Control: no-transform and X-Accel-Buffering: no so an intermediate proxy doesn't buffer them — and confirm no BaseHTTPMiddleware sits in front of them either (see the traps section; the proxy headers don't help against server-side buffering).
  • Confirm every startup/shutdown hook migrated to lifespan, not @app.on_event (deprecated, and easy to leave behind because it still works).
  • Re-run whatever end-to-end / browser-driven UI suite you have specifically against the new stack — not just the unit and integration layers — since that's usually the only mechanism left that can catch a behavioral divergence if step 6's parity testing was incomplete.

Flask idiom → ASGI replacement

Flask/WSGI idiom ASGI / FastAPI replacement Notes
Blueprint + @app.route APIRouter + include_router One router per feature area, same as blueprints
@app.before_request / @app.after_request Depends() on the route, or ASGI middleware A dependency is per-route and explicit; middleware is global and implicit — prefer dependencies unless the logic truly applies to every route
flask.g contextvars.ContextVar, or request.state g is scoped to Flask's application context and is not an ASGI request-state API. request.state is simplest when you already have request in scope; a ContextVar is useful when you do not (e.g. inside a logger)
flask.session Starlette SessionMiddleware Set same_site and https_only explicitly — Flask's defaults and Starlette's are not the same
@login_required (Flask-Login) Depends(require_auth) Auth becomes an explicit dependency parameter on every protected route, not an implicit decorator side effect
Flask-WTF CSRF A CSRF Depends() or ASGI middleware (no first-party FastAPI equivalent) Check this early — it's one of the extensions with no canonical replacement, expect to write or vendor one
Flask-Limiter slowapi See the traps section — the default middleware buffers streaming routes
Flask-SocketIO python-socketio in ASGI mode (socketio.ASGIApp), mounted alongside FastAPI Handlers become module-level async def, not methods on a singleton; room/session tracking usually needs its own dict rather than relying on Socket.IO's built-in rooms if you need to reason about identity outside a handler
@app.errorhandler Exception handlers via add_exception_handler / @app.exception_handler Register once, centrally, same as the seam-first advice above
jsonify(...) Return a Pydantic model / dict, or JSONResponse Response validation is now automatic (and will 500 on a mismatched shape) — write the Pydantic model deliberately, it changes what "valid response" means
abort(404, ...) raise HTTPException(status_code=404, ...) Exceptions replace the early-return-by-abort pattern
request.form / request.files Pydantic form/body models, UploadFile Validation moves from manual if checks to the type declaration
Werkzeug dev server / gunicorn sync workers uvicorn (optionally behind gunicorn as a process manager) See §8, "Stage the cutover", for worker-count guidance
Flask test client (app.test_client()) httpx.ASGITransport / FastAPI TestClient Both are synchronous-looking wrappers; note TestClient runs your app in a thread, which can mask async-specific bugs that only appear under real concurrency
threading.local() for request-scoped anything contextvars.ContextVar Not a drop-in swap — see the per-request-state section below, propagation across a thread-pool boundary is one-directional
Flask-SQLAlchemy scoped_session (thread-scoped) An async session per request via Depends with yield See the per-request-DB-session section below
send_from_directory, streaming with Response(generate()) StreamingResponse / FileResponse Confirm nothing outside the route sits between the response and the client that buffers it (see traps section)

Special cases this app's shape makes worth calling out separately

WebSockets / Socket.IO

Flask-SocketIO supports several runtimes, including eventlet/gevent and a standard threading mode; this repository used async_mode="threading" before the migration. python-socketio's ASGI mode (socketio.ASGIApp) instead runs as a native ASGI sub-application, mountable alongside FastAPI and scheduled by the event loop. The identity model changes shape along with the runtime, though, and it is worth planning for rather than discovering: Flask-SocketIO code is commonly written as methods on a singleton service object, dispatching on self; python-socketio's AsyncServer dispatches to module-level async def event handlers instead, registered by event name. If your existing code tracked "which user is this socket" or "which room is this session in" via instance state on that singleton, decide up front where that state now lives — a plain dict keyed by session id, scoped at module level, is the usual answer, and it needs the same concurrency discipline (no assumption that only one coroutine touches it at a time) that any other shared mutable state under ASGI needs.

Background workers / queue processors

If a background worker currently gets nudged by a Flask before_request hook (checking a queue on every authenticated request, say), that hook has no direct ASGI equivalent — there's no dependency that fires on literally every request the way before_request does across an entire blueprint tree by registration. Two honest options: replicate it explicitly as a dependency on the routes that should trigger it, or fold the work into the background worker's own loop (an interruptible wait rather than a per-request nudge) and rely on a different, less frequent trigger — login, a scheduled interval — to catch anything the loop's own cadence missed. The second is usually the better fit for ASGI, since it removes a per-request cost, but check that whatever you pick as the "catch-up" trigger actually fires often enough for anything that can pile up while the worker is between intervals, and check it against a cold start specifically — a worker that only learns about queued work from an in-memory set populated by request traffic will forget everything across a restart unless something else re-seeds it.

Per-request database sessions

Flask-SQLAlchemy's scoped_session ties a session to the current thread and tears it down in teardown_appcontext. Under ASGI there's no guaranteed one-thread-per-request mapping to hang a scoped session off of, so the replacement is a session created and torn down explicitly per request — a FastAPI dependency with yield, opening the session before the yield and closing it in a finally after. Two things commonly go wrong porting this:

  • A sync DB driver used from an async def route blocks the event loop for every query — this is the def-vs-async-def trap from step 5, arriving specifically at the database layer, which is often the single most frequent blocking call in the whole app.
  • Session lifetime assumptions that depended on the old thread-per-request model — code that reached for "the current session" from somewhere deep in a call stack via a thread-local, rather than having it passed down, needs a contextvar or an explicit parameter instead, and (see below) a contextvar set inside a synchronous, thread-pooled dependency does not propagate back to the coroutine that awaited it, so "session id set via a contextvar inside a sync dependency" is a specific pattern to check for and remove, not just port as-is.

Traps that pass CI

These are the ones worth naming specifically, because each one is consistent with green tests and a clean local run, and only shows up under production-shaped load, a specific input, or a code-reading pass nobody did:

  • Version-specific folklore, repeated as current fact. The most widely cited ASGI trap is that BaseHTTPMiddleware buffers streaming responses, so any SSE endpoint behind one delivers nothing until it completes. This was true of older Starlette and is still repeated in write-ups, issue threads and migration guides that were never re-checked. It is not true of current Starlette: since the rework that introduced an internal _StreamingResponse fed by a body_stream() generator (starlette/middleware/base.py), BaseHTTPMiddleware forwards chunks as they are produced. Verified on 1.3.1 by measurement — first chunk readable while the generator is still blocked mid-stream, identical to no middleware at all.

    The trap is therefore not the buffering; it is inheriting a conclusion from a source older than your pinned version. This one cost a real detour on this repo's migration: the claim was written into an audit register and a changelog, and a replacement pure-ASGI middleware was written before anyone measured the thing itself, at which point the finding was retracted and the middleware reverted unshipped. Whatever your pinned versions are, run the five-line experiment (a generator that yields, blocks on an Event, then yields again; assert the client reads chunk one before you release it) rather than trusting a blog post — including this one. Keep that experiment as a test afterwards, because the property is worth pinning even when it currently holds: a later dependency bump or a newly added middleware can reintroduce buffering, and a static test suite cannot tell a streamed response from a buffered one.

  • The obvious pure-ASGI fix can itself be broken. slowapi ships SlowAPIASGIMiddleware specifically as the non-buffering alternative to SlowAPIMiddleware — and, in the 0.1.10 release, it re-sends the http.response.start ASGI event before every body chunk rather than once, which is a protocol violation that breaks any response streamed in more than one chunk. This isn't documented anywhere as a known issue; it was found by running the middleware against a real multi-chunk stream and watching it fail. The general lesson is narrower than "avoid this one library": a fix that is architecturally correct (pure ASGI instead of BaseHTTPMiddleware) can still have an implementation bug, and a small single-maintainer dependency in the exact code path you're migrating is worth actually exercising — not just reading the docs for — before you trust it in front of production streaming traffic.

  • A long-lived migration branch that auto-merges main can silently strand a fix. If main and the migration branch touch the same test file, the merge can succeed cleanly at the text level while the merged result imports something the migration branch had already deleted (a Flask module, say) — so the test file merges without conflict, and then cannot be collected or run at all. Nothing in a normal CI run flags this distinctly from "test doesn't exist"; it just quietly stops contributing coverage. If your migration branch lives longer than a few days and merges from trunk regularly, specifically check, after every merge, whether any file that merged cleanly now imports something the branch has deleted — a plain import grep across newly-merged files against your deletion list is cheap and catches this before it's a mystery three weeks later.

  • Thread-locals silently stop working, without an error. Code that relied on threading.local() (directly, or via something like Flask's g) for request-scoped state doesn't raise when ported unmodified to an ASGI app — it just returns stale or empty values, because there's no guarantee a coroutine stays on one OS thread for its lifetime under ASGI's scheduling. contextvars.ContextVar is the replacement, but it has its own boundary to know about: a contextvar's value set inside a synchronous, thread-pool-offloaded dependency (any plain def FastAPI dependency, which Starlette runs via run_in_threadpool) does not reliably propagate back out to the coroutine that awaited it. Starlette copies the current context into the worker thread so reads see the right value, but writes made inside that thread are local to the copy and don't flow back — a known, discussed limitation (fastapi#2776), not a bug that will be fixed, because the same code path also has to support ProcessPoolExecutor, where contextvars can't cross the process boundary at all. If a piece of state needs to be set by a dependency and read later in the same request, either make that dependency async def, or set the value at the async def route/middleware layer instead of inside the sync dependency.

  • A version pin that reads narrow is wider than it looks. PEP 440's compatible-release operator, ~=, only fixes as many leading version components as you write. ~=0.34 — two components — expands to >=0.34,<1.0, not "any 0.34.x patch release" the way someone skimming it might assume; that reading only holds for a three-component pin like ~=0.34.0 (>=0.34.0,<0.35.0) — and not even then on a 0.0.x project, where ~=0.0.20 expands to >=0.0.20,<0.1. For a small, actively-changing dependency sitting directly in your new critical path (a rate limiter, an auth library), a two-component ~= pin can silently pull in a minor version with a real behavior change — including, as above, a bug in exactly the code path you're relying on — with nobody having reviewed anything, on a routine pip install or lockfile refresh. Pin dependencies that sit directly in a security- or correctness-critical migration path to three components, or exactly, and bump them deliberately.

  • A test that can't observe what it asserts passes vacuously. This isn't ASGI-specific, but framework migrations create it constantly: a logging call gets ported from the standard library's logging to a different logging library (loguru, structlog) as part of modernizing the stack, and an existing test that asserts against pytest's caplog fixture — which only captures records that pass through the standard logging module — keeps passing, because the assertion is a no-op against output it structurally cannot see; there's simply nothing for it to fail on. The only reliable way to catch this class of bug is a positive control: before trusting a test, temporarily break the thing it claims to verify and confirm the test actually goes red. If it stays green while the underlying behavior is provably wrong, the test was never testing anything, and a migration is exactly the moment this tends to get introduced, because so much scaffolding — logging, error handling, request context — changes shape at once.

What this repo learned

This section draws on the findings recorded while migrating this application (PR #3299; a concise source-provenance summary is in ADR-0011). They're included here because each one generalizes past this codebase — they're restated above, in the traps section, in the general form other teams can apply directly. What's specific to this repo is how each one showed up and what it cost to find:

  • The most instructive finding here was a finding that turned out to be wrong. An audit concluded the app's five SSE routes were buffered by the rate-limit middleware, reasoning correctly from Starlette's own documentation of BaseHTTPMiddleware — documentation describing a version years older than the one pinned. It reached an audit register, a changelog entry and three commit messages before anyone ran the experiment; a replacement middleware had been written and was about to ship. Measuring it took five minutes and refuted it outright, and the middleware was reverted unshipped rather than adding risk to a rate limiter to fix nothing. The genuinely useful residue is a test that pins incremental delivery (tests/web/test_streaming_and_sse_contracts.py), which the suite had no way to express before — the static streaming checks could not distinguish a streamed response from a buffered one, which is precisely why the wrong conclusion survived scrutiny for as long as it did. SlowAPIASGIMiddleware's ASGI protocol violation (below) is real and was found the same way: by running it.
  • The long-lived-branch/auto-merge trap was real here specifically because this migration branch stayed open for 145 days across 49 merges from main. A test file merged cleanly by content — no conflict — while importing a Flask module the branch had already deleted, and so could not run. That specific failure mode (clean text merge, broken import) is exactly the shape that neither a human diff review nor a normal CI run is well-positioned to catch, because the merge itself reports success.
  • The thread-local-to-contextvar migration point in this codebase is utilities/request_context.py, populated by an ASGI middleware rather than Flask's g. The specific sharp edge — a contextvar set inside a thread-pooled def dependency not propagating back to the caller — is the reason this repo's auth dependency pattern sets the identity contextvar from the async layer rather than from inside a sync dependency; getting this backwards is the kind of bug that works in local testing (low concurrency, one dependency at a time) and fails intermittently under load.
  • The vacuous-test trap here was a caplog-based test written against a loguru-based logging call — loguru does not route through the standard logging module by default, so caplog had nothing to capture and the assertion passed unconditionally. It was caught by a positive control: deliberately breaking the log statement and confirming the test stayed green regardless.
  • The narrow-looking-pin-that-is-actually-wide trap showed up on the ASGI server and adjacent request-parsing plumbing, not on FastAPI itself: uvicorn[standard]~=0.34 and slowapi~=0.1 are two-component ~= pins and each floats a full minor-version range (>=0.34,<1.0 and >=0.1,<1.0). python-multipart~=0.0.20 is a three-component pin, but the same trap reaches it from the other end: because the project is still on a 0.0.x line, ~=0.0.20 expands to >=0.0.20,<0.1 — every future 0.0.x release, not a patch range. All three are far wider than the pin reads at a glance. fastapi~=0.136.3, by contrast, really does bound a patch range (>=0.136.3,<0.137.0), and it is deliberately narrow because it's load-bearing: 0.138 breaks include_router on this branch. The lesson isn't "pin tighter everywhere" — it's that a ~= pin's actual width depends on both how many components you wrote and where the leading zeros fall, so it's worth expanding each one rather than assuming uniformity across a pyproject.toml.

None of these are exotic. They're the ordinary cost of a real migration — findable, but only by someone actually checking each seam against the mechanism that's supposed to hold it, rather than trusting that green CI and a clean diff mean the same thing under ASGI that they meant under WSGI.

Sources