DEV Community

Cover image for 5 Reasons to Adopt FastAPI to build REST Web Services
Rodolfo Mendes
Rodolfo Mendes

Posted on • Originally published at reinforcement-learning4.fun

5 Reasons to Adopt FastAPI to build REST Web Services

A common way of deploying our machine learning models is exposing them through a REST Web Service API. And because our focus is not creating web tools, we prefer to adopt a well-tested, ready-to-use framework to shorten our time to market.

When choosing a framework, we want to get the right balance between performance and productivity. Usually, as a tool is more specific and closer to the bare metal, it will perform better. However, low-level libraries leave most of the work to us. On the other hand, if a framework provides too much abstraction, it could degrade performance. 

This article briefly discusses five reasons to adopt FastAPI as the framework for our REST API and why it offers an outstanding balance between performance and productivity. 

1. High Performance

FastAPI is one of the fastest Python frameworks available, and its performance is comparable to NodeJS and Go frameworks, according to the TechEmpower benchmark.

2. Declarative Routing

Creating routes and endpoints using FastAPI is very simple. We include a decorator around a method, and it will serve on the path specified in the decorator argument: 

@app.post("/diabetes:predict")
def predict(p):
    return __predict_batch([p])[0]

To receive a path parameter, we need to declare the name of the path parameter in the path template and then declare a method parameter using the same name: 

@app.get("/patients/{patient_id}")
def get_patient(patient_id):
    pass

Any other parameter declared in the method signature is handled as a query parameter. If the method has typed parameters, FastAPI automatically converts the query string parameters to the right type. In the example below, the endpoint supports the parameters "size" and "page" so that the framework will automatically convert the query parameters to their right type:

@app.get("/patients")
def get_patient(page: int, size: int):
    pass

With FastAPI, we declare routes and their parameters close to the method that servers the endpoint. We also receive the route parameters directly in method parameters, so we don't need to inspect generic data structures to find and validate parameters, representing a gain in our productivity.

3. Automatic Serialization and Deserialization

FastAPI automatically deserializes the bodies of requests into method parameters. To receive the request body as an object in our method, we need to create a Pydantic model class and declare this model as the type of our parameter. For example:

from pydantic import BaseModel

class Patient(BaseModel):
    name: str
    address: str

@app.post("/patients")
def save_patient(patient: Patient):
    pass

On the other hand, it is possible to automatically serialize a model to JSON format by just returning the object. For example:

@app.get("/patient/{patient_id}")
def save_patient(patient_id):
    return Patient('John Smith', '99 Nowhere Street')

Thus, we FastAPI we don't need to worry about handling serialization and deserialization in our business code. 

4. Declarative validation

As we have declarative routing, we validate our parameters by declaring their accepted values instead of writing logic to test them. The first alternative in which we obtain automatic validation is by declaring the parameters' types in the method signature. In the following example, trying to pass an argument that is not an integer to the parameters page and size will result in a client error:

@app.get("/patients")
def get_patient(page: int, size: int):
    pass

However, only validating the parameters' types is not enough. We also need to validate the accepted range for them. In the previous, it does not make sense to have negative values or zero for page and size. So we can modify our method to accept only positive numbers:

@app.get("/patients")
def get_patient(\
        page: int = Query(..., gt=0),\
        size: int = Query(..., gt=0)):
    pass

We can also set validators for our models. For example, we can validate that the field name of a Patient model will only accept letters and have a maximum length of 50 characters:

class Patient(BaseModel):
    name: str = Field(regex='[a-zA-Z\s]*', max_length=50)
    address: str

Using declarative validation, we drastically reduce the amount of code in our application, as we do not need to write logic to test our parameters. And because we can write validation more easily and rapidly, we significantly increase our API's quality and security.

5. Automatic Documentation Generation

Good documentation is an essential part of a great API. However, for most developers is a tedious task, and in many cases, they don't write any documentation at all. Even for those who enjoy writing documentation, writing a documentation page is time-consuming, which could be a problem in a short deadline project.

With FastAPI, we have excellent documentation for our API with no significant additional efforts. That's because FastAPI automatically generates an OpenAPI (Swagger) documentation page for our API and makes it available under our application's/doc path. The documentation generated by FastAPI includes:

  • The endpoints of our API
  • The request parameters and responses
  • The returned codes
  • The schemas of entities used in requests and responses
  • An interface to test our API

All the information used to build the documentation page comes from our code. There's no need to write additional files nor executing extra commands to generate the docs. You start serving the application, and the documentation is there.

Conclusion

FastAPI is a great framework that offers an outstanding balance between performance and productivity. The declarative nature of routes and validation, combined with automatic features like serialization and documentation, allows us to write high-quality APIs with much less code so that we can focus on our models and business goals.

To learn more about FastAPI, visit the site:

https://fastapi.tiangolo.com/

Top comments (2)

Collapse
 
onnotimmerman profile image
code-on

If you like fast-api you also have django alternative...
It similar but integrated in django eco system
django-ninja.rest-framework.com/

Collapse
 
rodolfomendes profile image
Rodolfo Mendes

Great. Thank you!