Chapter 15
Python and FastAPI for GenAI Services
Learning Objective
Learn how GenAI logic is commonly exposed as an API service.
What it means
Python is widely used for GenAI because most AI SDKs, orchestration frameworks, and data tools support it well. FastAPI is a popular framework for exposing AI functions as REST APIs.
Why it matters
Business applications rarely call notebooks directly. Production GenAI capabilities are usually packaged as APIs. This allows applications to send requests, receive structured responses, enforce security, and scale independently.
Healthcare Example
A clinical summarization API receives document text and returns JSON with summary, missing data, confidence, and review routing.
Code: FastAPI GenAI Service
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="Healthcare GenAI Service")
class SummaryRequest(BaseModel):
case_id: str
document_text: str
class SummaryResponse(BaseModel):
case_id: str
summary: str
confidence: float
route: str
@app.get("/health")
def health():
return {"status": "healthy"}
@app.post("/summarize", response_model=SummaryResponse)
def summarize(req: SummaryRequest):
summary = req.document_text[:300] + "..."
confidence = 0.88
route = "human_review" if confidence < 0.95 else "auto_process"
return SummaryResponse(case_id=req.case_id, summary=summary, confidence=confidence, route=route)Common Mistakes
- No request/response schema.
- No error handling.
- No input size limits.
- No authentication.
- No logging or trace ID.
Interview Q&A
Q: Why expose GenAI as an API?
A: APIs provide clean integration, security controls, scalability, versioning, and separation between business applications and AI services.
Q: Why FastAPI?
A: FastAPI is lightweight, supports type validation with Pydantic, auto-generates API documentation, and performs well for Python services.
Architect Takeaway
A GenAI prototype becomes production-ready when it is packaged behind clear APIs with validation, logging, security, and operational controls.