The previous lecture defined your product API. FastAPI is this curriculum’s default way to implement it in Python: type hints, Pydantic validation, async, and free OpenAPI at /docs. You will wrap Module 18.1 SDKs (OpenAI first) so browsers never hold vendor keys.
Next: Flask as the older/simpler sibling, then Docker so this process is reproducible. Streaming and auth land later in the same module—design the app so those lectures plug in, not rewrite everything.
Learning Objectives
By the end of this lesson, students should be able to:
- Create a FastAPI app with Pydantic request/response models for chat.
- Call the OpenAI SDK from a route without leaking the SDK type to clients.
- Use
async def+ thread/async clients so one slow LLM does not block the event loop carelessly. - Read auto-generated OpenAPI and treat it as the contract artifact.
- Sketch SSE streaming (full treatment in the streaming lecture).
- Contrast FastAPI with Flask and know when each is enough.
FastAPI is a Python web framework built on Starlette (ASGI) and Pydantic. You declare path operations with decorators, annotate parameter types, and get validation, serialization, and OpenAPI for free. It runs under Uvicorn (or Gunicorn+Uvicorn workers). It is not a model server like vLLM/Triton and not Kubernetes—it is the HTTP process that will later sit in a container.
Why FastAPI for AI Wrappers
| Need | FastAPI feature |
|---|---|
| Strict JSON in/out | Pydantic models = runtime validation + docs |
| Many concurrent waits on vendor APIs | ASGI + async (still: do not block on sync SDK calls blindly) |
| Token streaming to browsers | StreamingResponse / SSE (later lecture) |
| Partner integrations | /docs OpenAPI as living contract |
Minimal Chat Wrapper
FastAPI vs Flask (preview)
FastAPI
- ASGI, async-native
- Pydantic + OpenAPI default
- Best default for new AI APIs
Flask
- WSGI, tiny, everywhere
- You add schema/docs yourself
- Fine for simple sync tools
Neither is
- A GPU scheduler
- A queue (see Celery/Redis)
- An auth protocol (see Authentication)
Do
- Return your DTOs, not raw SDK objects
- Map upstream failures to 502/504, validation to 422
- Keep secrets in env; inject clients via lifespan
Don’t
- Run CPU-heavy tokenization on the event loop without care
- Ship
--reloadin production - Expose
/docsunauthenticated on the public internet forever
Related Lectures
| Lecture | Role |
|---|---|
| API | The contract you just implemented |
| OpenAI SDK | Upstream client inside the route |
| Flask | WSGI alternative |
| Docker | Package Uvicorn next |
| Streaming / Auth | Plug into this app, don’t fork it |
“FastAPI is async so my OpenAI call is automatically non-blocking.” The official sync client still blocks a worker thread unless you use the async client or asyncio.to_thread. Second: Pydantic validation is not authentication. Third: /docs is not a substitute for a changelog when you break ChatOut. Fourth: FastAPI will not quantize a 70B model—that is Module 18.3.
Knowledge Check
- Short Answer: Which two libraries sit under FastAPI? Answer: Starlette (ASGI) and Pydantic.
- True/False: Uvicorn is a common ASGI server for FastAPI. Answer: True.
- Multiple Choice: Invalid JSON body typically becomes: (a) 422, (b) 301, (c) CFG 7. Answer: (a).
- Short Answer: Why use
response_model=ChatOut? Answer: Validate/serialize a stable DTO and document it in OpenAPI. - True/False: Returning the raw OpenAI response object is a good public contract. Answer: False.
- Multiple Choice: Upstream LLM outage should often be: (a) 502/504, (b) 204, (c) 418 only. Answer: (a).
- Short Answer: What command starts this app locally in the sketch? Answer: uvicorn main:app --reload --host 0.0.0.0 --port 8000 (or equivalent).
- True/False: FastAPI replaces Redis and Celery. Answer: False.
- Multiple Choice: Next lecture: (a) Flask, (b) FLUX, (c) PCA. Answer: (a).
- Short Answer: Name one reason FastAPI beats a notebook
requests.postfor products. Answer: Any of: validation, OpenAPI, status codes, middleware/auth, concurrency, deployment shape.
Key Takeaways
- FastAPI implements your API with Pydantic + OpenAPI + Uvicorn.
- Wrap SDKs; return DTOs; map errors honestly.
- Async is not magic—do not block the event loop on sync I/O.
- Auth, SSE, Docker, queues come next; keep the app small.
- Next: Flask.
Lab: Implement the sketch, hit /docs, then add a second provider stub (anthropic) behind the same ChatOut. Break the schema on purpose and show 422.
Whiteboard: Request → Pydantic → SDK → DTO. Mark where JWT will hook (Authentication) and where SSE will replace JSON (Streaming).
Recap: FastAPI is the default Python AI API. Compare the WSGI path next: Flask.