1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
37 small wins to finish your pathNext lesson →
Messages
A conversation is a list. Each item says who spoke and what they said. That is all a message is, and it is the only new idea in this lesson.
Up to now your state has held strings you invented, like ticket and reply. From here it holds a conversation, because that is what you hand a model.
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
chat = [
SystemMessage("You are a polite support agent."),
HumanMessage("I was charged twice"),
AIMessage("Thanks, I will look into that."),
]
for m in chat:
print(f"{m.type:<6} {m.content}")The three you will use most
| Type | Who it is | When you write it |
|---|---|---|
SystemMessage | The instructions | Once, at the front. It tells the model how to behave. |
HumanMessage | The person | Every time a user says something. |
AIMessage | The model | You rarely write these. The model gives them to you. |
Every message has a type, which is the short name printed above, and content, which is what was said. There are more fields, and one of them becomes important in lesson 17, but you do not need it yet.
There is a fourth type for the answer a tool gives back. It arrives in lesson 18, once there is a tool to give it.
Why the import says langchain
These come from
langchain_core, not from LangGraph. LangGraph is the part that runs your graph. The message types, the models and the tools all come from LangChain, which is the layer underneath. You installed both when you installed LangGraph.Try it yourself
- Add a second
HumanMessageand a secondAIMessage, so the list reads as a real back and forth. - Print
m.typeon its own and see the short names. - Try
print(chat[1])and look at everything a message actually carries.
You understood something today that you didn't yesterday.