Exercise py-10 — the service one request, three doors draws

You build the FastAPI app that scene traces: POST /ask (JSON), GET /stream (SSE from a stub model), /healthz that never touches the model and /readyz that gates on it, all wired through an X-Request-Id middleware that puts the id in a ContextVar — the same idea as py-09's request_scope, now scoped to one asyncio task per HTTP request instead of one thread per CLI call. This exact service is what goes to Fargate on day 16 and becomes the RAG API later. It runs on your Mac rather than in the browser because Pyodide has no FastAPI, no real ASGI server, and no Docker.

~90 minruns locally · uv + pytest 9 checkspy-10-fastapi-service

What you're building

The tests are ordinary pytest and ship in the public folder with the starter — read them first; the names below are the check list. Solutions are not published.

Get the repo (once)

Needs git. uv installs the right Python itself, so nothing else is required.

# once, anywhere on your machine
git clone https://github.com/theDocWho/ai-ml-roadmap.git
cd ai-ml-roadmap

No git? Download the ZIP, unzip it, and cd into the unzipped folder instead.

Run it

From the repo root:

# one-time: uv (https://docs.astral.sh/uv/) manages the venv and pins Python ≥ 3.12
cd exercises/py-10-fastapi-service && uv sync && uv run pytest -q

# once the tests pass: talk to it for real
uv run uvicorn askapi.main:app --port 8000 &
curl -s -X POST localhost:8000/ask -H 'content-type: application/json' -d '{"prompt": "hi"}'
curl -sN localhost:8000/stream?prompt=hi
curl -si localhost:8000/readyz          # 503 for the first ~3 s — main.py load()s the stub on a timer, then 200

# the same bar the reference solution clears
uv run ruff check . && uv run mypy src

Done when uv run pytest -q prints 9 passed — or 8 passed, 1 skipped without a Docker daemon running (the one Docker test skips itself rather than failing when it can't reach one). A skip elsewhere is not a pass: only the Docker-build check gets that exception. The untouched starter fails all nine. The line also says 3 deselected: the live checks in integration/, which cloud-07 runs against the deployed service with BASE_URL=http://<alb-dns> uv run pytest -q -m integration — not part of the nine.

The checks

Files

This is self-attestation — the site cannot see your terminal, so the box and the button are you telling The Path the suite went green on your machine.

If you get stuck