Exercise 2 of 10 · 0 passed
Structured Outputs with Pydantic
Raw LLM text is unpredictable. Structured outputs give you typed, validated Python objects.
The pattern:
- Define a Pydantic model for the expected shape
- Instruct the LLM to respond with JSON matching that shape
- Parse with
Model.model_validate_json(response)
Why care? This is how production AI systems stay reliable. Instead of parsing strings, you get objects with guaranteed types and validation errors when the model goes off-script.
Task
Complete classify_expense(description, client) to return an ExpenseResult.
The ExpenseResult model and CATEGORIES list are already defined. Your system prompt must:
- Tell the model the allowed categories
- Request JSON with the shape
{"category": "...", "confidence": 0.0, "reason": "..."}
Click "Run tests". First time loads Python in your browser (~5s). After that, runs are instant on this page and ~1s on the others.
Loading tests…
Pass the tests to reveal the reference solution.
The point of the exercise is the struggle, not the answer. Once your tests are green, the reference solution unlocks here so you can compare patterns.
Loading solution…
That's the warm-up.
You ran ten exercises against a mocked SDK. The cohort builds the real thing: an AI agent shipped to production with 150+ passing tests, Telegram bot, FastAPI, Streamlit, Docker. Six weeks. Every PR reviewed.