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.

PYPI

Github

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


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

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

Installation

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store