Accelerate FastAPI Development with OpenAPI Generator

Dr. Evelyne Groen, Kateryna Budzyak

Programming & Software Engineering & Testing
Python Skill Intermediate
Domain Expertise Intermediate

Machine learning models are often deployed as APIs, where we have an endpoint that generates predictions given some input. For example, we can send a POST request specifying a color, a length, and a number of legs, and the endpoint predicts the best fitting animal. The description of the endpoint, the schema of the request, and the response acts as a form agreement between the consumer and the service. In practice, the restrictions on the API are not well defined. How does the consuming app know if a parameter is optional or required? In this tutorial you will learn to define an API contract as an OpenAPI specification (OAS). OAS is a standardized description of the API endpoints and data models. We will demonstrate how to use the OpenAPI Generator to automatically generate the API endpoints and strictly typed Pydantic data models, by only designing the OAS in YAML format, without GenAI. OpenAPI Generator utilizes mustache templates to translate the specification into actual code. We will demonstrate use cases for customizing the template for specific needs of the resulting API stubs.
By generating code from the contract, you ensure that the deployed application always reflects the agreed-upon specification. It automates the writing of repetitive code, such as Pydantic models and endpoint definitions, allowing developers to focus on the implementation logic. It enforces standard patterns and structures, ensuring consistency and maintainability across different projects.

Expect fun mystic creatures after deploying the resulting API in your local environment.

Target Audience

Engineers and data scientists looking to standardize their FastAPI development workflow. We expect you to have basic knowledge in Python, virtualenv, Pydantic data models and FastAPI.

Technical Setup

Outline

  1. Introduction (10 min)
  • The philosophy of Contract-First development
  • Overview of the OpenAPI specification and Pydantic data models
  • Introduction to the OpenAPI generator tool
  1. Design (20 min)
  • Introduction to the unicorn service logic (Input: Real Life Problems, Output: Mystic Creatures)
  • Definition of the openapi specification, focusing on the Request and Response schemas
  1. Generate (30 min)
  • Running the standard vanilla OpenAPI generator
  • Introduction to mustache templates
  • Customization of the default mustache to inject our specific dependencies
  1. Implementing (15 min)
  • We will connect the generated API stubs to a predict() function that calls our unicorn generation service.
  1. Demo & QA (15m)
  • Running the server via uvicorn and testing our unicorn service endpoint using the Swagger UI.

Dr. Evelyne Groen

I am a senior MLOps engineer at Malt. A long time ago I studied physics in Amsterdam, after which I moved to Berlin to discover the world of data science. Currently I'm working at Malt exploring the boundaries between devops and data.

Kateryna Budzyak

Kat is a Senior Machine Learning Engineer at Malt, the freelancer marketplace, where she works in the relevancy and matching team. She has a background in bioinformatics and passionate about beautiful code.