Sub topic 1
What a chat message is: role plus content
Prerequisites
This chapter assumes you know basic Python or JavaScript syntax. You should also be comfortable with lists, dictionaries (or arrays and objects), and JSON. You need a general idea of what a large language model is, and a basic sense of what it means to call an HTTP API or an SDK. You do not need an API key to follow along. The examples build data structures only.
The message as the unit of conversation
Chat-based language model APIs do not take one long string. They take a list of messages. Each message is a small record that says who is speaking and what they said.
A message has two fields:
roleis a string that names the speaker. The three core values aresystem,user, andassistant.contentis the text of the message.
In Python, a message is a dictionary:
message = {"role": "user", "content": "What is a REST API?"}A conversation is a list of these dictionaries, in the order the turns happened:
messages = [
{"role": "system", "content": "You are a concise assistant that answers in plain English."},
{"role": "user", "content": "What is a REST API?"},
{"role": "assistant", "content": "A REST API lets programs exchange data over HTTP using URLs and standard methods such as GET and POST."},
{"role": "user", "content": "Give me one example URL."},
]The list is the whole input. The model reads it from top to bottom and writes the next assistant message.
Why roles exist
The role tells the model how to treat each piece of text. The same sentence means different things depending on who said it.
| Role | Who it represents | Typical use |
|---|---|---|
| `system` | The developer | Sets behavior, tone, and constraints |
| `user` | The person using the application | Asks questions and supplies input |
| `assistant` | The model | Holds earlier replies, or example replies |
In the list above, the system message sets the style. The first user message asks a question. The assistant message is a reply that was already given. The last user message is the new input. The model answers that last message, and it can see everything before it.
Later chapters cover each role in detail. For now, remember that the role field is how you label who said what.
Reading the structure in JSON
Most chat APIs send messages as JSON over HTTP. The Python list above becomes this JSON array:
[
{"role": "system", "content": "You are a concise assistant that answers in plain English."},
{"role": "user", "content": "What is a REST API?"}
]SDKs such as the OpenAI and Anthropic Python packages build this JSON for you from your Python list. You work with dictionaries. The library handles the encoding.
Checking a message in code
Since a message is only a dictionary, mistakes are easy to make. A misspelled key or an unknown role often produces an API error, or a reply that ignores your intent. A small check helps while you learn the structure:
VALID_ROLES = {"system", "user", "assistant"}
def check_message(message):
if set(message) != {"role", "content"}:
raise ValueError("A message needs exactly the keys 'role' and 'content'.")
if message["role"] not in VALID_ROLES:
raise ValueError(f"Unknown role: {message['role']}")
if not isinstance(message["content"], str):
raise ValueError("Content must be a string here.")
check_message({"role": "user", "content": "Hello"})
print("Message is valid.")Common errors
- Wrong key names. Writing
"text"or"message"instead of"content"fails or is ignored. - Misspelled or invented roles. A value like
"bot"is not valid. Useassistantfor model replies. - Sending only the new question. The model sees only the messages in this request. Chapter by chapter you will see why, but the short version is that you must include earlier turns yourself.
- Putting everything in one user message. This works, but you lose the ability to separate instructions from input. Use a system message for rules.
Exercise
A teammate writes this message: {"role": "bot", "text": "Hi there"}. Name the two problems and write the corrected message for a model reply.
Show answer
The role "bot" is not a valid role, and the key "text" should be "content". The corrected message is {"role": "assistant", "content": "Hi there"}.
Exercise
Write a list of messages in which the developer tells the model to answer in one sentence, and the user asks "What is JSON?".
Show answer
messages = [{"role": "system", "content": "Answer in one sentence."}, {"role": "user", "content": "What is JSON?"}]