FastAPI has Ruined Flask Forever for Me

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 (for 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 because nothing is more frustrating than when the end-users are implementing but do not match up the documentation. So 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)

http://127.0.0.1:8000/docs

Alternative automatic documentation is also provided (provided by ReDoc)

http://127.0.0.1:8000/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.

# Source: https://medium.com/@tiangolo/introducing-fastapi-fdc1206d453f

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool = None

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

@app.put("/items/{item_id}")
def create_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}

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:

@app.get('/book')
async def read_book():
    book = await get_book()
    return book

In conclusion, FastAPI is a fast web framework and supports asynchronous code. In addition, this is coupled with very complete documentation and an integrated validation system that makes it easier to use.

Are you interested in using FastAPI? If you want to get started using FastAPI, there are some great resources out there. Here are some that you can explore.

Did you find this article valuable?

Support Programmer's footnote by becoming a sponsor. Any amount is appreciated!