avatarAhmed Nafies

Summary

The web content provides a tutorial on how to use camel-case JSON request/response bodies in FastAPI while maintaining snake-cased Pydantic models, using the fastapi_camelcase package and Pydantic's Config class for alias generation.

Abstract

The article discusses a common challenge faced by FastAPI developers who want to serialize their Pydantic models using camel-case JSON while keeping their codebase in snake-case to adhere to PEP-8 standards. The author outlines the problem with an example using a User model and demonstrates how to use Pydantic's Field aliases to achieve camel-casing in the JSON schema. To avoid manually creating aliases for numerous fields, the author introduces the alias_generator function within Pydantic's Config class, which leverages the humps library to automatically convert field names to camel-case. Additionally, the allow_population_by_field_name configuration is highlighted as necessary to prevent internal server errors when using aliases. The article culminates in the creation of a reusable CamelModel base class that encapsulates this functionality, and the author promotes their fastapi_camelcase package, which simplifies the process of implementing camel-case serialization across multiple models in FastAPI applications.

Opinions

  • The author finds the FastAPI and Pydantic documentation to be comprehensive but notes a gap in covering the specific use case of camel-case serialization with examples.
  • They express enthusiasm and relief regarding the existence of solutions for automatic alias generation and the ease of integrating these solutions into FastAPI applications.
  • The author's use of humor is evident when encountering an internal server error during the tutorial, using a GIF to express surprise and frustration.
  • The author believes that using camel-case for JSON serialization while maintaining snake-case in the codebase is not only possible but also desirable for adhering to Python standards and improving the developer experience.
  • They advocate for the use of inheritance to extend the CamelModel base class, suggesting that this approach is scalable and beneficial for projects with multiple models.
  • The author is proud of the fastapi_camelcase package they have created, recommending it as a convenient tool for developers to achieve camel-case serialization in FastAPI.

CamelCase Models with FastAPI and Pydantic

The easiest way to to have camel-case JSON request/response body while keeping your models snake-cased

Intro

It took me a while to figure out how to use camel-casing for one of the Fast API micro-services endpoints that I have been working on while keeping “Model” fields snake-cased.

The documentation for FastAPI and Pydantic are awesome but it did not cover that part in a nice and easy example to comprehend . This is why I decided to create this tutorial.

I have also created a package called fastapi_camelcase which you can install using pip and use right away, I wrote an article about it here.

HINT: This tutorial assumes that you have some experience with FAST API and know how to create REST endpoints using Pydantic models

The Problem

lets assume that you have the following application exposing an API endpoint that represent a resource called “User” in your “models.py”

from pydantic import BaseModel


class User(BaseModel):
    first_name: str
    last_name: str = None
    age: int

and you have your “main.py” containing the “run” configuration

import uvicorn
from fastapi import FastAPI
from models import User


app = FastAPI()


@app.post("/user/", response_model=User)
async def create_user(user: User):
    return user


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

if you run the application

python main.py
snake_case request/respose

we will see snake-cased request/response body in the OpenAPI documentation as we expected. Now we can change the field names to camelCase but that does not follow the PEP-8 standard.

Pydantic Aliases

Aliases for pydantic models can be used in the JSON serialization in camel case instead of snake case as follows:

from pydantic import BaseModel, Field


class User(BaseModel):
    first_name: str = Field(..., alias='firstName')
    last_name: str = Field(..., alias='lastName')
    age: int = Field(..., alias='age')

That we make our schema in camelCase

camel-case request/response body

What if we have 1000 fields instead of 3, what do we do now? I wish there was a magic alias generator.

Luckily there is :)

Alias Generator

Pyndantic has a Config class which controls the behaviour of models. We can generate aliases for all of the model fields as follows:

from pydantic import BaseModel
from humps import camelize


def to_camel(string):
    return camelize(string)


class User(BaseModel):
    first_name: str
    last_name: str = None
    age: float

    class Config:
        alias_generator = to_camel

now lets try it out

Internal server error

Internal server error? SAY WHAAAAT?

error log

Well of course, we are using aliases (camelCase) instead of field names (snake_case), I hope there is an easy to fix that

Luckily there is :D

All what is needed to be done is to add the following to the model Config class

allow_population_by_field_name = True

so our model should look like this now

from pydantic import BaseModel
from humps import camelize


def to_camel(string):
    return camelize(string)


class User(BaseModel):
    first_name: str
    last_name: str = None
    age: float

    class Config:
        alias_generator = to_camel
        allow_population_by_field_name = True

lets try it one last time

success repose

AND BOOM!, all of our code is in snake_case and request/response and documentation are in camelCase, isn’t that AWESOME?

One last enhancement

We would probably have many models used by many API endpoints and we want to use camelCase across them all, right? probably :)

Now lets use the power on inheritance

from pydantic import BaseModel
from humps import camelize


def to_camel(string):
    return camelize(string)


class CamelModel(BaseModel):
    class Config:
        alias_generator = to_camel
        allow_population_by_field_name = True
class User(CamelModel):
    first_name: str
    last_name: str = None
    age: int

Great, now you can inherit from CamelModelinstead of BaseModeland ENJOY!!!

I created a simple package easy to use following the above tutorial.

pip install fastapi_camelcase

from fastapi_camelcase import CamelModel
class User(CamelModel):
    first_name: str
    last_name: str = None
    age: int

The full code for this example can be found here on github

Python
Fastapi
Pydantic
Rest Api
Json
Recommended from ReadMedium