Accelerate FastAPI Development with OpenAPI Generator
Dr. Evelyne Groen & Kateryna Budzyak
Transcription
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.