Working with FastAPI?

FastAPI service misbehaving in production? We fix it.

Most FastAPI codebases that reach us look fine locally and fall over the moment real traffic arrives. A sync call inside an async handler, a Pydantic v1 model nobody dares touch, or a deploy that was never sized for real workers. We find the piece that is actually slowing the loop down and sort it out.

  • Since 2015Fixing technology
  • FreeEstimates
  • US teamReal engineers

uvicorn.log

still failing

What it is

FastAPI at a glance

FastAPI is a modern Python web framework for building APIs, first released in December 2018 by Sebastian Ramirez. It sits on top of Starlette for the ASGI machinery and Pydantic for data validation, and it turns ordinary Python type hints into runtime request validation, response serialisation and an automatically generated OpenAPI and Swagger UI.

The framework is async-first, with native support for async def handlers, WebSockets and background tasks. The current line sits on Pydantic v2, released in 2023, which was a hard break from the v1 model API that a lot of older projects were written against.

FastAPI has become a common choice for machine learning inference endpoints, AI product back ends, JSON APIs behind single-page frontends, internal tooling and microservices. It competes directly with Flask, Django REST Framework and Node Express in that same slice of work.

Type
Python async web framework for APIs
First released
2018
License
MIT
Maintained by
Sebastian Ramirez and contributors
Typically used for
JSON APIs, ML inference endpoints, internal tooling, microservices, AI product back ends

The honest version

FastAPI pros and cons

No technology is all upside. This is where FastAPI earns its place and where it tends to cause trouble.

Where FastAPI is strong

  • Type hints become real validationA Pydantic model on a request body is not documentation but working code: the framework rejects malformed input before your handler ever runs, and it does so with helpful error messages.
  • Async and await are first classAsync def handlers, async dependencies and async middleware are the native path, not a bolt-on, which matters a great deal for services that spend their time waiting on other services.
  • Swagger and OpenAPI for freeThe framework builds an OpenAPI schema and an interactive Swagger UI from your type hints automatically, so the docs do not drift away from the code the way hand-written API docs always do.
  • Developer experience built for APIsError messages point at the exact field that failed validation, the auto-reloader is quick, and the shape of a handler reads like a function signature rather than a routing table.
  • A clean dependency injection systemThe Depends() helper lets you compose authentication, database sessions and feature flags without wiring them through every function, and the same dependencies work for tests.
  • Starlette underneathStarlette is a lean, fast ASGI toolkit, and FastAPI inherits its routing, middleware and WebSocket support rather than reinventing any of it.
  • Throughput comparable to Node and GoA FastAPI service that avoids blocking the event loop will match the request rates teams usually associate with Node Express or a small Go API, which is rare territory for Python.
  • WebSockets and background tasks includedReal-time endpoints and fire-and-forget work are supported in the box, so a small service does not need to pull in Celery or a separate socket server on day one.

Where FastAPI bites

  • Easy to block the event loopA single synchronous database call or a requests.get inside an async def handler will stall every other request on that worker, and the symptom is a service that looks idle while timing out.
  • Pydantic v1 to v2 broke a lotThe July 2023 Pydantic v2 release changed validators, Config classes and serialisation behaviour, and plenty of plugin libraries lagged behind, so migrations tend to touch far more files than anyone expects.
  • SQLAlchemy async needs careAsync sessions, greenlet compatibility and the boundary between async handlers and sync ORM code are a reliable source of production bugs, especially around transaction scope and expired objects.
  • Depends() chains get brittleIt is tempting to express everything as a dependency, and before long the signature of a handler is twelve Depends calls deep and reordering any one of them breaks tests nobody can trace.
  • Deploying ASGI confuses teamsUvicorn with Gunicorn workers, Uvicorn standalone, Hypercorn, Daphne, worker counts, graceful shutdown and lifespan events are not one decision, and teams that skipped that reading tend to run one worker on a sixteen core box.
  • Lifespan events still trip some setupsStartup and shutdown hooks moved to the lifespan context manager, and plenty of older examples, some load balancers and a few ASGI tools still do not line up with it cleanly.

Sound familiar?

Common FastAPI problems we fix

The FastAPI problems that bring people to us, and what you have once each one is fixed.

sync endpoint blocked event loop for 2100ms

The API is slow even though it is async

A handler declared async def that then calls a synchronous library, usually requests or a sync ORM, which parks the whole event loop until the call returns and queues every other request behind it.

Event loop unblocked, latency back down

pydantic v2: BaseModel.Config is deprecated

Stuck on Pydantic v1 after FastAPI 0.100

A codebase written against Pydantic v1 with inner Config classes, validator decorators and v1 serialisation, now blocked from upgrading FastAPI because 0.100 and newer require Pydantic v2.

Models migrated, upgrade unblocked

author: unknown

The developer who built the service has left

A FastAPI codebase with custom dependencies, a hand-rolled auth layer and a deploy script nobody has read in a year, and no one left who can explain how a request actually gets served.

Read, documented, maintainable

gunicorn: worker exited with signal 9 (OOM)

Workers keep getting killed in production

Gunicorn running too many Uvicorn workers for the memory available, a request that loads a large model or dataset per request, and the kernel killing the worker before it can reply.

Workers sized to the box, OOM gone

SQLAlchemy async: Connection was closed

The database layer is unstable under load

Async SQLAlchemy sessions kept open past the request, transactions crossing await points they should not, and intermittent errors that only show up when two requests race for the same connection.

Session scope correct, transactions clean

Depends chain · 12 levels

Dependencies have grown into a tangle

Every handler takes a stack of Depends arguments that call each other, overrides in tests do not line up with overrides in production, and small changes ripple through routes that should be unrelated.

Dependencies flattened, tests predictable

OpenAPI: operationId collision

The auto-generated OpenAPI schema is broken

Two endpoints with the same function name, duplicate operation IDs, or a response model that serialises differently from what Swagger advertises, so client code generators produce broken clients.

Schema valid, clients regenerate cleanly

What we do

FastAPI help, from rescue to upkeep

Call us for one slow endpoint, or hand us the whole service.

Pydantic v2 migration
Moving models, validators and Config classes from Pydantic v1 to v2, with the API behaviour before and after compared against real payloads rather than hoped to be identical.
Async performance work
Finding the sync calls inside async handlers, the dependencies that block, and the database calls that serialise, and leaving the service with event loop latency you can measure.
Takeovers
Inheriting a FastAPI service from a developer who has moved on, reading it properly and giving you back something a new engineer can pick up without a week of archaeology.
Deploy and sizing
Uvicorn with Gunicorn workers set up for the hardware you actually have, graceful shutdown, lifespan events and health checks that reflect reality.
Database integration
Async SQLAlchemy done without the usual session and transaction footguns, or sync SQLAlchemy run correctly off the event loop where that fits better.
Managed Tech
Dependency updates, monitoring and a team to call, so a FastAPI service does not drift into an unsupported Pydantic or Python version between hands.

How it works

From first message to fixed

  1. Look Free estimate

    Tell us what's going wrong. We look at it, then tell you plainly what's wrong and what it will cost.

  2. Fix

    We fix the cause, not the symptom. If something is down, getting it back up comes first.

  3. Harden

    Backups, monitoring, updates and access, so the same problem can't quietly come back.

  4. Hand back or stay on

    You get a plain-language write-up. Or we stay on for hosting, upkeep and new work.

Often found together

Running FastAPI alongside something else?

We work on the whole stack, so the problem can be wherever it actually is.

Questions

FastAPI questions

Something else? Call+1 737 727 7327

Can you upgrade our FastAPI app from Pydantic v1 to v2?

Yes, and this is one of the most common FastAPI jobs we pick up. We move the models one module at a time, replace inner Config classes with model_config, port validator and root_validator decorators to field_validator and model_validator, and check the serialised output against a captured set of real payloads so behaviour does not drift. FastAPI 0.100 and newer require Pydantic v2, so getting off v1 is the gate to every other upgrade on the project.

Can you take over a FastAPI codebase from a previous developer?

Yes. We read the routers, the dependencies, the settings and the deploy configuration, write down how a request is actually served from socket to response, pin a reproducible environment, rotate any credentials the old developer had, and leave the service in a state the next engineer can pick up without guessing.

Why is our FastAPI endpoint slow even though it's async?

Almost always because the handler is async def but calls synchronous code inside it. A requests.get, a sync SQLAlchemy query or a blocking file read will park the event loop and queue every other request behind it. The fix is either an async equivalent of the call or running the sync work in a thread pool with asyncio.to_thread or run_in_executor. We measure the loop with real traffic first, then fix the handful of handlers that account for the latency.

How do we deploy FastAPI safely with Uvicorn and Gunicorn workers?

The common production shape is Gunicorn as the process manager with Uvicorn worker classes, sized to roughly one worker per CPU core with enough memory headroom that none of them get killed by the OOM killer. We set graceful shutdown timeouts, wire the lifespan context manager so startup and shutdown hooks actually run, and add health and readiness endpoints that reflect database connectivity rather than always returning 200.

Should we rewrite our Flask or Django API in FastAPI?

Only when there is a real reason. If the service is I/O bound, needs WebSockets or would benefit from automatic OpenAPI, FastAPI is a strong fit. If it is a healthy Django app with the ORM, admin and auth already in use, Django REST Framework is almost always the right answer and a rewrite in FastAPI mostly moves the work from one codebase to another. We will tell you plainly which side the evidence points to before any code is touched.

How much does FastAPI help cost?

Estimates are free. Describe the service and what is going wrong, and you get a price before any work begins.

Free estimate

FastAPI service giving you trouble?

Tell us what you're running and what's going wrong. A person reads it and comes back with an estimate.

  • No charge to find out what it will take
  • If it's urgent, say so

Replies come from a person. By sending, you authorize us to email you back. No marketing.