FastAPI has Ruined Flask Forever for Me
By: Fahmi Nurfikri
Tags: Programming, Python, Flask, Data Science, Machine Learning

FastAPI has Ruined Flask Forever for Me

FastAPI Logo — https://fastapi.tiangolo.com

What do you like best about being a data scientist? It’s definitely modeling and fine-tuning for optimal results. But what does it mean to be a good model if it’s never used or never deployed?

To produce a machine learning model, the typical approach is to wrap it in a REST API and use it as a microservice. One of the most widely used frameworks for creating APIs is Flask.

The main reason Flask is widely used is its simplicity. In general, we only use the API to model predictions, so we don’t need a complex architecture (example: Django). Another reason is that Flask is written in Python, which is the language used to do machine learning modeling in general, so we are familiar with it.

However, if you want to create a REST API with clear, static, inputs that are validated, you must include several different packages from several third parties that do not cooperate with each other. And you have to create custom code to get everything running.

This is what caused me to look for alternatives for my needs, where finally I found a framework called FastAPI and it became my new favorite framework. Here are the reasons why I like to use FastAPI.

Documentation

The first thing I notice from FastAPI is the documentation. FastAPI has very extensive documentation and rich examples, which makes things easier. If you need to look for something about FastAPI, you usually don’t need to look anywhere else.

In addition, FastAPI will automatically generate interactive API documentation from the program we are running. So that we don’t need to write the API documentation that we created ourselves. As we all know, writing API documentation is annoying, not only because there is a lot of detail to go into, but also nothing more frustrating when the end-users are implementing but not match up the documentation. So that with the automatic generation of API documentation it will be very helpful

Below is a display example of the API documentation (provided by Swagger UI)

Alternative automatic documentation is also provided (provided by ReDoc)

To define the created schema, it uses Pydantic, which is another awesome Python library, used for data validation. Its purpose is to validate the input that goes to FastAPI. If the input/request is incorrect then the appropriate error code will be returned.

Performance

As the name implies, this is indeed FastAPI. Based on data from techempower benchmarks, FastAPI beats every framework when it comes to performance. Where the most commonly used frameworks like Flask and Django lag behind.

Framework performance — techempower

Yeah, it blazingly fast.

Asynchronous

One of the drawbacks of Flask is the lack of asynchronous I/O support. Asynchronous is pretty important for HTTP endpoints, which tend to do a lot of waiting for I/O and network chat, making it a good one candidate for concurrency using async.

Unlike Flask, FastAPI supports asynchronous code out of the box using the async / await Python keywords. FastAPI supports asynchronous by default, which means we don’t need to use a third-party framework to do asynchronous.

All you have to do is declare the endpoints with the async keyword like this: