Sub topic 5
Putting Examples into Chat Messages
This chapter assumes you know what a few-shot prompt is, can read short Python, and understand the system, user, and assistant roles in chat-style prompts. Here you will turn examples into a message list that a chat model reads as a real conversation.
Why chat format changes how you write examples
A plain-text prompt puts every example in one block, with labels such as Review: and Sentiment: marking each part. Chat models take a list of messages instead. Each message has a role and some content. Models are trained on conversations in this shape, so you can use the roles to separate the parts of an example.
The usual pattern is:
- The system message holds the instruction.
- Each example becomes a user message (the input) followed by an assistant message (the desired output).
- The real query goes in a final user message.
The model then sees earlier turns in which "the assistant" already answered the same way you want it to answer now. It continues that pattern.
Building the message list
This function builds the list from a system instruction, a list of example pairs, and a query.
SYSTEM = "Classify the sentiment of each review as positive, negative, or neutral. Answer with one word."
examples = [
("The battery lasts all day and charging is fast.", "positive"),
("It stopped working after a week.", "negative"),
("The package arrived on Tuesday.", "neutral"),
]
def build_messages(examples, query):
messages = [{"role": "system", "content": SYSTEM}]
for text, label in examples:
messages.append({"role": "user", "content": text})
messages.append({"role": "assistant", "content": label})
messages.append({"role": "user", "content": query})
return messages
if __name__ == "__main__":
msgs = build_messages(examples, "The screen is bright but the speakers are weak.")
for m in msgs:
print(m["role"], "->", m["content"])The output has eight messages: one system, three user/assistant pairs, and the final user message. The list ends on a user turn, so the model's next turn is the assistant's answer.
What goes in each message
Keep the content of each message to the data only. The role already says who is speaking, so you do not need Review: or Sentiment: labels. Put the output in the assistant message exactly as you want to receive it. If you want one lowercase word, write one lowercase word.
Compare the two styles:
| Aspect | Plain-text prompt | Chat messages |
|---|---|---|
| Where the instruction goes | Top of the text | System message |
| How input and output are separated | Labels and blank lines | User and assistant roles |
| How the model's answer starts | After the final label | In a new assistant turn |
| Risk of the model continuing with extra examples | Higher | Lower, because the turn ends after one answer |
Common errors
- Examples that break the pattern. If one assistant message says
positiveand another saysPositive., the model sees two formats and may mix them. Keep every assistant message identical in style. - Chatty example answers. If an assistant example says "Sure! The sentiment is positive," the model will reply that way. Show only the output you want.
- Putting examples in the system message. This works, but the model does not see them as past answers. Role-separated turns usually steer the output format more reliably.
- Two user messages in a row. Some APIs reject or merge consecutive messages with the same role. Always alternate user and assistant.
- Forgetting the final user turn. If the list ends on an assistant message, the model has nothing new to answer.
Checking your result
Print the list before you send it, and read it as if you were the model. The system message should state the task, each pair should look alike, and the last message should be the new query. A malformed list is a common cause of poor results, and it is easier to catch by reading than by testing.
Exercise
You build a few-shot chat prompt with four examples, but your list is ordered system, user, user, assistant, assistant, user, assistant, user. Name the problem and describe the fix.
Show answer
The example inputs and outputs are not alternating. Two user messages come in a row, then two assistant messages, so the model cannot tell which answer belongs to which input, and some APIs reject the list. The fix is to emit each example as a user message immediately followed by its assistant message, as build_messages does, and then end with a single user message holding the query.