Module 2: FastAPI Setup, Starlette & Pydantic
Understand FastAPI philosophy. Inspect the roles of Starlette routing web engines and Pydantic validation schemas in launching first endpoints.
FastAPI Philosophy & Dependency Stack
Why this matters
FastAPI Stack: FastAPI on Starlette/ASGI gives speed, automatic OpenAPI docs, and Pydantic validation out of the box.
FastAPI is built on Starlette (ASGI) and uses Pydantic for validation. It generates OpenAPI/Swagger UI automatically.
| WSGI (Flask) | ASGI (FastAPI) |
|---|---|
| Synchronous request model | Async-native; better concurrency |
| Manual schema docs | Auto OpenAPI from type hints |
Stack: Python type hints → Pydantic models → route handlers → uvicorn server.
Common mistakes
- Blocking the event loop with heavy sync code in async routes.
- Returning wrong HTTP status codes (200 on validation failure).
- Shipping without request/response models for ML endpoints.
Interview checkpoints
- Q: Explain fastapi stack in one minute. A: Definition + ML deployment angle.
- Q: One FastAPI pitfall? A: Validation, async blocking, or wrong status code.
Practice
- Basic: Define FastAPI Stack and give an example.
- Intermediate: Implement a minimal snippet for FastAPI Stack.
- Advanced: Break it and read the OpenAPI / error response.
Recap
- You can explain fastapi stack clearly.
- You know one mistake to avoid.
- You see how this connects to the next lesson.
Next: First Endpoint
Writing and Launching Your First API
Why this matters
First Endpoint: Your first route proves the dev loop: define handler → run uvicorn → hit /docs.
Every FastAPI app starts with an application instance and route decorators. Use /docs for interactive testing.
from fastapi import FastAPI
app = FastAPI(title="My ML Service")
@app.get("/health")
def health():
return {"status": "ok"}
# Run: uvicorn main:app --reload --port 8000Dev loop
Edit code → uvicorn reloads → Swagger UI reflects new routes immediately.
Common mistakes
- Blocking the event loop with heavy sync code in async routes.
- Returning wrong HTTP status codes (200 on validation failure).
- Shipping without request/response models for ML endpoints.
Interview checkpoints
- Q: Explain first endpoint in one minute. A: Definition + ML deployment angle.
- Q: One FastAPI pitfall? A: Validation, async blocking, or wrong status code.
Practice
- Basic: Define First Endpoint and give an example.
- Intermediate: Implement a minimal snippet for First Endpoint.
- Advanced: Break it and read the OpenAPI / error response.
Recap
- You can explain first endpoint clearly.
- You know one mistake to avoid.
- You see how this connects to the next lesson.
Next: HTTP CRUD
