Building the Message Array

Message array

Part of: Chat Roles & Messages

Every chatbot you've ever used is, at its core, one growing Python list and a loop that pokes it. There's no secret sauce, just "start with a system message, append a user turn, get a reply, append the reply, repeat." Once you can build that list correctly, you can build a chatbot. This lesson is where the previous three click into running code. What it is The message array is the list of message dicts you assemble in code and send to the model. Building it well comes down to three rules: 1. Start with the system message (if you have one), it goes first. 2. Append turns in order : each user message, then each assistant reply, in the sequence they happened. 3. Keep it a flat, ordered list : no nesting, no shuffling; sequence carries the meaning. How it works You maintain one list and grow it. A tiny helper keeps the appends clean: That's the entire pattern. A real chat loop just wraps it: read user input, append a user message, call the model, append the returned assistant message, loop. The list is the single source of truth for the conversation, and you send the whole list every time (from the multi-turn lesson). Two rules people break: putting the system message not first, and fo

Challenge: Sliding-Window Array Assembler