Sub topic 5
Assistant Messages: The Model's Replies
This chapter assumes you can read basic Python, work with lists and dictionaries, and have called a chat API or SDK at least once. It covers the assistant role: what it contains, how you read it, and how you send it back.
What the assistant role is
The assistant role marks text the model wrote. Every message in the list has a role and content. When the role is assistant, the content is a reply from the model, not from you.
You get an assistant message back from every API call. You also write assistant messages yourself in two cases: when you replay earlier replies as history, and when you invent example replies for few-shot prompts. Both cases come up in later chapters. Here the focus is on reading a reply and putting it back in the list correctly.
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 third message is an assistant message. It sits between two user messages, which shows that the model already answered the first question.
Reading the reply from an API response
The SDK does not hand you a bare string. It returns a response object, and the assistant message is nested inside it. The location differs by provider.
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="MODEL_NAME", # replace with a chat model available to you
messages=[{"role": "user", "content": "What is a list in Python?"}],
)
reply = response.choices[0].message.content
print(reply)In OpenAI-style responses, choices is a list because you can request more than one completion. Most calls return one, so you read index 0. The message object has role set to "assistant" and a content string.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="MODEL_NAME", # replace with a model available to you
max_tokens=300,
system="You are a helpful assistant. Keep answers under three sentences.",
messages=[{"role": "user", "content": "What is a list in Python?"}],
)
print(response.content[0].text)Anthropic-style responses put a list of content blocks in response.content. For plain text replies, the first block holds the text in its .text attribute.
Sending the reply back
The API stores nothing between calls. If you want the model to remember its own answer, you must append it to your history as an assistant message before the next request.
from openai import OpenAI
client = OpenAI()
history = [
{"role": "system", "content": "You are a helpful assistant. Keep answers under three sentences."}
]
def chat(user_text):
history.append({"role": "user", "content": user_text})
response = client.chat.completions.create(
model="MODEL_NAME", # replace with a chat model available to you
messages=history,
)
reply = response.choices[0].message.content
history.append({"role": "assistant", "content": reply})
return reply
print(chat("What is a list in Python?"))
print(chat("How do I add an item to it?"))The line history.append({"role": "assistant", "content": reply}) is the important one. Without it, the second call would contain only the system message and the new question. The model would have no idea what "it" refers to.
Common errors
- Forgetting to append the reply. The model seems to forget the previous answer. The fix is to add the assistant message to the list every turn.
- Appending the whole response object. The list expects a dictionary with
roleandcontent. Extract the text first. - Reading the wrong field.
response.choices[0].message.contentworks for OpenAI-style APIs. The same path fails on Anthropic-style APIs, which useresponse.content[0].text. - Assuming the content is always text. A reply can be empty or cut off. If the token limit is reached, the text ends mid-sentence. Check the finish reason or stop reason in the response when completeness matters.
- Editing replies without realizing it. The model treats anything labeled
assistantas something it said. Changing earlier replies changes how it behaves next.
Exercise
A chat function returns the model's reply but never adds it to the history list. The user asks "What is a list in Python?" and then "How do I add an item to it?". Why does the second answer fail, and what one line fixes it?
Show answer
The API is stateless, so the second request contains the first user question but not the model's earlier answer. The model cannot tell what "it" refers to. Adding history.append({"role": "assistant", "content": reply}) after each call fixes it.