AI Engineer Portal
Your personal operating system for career transition.
Private mode
Exercise
Multi-turn conversation builder with JSON serialization
Multi-turn conversation builder with JSON serialization
A conversation is a list of messages that must be assembled correctly and serialized for persistence. This exercise builds the foundation for multi-turn state management.
What you are building
Implement a Conversation class that manages a list of message dicts:
Methods:
add_system(content: str) -> None— Add (or replace) the system message. The system message is always first.add_user(content: str) -> None— Append a user message.add_assistant(content: str) -> None— Append an assistant message.to_messages() -> list[dict]— Return the full messages list in order: system (if set), then user/assistant turns.to_json() -> str— Serialize to a JSON string.from_json(json_str: str) -> "Conversation"(classmethod) — Deserialize from a JSON string.token_estimate() -> int— Return a rough token estimate:sum(len(m["content"].split()) * 1.3)for all messages.last_role() -> str | None— Return the role of the last message, or None if empty.
Requirements
- A conversation can only have one system message (replacing it overwrites the previous one)
to_messages()must always put the system message first, regardless of whenadd_systemwas called- Maintain insertion order for non-system messages
Prompt Formatting / easy / Step 5 of 5
Practice stage
Prompt boundary discipline
Hint
Treat prompt construction like request composition: trusted instructions, untrusted user input, and context blocks should stay separate.
Next drill
This is the end of the current mini-sequence.
Success criteria
- - Separates system, user, and context cleanly
- - Avoids string chaos and hidden assumptions
- - Would scale to a real LLM feature
Review checklist
- - Did I preserve trusted versus untrusted boundaries?
- - Would this format survive longer prompts and more context?
- - Can another engineer review the structure quickly?
Practice
Generate a variation
Generate a new exercise variation to deepen understanding or practice a related concept.
Attempt history
Recent submissions
Before you submit, decide what a strong answer should make obvious to the reviewer.
No attempts yet.