r/FastAPI • u/aliparpar • Sep 13 '24
Tutorial Upcoming O'Reilly Book - Building Generative AI Services with FastAPI

UPDATE:
Amazon Links are now LIVE!
US: https://www.amazon.com/Building-Generative-Services-FastAPI-Applications/dp/1098160304
UK: https://www.amazon.co.uk/Building-Generative-Services-Fastapi-Applications/dp/1098160304
Hey everyone!
A while ago I posted a thread to ask the community about intermediate/advanced topics you'd be interested reading about in a FastAPI book. See the related thread here:
https://www.reddit.com/r/FastAPI/comments/12ziyqp/what_would_you_love_to_learn_in_an_intermediate/
I know most people may not want to read books if you can just follow the docs. With this resource, I wanted to cover evergreen topics that aren't in the docs.
I'm nearly finishing with drafting the manuscript which also includes lots of topics related to working with GenAI models such as LLMs, Stable Diffusion, image, audio, video and 3D model generators.
This assumes you have some background knowledge in Python and have at least skimmed through the FastAPI docs but focuses more on best software engineering practices when building services with AI models in mind.
š The book will teach you everything you need to know to productise GenAI by building performant backend services that interact with LLMs, image, audio and video generators including RAG and agentic workflows. You'll learn all about model serving, concurrent AI workflows, output streaming, GenAI testing, implementing authentication and security, building safe guards, applying semantic caching and finally deployment!
Topics:
Learn how to load AI models into a FastAPI lifecycle memory
Implement retrieval augmented generation (RAG) with a vector database and streamlit
Stream model outputs via streaming events and WebSockets into browsers
How to handle concurrency in AI workloads, working with I/O and compute intensive workloads
Protect services with your own authentication and authorization mechanisms
Explore efficient testing methods for AI models and LLMs
How to leverage semantic caching to optimize GenAI services
Implementing safe guarding layers to filter content and reduce hallucinations
Use authentication and authorization patterns hooked with generative model
Use deployment patterns with Docker for robust microservices in the cloud
Link to book:
https://www.oreilly.com/library/view/building-generative-ai/9781098160296/
Early release chapters (1-6) is up so please let me know if you have any feedback, last minute changes and if you find any errata.
I'll update the post with Amazon/bookstore links once we near the publication date around May 2025.
1
u/aliparpar Jan 14 '25
UPDATE:
Amazon Links are now LIVE!
US: https://www.amazon.com/Building-Generative-Services-FastAPI-Applications/dp/1098160304
UK: https://www.amazon.co.uk/Building-Generative-Services-Fastapi-Applications/dp/1098160304
Accompanying Repository: https://github.com/Ali-Parandeh/building-generative-ai-services
I'm addressing hundreds of technical review comments on the book as we speak to get it to OR's production team this month.
5
1
u/Routine_Term4750 Sep 13 '24
Link doesnāt take me to the book. Just OāReilly app
2
u/aliparpar Sep 13 '24
Itāll be available as physical print in May 2025. You can read 6 early release chapters on the platform if you canāt wait that long.
1
Sep 14 '24
I don't understand why O'Reilly gives me a 403 because my membership has ended. Can I atleast see the book cover and description? I need to logout to be able to see that page at all.
Looks cool though looking forward to checking it out!
1
u/aliparpar Sep 14 '24 edited Sep 14 '24
Yeah thatās weird. Let me paste the description here:
Ready to build applications using generative AI? This practical book outlines the process necessary to design and build production grade AI services with a FastAPI web server that communicate seamlessly with databases and external APIs. Youāll learn how to develop autonomous generative AI agents that stream outputs in real-time and interact with other models.
Web developers, data scientists, and DevOps engineers will learn to implement end-to-end production-ready services that leverage generative AI.
Youāll learn design patterns to manage software complexity, implement FastAPI lifespan for AI model integration, handle long-running generative tasks, perform content filtering, cache outputs, implement retrieval augmented generation (RAG) with a vector database, implement usage/cost monitoring and tracking, protect services with your own authentication and authorization mechanisms, and effectively control stream outputs directly from GenAI models.
Youāll explore efficient testing methods for AI outputs, validation against databases, and deployment patterns using Docker for robust microservices in the cloud.
Build generative services that interact with databases, external APIs, and more
Learn how to load AI models into a FastAPI lifecycle memory
Monitor and log model requests and responses within services
Use authentication and authorization patterns hooked with generative models
Handle and cache long-running inference tasks
Stream model outputs via streaming events and WebSockets into browsers or files
Automate the retraining process of generative models by exposing event-driven endpoints
ā
Brief Table of Contents (Not Yet Final)
Introduction
Why Generative AI services will power future applications
Facilitating the creative process
Suggesting contextually relevant solutions
Personalizing the user experience
Minimizing delay in resolving customer queries
Acting as an interface to complex systems
Automating Manual Back Office Tasks
Scaling and democratizing content generation
What prevents the adoption of generative AI services
Making generative services autonomous
Why build generative AI services with FastAPI
Overview of the Capstone Project
Summary
Getting Started with FastAPI
Introduction to FastAPI
FastAPI Features and Advantages
FastAPI Limitations
Comparing FastAPI to other web frameworks
Setting up your development environment
Installing Python, FastAPI and required packages
Setting up tooling with IDEs
Creating a simple FastAPI web server
Building larger FastAPI applications
FastAPI project structures
Progressive Re-organization of your FastAPI project
Onion / Layered Architecture
Migrating to FastAPI
Migrating from Django
Migrating from Flask
Migrating from other web frameworks
Summary
AI Integration and Model Serving
Serving Generative Models
Language Models
Audio Models
Vision Models
Video Models
3D Models
Strategies for serving generative AI models
Model swapping on every request
Using FastAPI application lifespan to preload models
Serving Models Externally
The role of middlewares in service monitoring
Summary
References
Implementing Type Safe AI Services
Introduction to Type Safety
Why do people prefer to skip type-safety?
Implementing Type Safety
Type Annotations
Dataclasses
Pydantic Models
How to use Pydantic
Compound Pydantic Models
Field Constraints and Validators
Custom Field and Model Validators
Computed Fields
Model Export and Serialization
Parsing environment variables with Pydantic
Dataclasses or Pydantic models in FastAPI
Summary
Achieving Concurrency in AI Workloads
Optimizing GenAI services for multiple users
Optimizing for I/O Tasks with Asynchronous Programming
Synchronous vs. Asynchronous (Async) Execution
Async Programming with model provider APIs
Event Loop and Thread Pool in FastAPI
Blocking the main server
Project: Web Page Scraper
Project: Retrieval Augmented Generation
Optimizing Model Serving for Memory and Compute-Bound AI Inference Tasks
Externalizing Model Serving
Managing long-running AI inference tasks
Conclusion
References
Real-Time Communication with Generative Models
Web Communication Mechanisms
Regular / Short Polling
Long Polling
Server Sent Events (SSE)
Web Sockets (WS)
Comparing Communication Mechanisms
Implementing Server-Sent Events (SSE) Endpoints
SSE with POST Request
Implementing WebSockets (WS) Endpoints
Streaming LLM Outputs with WebSockets
Handling WebSocket Exceptions
Designing APIs for streaming
Conclusion
(Detailed outline coming soon)
7. Integrating AI services with Databases
8. Authentication & Authorization
9. Testing AI Services
10. Security, Optimization, and Deployment
11. Future Trends
ā
3
u/LuckyNumber-Bot Sep 14 '24
All the numbers in your comment added up to 69. Congrats!
1 + 2 + 3 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 = 69Click here to have me scan all your future comments.
Summon me on specific comments with u/LuckyNumber-Bot.
1
u/dcbrown73 Jun 09 '25
Maybe it's just me, but I'm been going over the exercises in this book and I'm quite disappointed in it.
The book's structure is terrible and it's difficult to follow. It's like O'Reilly didn't proof this book or something.
It has code blocks in the book as you would expect, but it uses a single code block to display multiple files in that same code block. Sort of like when you have multiple YAML files in a single file. I
It will snip out code from those blocks for brevity which is understandable. The problem is, you must go back and reconcile everything before you continue or face troubleshooting when trying to run the code.
I'm in section 5 now and it supplies code to review for the application. In that code it imports functions from a module called schemas, except this code is not referenced anywhere. Talk about stomping on the breaks while you are trying to learn something.
The code base in section 5 provides code for dependencies.py, except other files (main) it imports assets (construct_prompt) from it that aren't in the dependencies.py code provided.
I found the git repo that the book comes with and while I did end up finding the schemas.py code under "app" section of the git repo. Dependencies.py is not there.
Here is what I'm referencing: This code has main.py and dependencies.py in one code block (file in this case) and you can see main import from dependencies, but what it is importing does not exist in dependencies.py
https://github.com/Ali-Parandeh/building-generative-ai-services/blob/main/examples/ch05/5-7.py
This is just not helpful and leads to so many road blocks in the learning process. I seriously hope they update this book and clean it up. I think there is real value in a book like this, but this is just not that.
2
u/aliparpar Aug 03 '25
Hey u/dcbrown73
Thanks for your feedback, it's fair and appreciated. I just saw this now.
You're right to expect clarity, especially in a technical book where the aim is to learn by doing. Let me respond to your main points directly:
On the bookās structure: the chapters are intentionally organised around building progressively more complete applications, with each chapter scoped to a specific concept (like model integration, model type safety, or real-time streaming). Itās not meant to be a linearly cumulative codebase, but rather a set of focused, self-contained examples. That said, I completely understand how this can feel fragmented if youāre trying to run everything as one continuous app, and thatās something Iāll make clearer in the repo and future updates. I've tried to provide branches in the book's accompanying codebase so in case the reader get lost, they can quickly switch to a checkpoint at start and end of a chapter to follow along. I will also be provided a complete app version at each checkpoint instead of just single code block examples.
You're absolutely right about the code blocks combining multiple files like
main.pyanddependencies.py. That was intended to show the complete picture of how components connect in a single listing, but I see now that it creates confusion, especially when some places I've missed adding all the imports for brevity or by mistake. The example you flagged in 5-7.py clearly has that issue. It was a huge undertaking showcasing an application build up one block at a time from scratch for sure across chapters.The good news is Iām adding updates to the GitHub repo and have access to the digital version of the book on the O'Reilly platform for any errata. I
Thanks again for taking the time to explain the issues. I take this seriously and will sort any issues.
1
u/Batteredcode Dec 18 '25
the code still doesn't match the book content as the above user described
1
u/aliparpar Dec 24 '25
Sorry for delay. Iāll spend some time this January and do a revision on the GitHub codebase.
3
u/_Garebear Sep 13 '24
I might be one of the few that still loves reading books to learn āŗļø.
Thanks for the heads up! Looking forward to reading!
2
u/aniketmaurya Sep 13 '24
This is great! FastAPI helped us build a high performance LLM serving engine - LitServe
2
2
2
2
u/singlebit Sep 15 '24
!remindme 9 months
1
u/RemindMeBot Sep 15 '24 edited Oct 11 '24
I will be messaging you in 9 months on 2025-06-15 02:12:14 UTC to remind you of this link
2 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback
2
-6
2
u/jkail1011 Sep 13 '24
Nice!
Iāll take a look and share my repoās as well.