# When Python and FastAPI belong in your AI backend

URL: https://brainpowergroup.eu/en/blog/python-and-fastapi-for-ai-backends
Date: 2026-03-11 (updated 2026-08-12)
By: Barry Willems, Brainpower Group

For calls to a language model API, TypeScript is enough. If you process documents or build your own embeddings, choose a FastAPI service with a typed client.

Python belongs in your stack when you do more than call a language model: processing documents, building your own embeddings, running evaluations or hosting open models yourself. If you only call a model's API, your existing TypeScript backend can do that fine. When Python is needed, I pick FastAPI: a separate service with an OpenAPI contract, from which your frontend generates a typed client.

## For API calls, TypeScript is enough

For chat, summaries or tool use through an API you do not need Python. Anthropic has official SDKs for Python, TypeScript, C#, Go, Java, PHP and Ruby, with streaming and retries built in.

In that case, stay with one language. A second language means a second build and a second set of dependencies to maintain, and that is only worth it when you get something back. How to add AI features to an existing application, whatever the language, is covered in [the post on adding AI features to existing software](/en/blog/adding-ai-features-to-existing-software).

## When Python pays off

Python gets interesting once you work with the data itself, because most machine learning tooling treats Python as its first language. I pick Python for:

- parsing PDFs, scans and other awkward documents;
- a retrieval pipeline with your own chunking, embeddings and reranking;
- evaluations on a fixed dataset, so you can see whether a prompt change improves anything;
- an open model you run yourself instead of calling an API.

The last one depends on your hosting, because running a model on a GPU is a craft of its own.

## Why FastAPI

FastAPI is built on standard Python type hints, with Starlette for the web layer and Pydantic for the data. You describe a request and a response with types, and FastAPI validates the input and turns it into an OpenAPI specification. Interactive documentation is available at `/docs` straight away.

Its async support suits AI work. A request waiting on a language model is mostly idle, and async lets the server handle other requests in the meantime. Since version 0.135.0 FastAPI also has built-in support for Server-Sent Events, which you use to stream tokens to the browser.

Pydantic is also useful for structured output. You define the schema you expect from the model once, and validate the answer against it before you do anything with it.

Django is still a good choice when you also want an admin and an ORM. For an internal AI service that is usually more than you need.

## The contract between TypeScript and Python

The link with your frontend runs through the OpenAPI specification that FastAPI generates. From it you generate a TypeScript client. The FastAPI documentation recommends Hey API (`@hey-api/openapi-ts`) for that. With a custom `generate_unique_id_function` you get clean function names instead of long generated ones.

My advice is to run that generation in CI. If the specification changes and the client has not been regenerated, the build fails. That way a breaking change in the Python service shows up in your TypeScript code before a user runs into it. Boris wrote more about [designing API-first](/en/blog/designing-api-first).

## Python behind your TypeScript backend

In the setup I usually recommend, the Next.js frontend talks to your TypeScript backend, which handles authentication and the database. The Python service runs behind it and only does the AI work. That way authentication lives in one place. It also means the Python service gets no public URL. Only the TypeScript backend can reach it, for example over an internal network in Docker Compose or in your cloud environment.

Long-running work, such as processing a folder of a hundred PDFs, does not belong in an HTTP request. Put it on a queue, let a worker pick it up and return a status to the frontend. Streaming an answer can stay within a request, with Server-Sent Events along the whole chain.

## Pitfalls in production

Blocking code inside an `async def` function holds up the whole server. FastAPI runs a plain `def` function in a separate threadpool, so use that for libraries without `await`. Heavy computation, such as calculating embeddings locally, belongs in a separate worker.

Pin your dependencies with a lockfile, for example with uv or Poetry, and run mypy or pyright in CI. That gives the Python side the same discipline as your TypeScript code. Machine learning libraries make Docker images large, so keep the API image small and put heavy models in their own service.

Log per request which model you called, how many tokens it used and how long it took. Without that data you cannot tell where the cost and the waiting time come from.

Finally, make sure at least two people on the team can maintain the Python service. A single Python developer in a TypeScript team is a risk as soon as that person goes on holiday. Count that second person in when you [plan the team](/en/build-a-remote-team).

## Sources

- [FastAPI, documentation](https://fastapi.tiangolo.com/)
- [FastAPI, generating clients](https://fastapi.tiangolo.com/advanced/generate-clients/)
- [FastAPI, Server-Sent Events](https://fastapi.tiangolo.com/tutorial/server-sent-events/)
- [FastAPI, async and the threadpool](https://fastapi.tiangolo.com/async/)
- [Anthropic, official SDKs](https://platform.claude.com/docs/en/cli-sdks-libraries/overview)

## Frequently asked questions

**Do I need Python to build AI features?**

Not if you only call a language model's API. There are official SDKs for TypeScript and other languages. Python pays off when you process documents, build your own embeddings, run evaluations or host models yourself.

**How do you connect a FastAPI backend to a TypeScript frontend?**

FastAPI generates an OpenAPI specification. You generate a typed TypeScript client from it, for example with Hey API. Run that generation in CI, so contract changes show up immediately.

**Can FastAPI stream responses from a language model?**

Yes. FastAPI supports async and has had built-in support for Server-Sent Events since version 0.135.0. That lets you send tokens to the browser while the model is still writing.

---

Source: https://brainpowergroup.eu/en/blog/python-and-fastapi-for-ai-backends
Brainpower Group, contact: https://brainpowergroup.eu/en/contact, hello@brainpowergroup.eu
