PyCon India 2025

FastAPI for Production: Patterns and Architecture in Practice
2025-09-12 , Room 3

FastAPI is deceptively simple at first glance, but developers often get stuck beyond “just works” to real-world, maintainable production systems.

This hands-on workshop dives deep into design patterns, architectural best practices, and real-world project structure that help you confidently ship and scale FastAPI applications. We'll explore patterns like Dependency Injection, Routing strategies, DTOs vs Pydantic models, Creational and Structural patterns, power of middlewares, Hexagonal Architecture, Service Layer separation, Common reusable modular code that adapts to API, testing strategies and much more.

Through a series of guided examples and exercises, participants will walk away with a practical blueprint for designing clean, testable, and maintainable APIs.


Outline

1. Foundations Recap (15 mins)

FastAPI basics: routing, models, DI, responses
Setting up a project structure (domain/app separation)

2. Layered Architecture (30 mins)

Service Layer Pattern
Repositories & use-cases
Keeping endpoints thin and services smart
Create modular reusable services

Exercise: Refactor a monolithic API to use services and repositories

3. Schema Design Patterns (20 mins)

DTOs vs Pydantic Models
Using inheritance, aliasing, and nesting
Validators and custom error messages

Exercise: Add multiple schemas for Create, Read, and Update with validation
Create modular reusable schemas

4. Dependency Injection & Lifespan (20 mins)

Custom dependencies
When to use Depends()
Startup/shutdown events, database sessions

Exercise: Inject services and DB connections, services, configs in a clean, reusable way

5. Routing & Modular Patterns (20 mins)

Router grouping, nested routes and Custom Routers
Versioning and route prefixing
Breaking apps into feature modules
Exercise: Create a modular route layout with grouped routers

6. Production Patterns (30 mins)

Custom exception handling
Logging and observability tips
Background tasks and event hooks

Middlewares
Exercise: Add custom error responses and log request lifecycle

7. Testing and Maintainability (15 mins)

8. Test services and routes

Use pytest, mock dependencies, isolate layers
Exercise: Write tests for services and route validation


Prerequisites

Python and FastAPI Basics

Target Audience

Beginner

I am a Python Enthusiast who loves building software applications and education related content. I am a technology professional and a passionate programmer with 12 years of experience in Python & Python related technologies. I am currently working as Software Architect at EPAM India.

I have been involved with multiple professional projects in various industrial domains and technical fields. My expertise is in application development, data processing & analysis, data pipelines, non-relational and relational databases, Python, FastAPI, Flask, Celery, Redis, AWS, Airflow, GreatExpectations, Docker, and various other tech stacks. I am also an open source contributor, and published self developed libraries on PyPI. I love creating libraries and various utility tools that help me in solving a challenge/problem that could also be used by others in the developers community.

This speaker also appears in: