Messages: System, Human, AI
A chat is a list of messages. Each message has a role and some text.
The role says who is talking. The model reads the whole list and writes the next message.
Once you understand messages, every chat trick in LangChain makes sense.
The three roles
Think of a school play. Every line in the script has a name in front of it.
System: the director. Sets the rules before the play starts. The model follows these closely.
Human: you. The questions and requests you send.
AI: the model. What it said before, or what it says next.
Each class takes the text as its first argument. The list keeps them in order.
Sending a list of messages
invoke accepts a list of messages, not just a string.
The reply is an AIMessage. It is the same kind of object you would put in the list yourself.
When you pass a plain string, LangChain quietly wraps it in a HumanMessage for you.
The system message is the boss
The system message sets the mood for the whole chat. Change it and the same question gets a different answer.
Same question, two very different voices. Only the first message changed.
Tuple shorthand
Typing the class names gets old. LangChain also accepts small tuples of (role, text).
The roles are system, human and ai. LangChain turns each tuple into the right message class.
Building a conversation
The model has no memory. It only knows what is in the list you send.
To keep talking, you append the reply and your next question, then send the whole list again.
The model knows the name only because the first question is still in the list.
Delete that line and it will have no idea. This is the whole secret behind chat memory.
Looking inside a message
Every message has a type and a content. AIMessage adds extras like usage_metadata and tool_calls, which we meet later.
Remember: the model only sees the list you send. If you want it to remember, keep the old messages in the list.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.