Learn Everything

Paths/AI Engineer/Python / Testing & Typing Your AI Code

Guided Taskbeginnerubuntu:24.04Ask AI

Type Your Message Models

A chat message is a small, fixed shape - and the shape is exactly where the bugs hide. Model it as a dataclass with type hints.

Validator

⏱ 0:00

IDLE — checks run live as you type commands; submit when done

01Requirements

live checks
  • Directory ~/ai-lab/src exists○
  • File ~/ai-lab/src/messages.py exists○
  • The file defines a dataclass with typed fields○

02Walkthrough

4 steps
  1. 01

    Create the source directory

    Keep the library code separate from the tests - src layout makes imports unambiguous.

    mkdir -p ~/ai-lab/src
  2. 02

    Write the message model

    A dataclass generates the constructor for you. The role is a str and the content is a str - say so in the annotations.

    echo 'from dataclasses import dataclass\n\n@dataclass\nclass Message:\n role: str\n content: str' > ~/ai-lab/src/messages.py
  3. 03

    Add a helper function

    The arrow annotation tells a reader - and your editor - what the function returns. Typing is documentation that cannot go stale.

    echo '\ndef user_message(text: str) -> Message:\n return Message(role="user", content=text)' >> ~/ai-lab/src/messages.py
  4. 04

    Verify and submit

    cat the module, then submit for validation.

    cat ~/ai-lab/src/messages.py

03Stuck?

0 of 3 revealed

Take a hint without losing the lab — each one points at the thing to look at, not at the answer.

04AI Tutor

guides, never solves

Tell me what you're stuck on — I'll nudge, not solve. Which requirement are you looking at?

04Notebook

saved locally in this browser

learner@lab — ubuntu:24.04

↑/↓ recalls commands · Ctrl+Enter submits · type help for the command list