FastAPI and Pydantic CamelCase

Hassle free what to implement camelCase response/request body while keeping your python models pythonic and in snake_case.

I previously wrote an article about how to have a camelcase request response bodies while still having your python code in snake_case. Afterwards it made more sense to create a package and add it to PYPI.



Lets assume that you have an endpoint representing a user resource as follows.

Here was have our model or schema

and we have an endpoint which handles POST requests to create a user

if we look at /docs we will see that our schema is in snake_case as expected

fixing that is really easy with fastapi-camelcase package


pip install fastapi_camelcase

edit your code as follows

instead of from pydantic import BaseModel we will use from fastapi_camelcase import CamelModel

and we will use CamelModel whenever we need the request/response body to be camelCased.

Full code should look like this:

And Voilaaa!

Software Developer