Exercise py-09 — askcli: JSON-lines logging, a request id, and env > file > default

Every service you deploy on this track starts with the same three pieces of scaffolding before any model code: a command line with honest exit codes, logs a machine can parse — one JSON object per line, each stamped with the request it belongs to — and a config loader where the environment beats the file and the file beats the defaults. In Spring Boot those come with the starter; in Python you write them once, here, in a package called askcli, and flagship F1 reuses them. It runs on your Mac with uv: the concurrency check starts real threads.

~90 minruns locally · uv + pytest 7 checkspy-09-cli-logging

What you're building

src/askcli/backend.py (the stand-in model, which logs model call: … through the child logger askcli.backend) and src/askcli/__init__.py are provided. The backend never receives a request id, yet its line must carry the right one — that is the whole reason the id lives in a ContextVar the formatter reads, and not in an argument or a global.

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-09-cli-logging && uv sync && uv run pytest -q

# the real thing, once the tests pass: uv sync installed an `askcli` launcher
uv run askcli ask "what is a logger tree?" --json
ASKCLI_MODEL_NAME=gpt-mini uv run askcli ask "hi"
uv run askcli ask; echo "exit code $?"

Done when uv run pytest -q prints 7 passed. The untouched starter fails all seven.

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