Accelerate FastAPI Development with OpenAPI Generator
In this presentation, Dr. Evelyne Groen, Senior MLOps Engineer at Malt, and Kateryna Budzyak, Senior Machine Learning Engineer at Malt, demonstrate how to accelerate FastAPI development using a contract-first approach. Evelyne and Kateryna address a common pain point in machine learning deployment: the fragile "agreement" between API consumers and services, where diverging documentation and code often lead to integration failures. By utilizing the OpenAPI Generator, the speakers show how to establish a single source of truth through an OpenAPI specification (OAS) to automatically generate API endpoints and strictly typed Pydantic data models.
The session guides viewers through the entire lifecycle of contract-first development, starting with the design of the OAS in YAML format, moving to the generation of a FastAPI skeleton, and concluding with the implementation of a functional ML model. A key highlight of the talk is the use of mustache templates to customize generated code to fit specific project requirements. Attendees will learn how this methodology eliminates repetitive boilerplate code, enforces standardization across projects, and ensures that the deployed application always reflects the agreed-upon specification. By shifting the focus from manual endpoint definition to architectural design, Evelyne and Kateryna provide a scalable workflow for engineers and data scientists to improve the maintainability and consistency of their production APIs.
This description was generated by Open-Source AI using the transcript of the session and the original submission contents.
This session took place in track Programming & Software Engineering & Testing and was classified suitable for intermediate domain / intermediate python by the speaker.
Submission
The proposal as submitted by the speaker before the conference.
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.
To attend this workshop, please install the openapi generator v7.20. For details, please visit the README.md of https://gitlab.com/Eeffee/pycon26
Technical Setup
- Operating system: We recommend using Unix OS (Mac or Linux)
- Python: Version 3.10+
- OpenAPI Generator: Version 7.20
- Installation Guide: https://openapi-generator.tech/docs/installation/
For details, please visit the README.md of https://gitlab.com/Eeffee/pycon26
Outline
- Introduction (10 min)
- The philosophy of Contract-First development
- Overview of the OpenAPI specification and Pydantic data models
- Introduction to the OpenAPI generator tool
- 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
- Generate (30 min)
- Running the standard vanilla OpenAPI generator
- Introduction to mustache templates
- Customization of the default mustache to inject our specific dependencies
- Implementing (15 min)
- We will connect the generated API stubs to a predict() function that calls our unicorn generation service.
- Demo & QA (15m)
- Running the server via uvicorn and testing our unicorn service endpoint using the Swagger UI.