Messages
Messages are the objects a chat model reads and writes. The four you use most are HumanMessage, AIMessage, SystemMessage and ToolMessage.
Last updated: 29 Sep, 2026 · LangChain 1.4
A conversation is a list of messages. Each one has a type (its role) and content. The model is given the list and returns the next message.
The LangChain section of the video separates two kinds of input. A plain string, such as model.invoke("what is langchain"), is a text prompt: fine for one request with no history. A list of message objects is a message prompt, which can tell the model how to behave and carry a conversation. The video's first list has a system message, "You are a poetry expert", and a human message asking for a poem on artificial intelligence; the poem comes back as an AI message. The video loads groq:qwen/qwen3-32b, since retired, so the code below uses the course model, loaded with init_chat_model, which Chat models covers:
from langchain.chat_models import init_chat_model
from langchain.messages import SystemMessage, HumanMessage, AIMessage
model = init_chat_model("groq:openai/gpt-oss-120b") # the model from the setup lesson
messages = [
SystemMessage("You are a poetry expert"),
HumanMessage("Write a poem on artificial intelligence"),
]
response = model.invoke(messages)
print(response.content)A graph passes the same kind of list from node to node. The support conversation below is built from the same classes and printed one message at a time.
The message-type imports
from langchain.messages import HumanMessage, AIMessage, SystemMessage, ToolMessageA conversation as a list
Start by importing the two message types you need and building a conversation. A conversation is a plain Python list of message objects.
from langchain.messages import SystemMessage, HumanMessage
conversation = [
SystemMessage("You are a support assistant."), # your instructions to the model
HumanMessage("I was charged twice."), # the user's turn
]Reading role and content
Walk the list and print each one. type(m).__name__ is the message's class name, which is its role, and m.content is its text.
for m in conversation:
print(type(m).__name__, "->", m.content) # role, then textPrinting the conversation
The two snippets together:
from langchain.messages import SystemMessage, HumanMessage
conversation = [
SystemMessage("You are a support assistant."),
HumanMessage("I was charged twice."),
]
for m in conversation:
print(type(m).__name__, "->", m.content)SystemMessage -> You are a support assistant. HumanMessage -> I was charged twice.
What each message carries
- Each message is an object with a role (its class) and
content. - A
SystemMessagesets instructions; aHumanMessageis the user's turn; the model replies with anAIMessage. - A
ToolMessagecarries a tool's result back to the model and must settool_call_idto match the call it answers.
The four message types
| Type | Who it is from | Key fields |
|---|---|---|
SystemMessage | You, as instructions | content |
HumanMessage | The user | content |
AIMessage | The model | content, tool_calls |
ToolMessage | A tool's result | content, tool_call_id |
Where messages appear
- Every model call takes a list of messages and returns one.
- The conversation your agent remembers is a list of these objects.
from langchain.messages import .... Older code uses from langchain_core.messages import ..., which still works.Related
- Previous: Subgraphs
- Next: MessagesState and add_messages
- Reference: Messages
- Add an
AIMessage("How can I help?")to the list and print it. - Give a
HumanMessageanameand print it.
This is what real progress feels like.