LangChain Messages
LangChain messages are the units of a conversation. Each message has a role (system, human, AI or tool) and content. Chat models read the whole list in order and reply with a new AIMessage.
Message Types
- SystemMessage: instructions that set the behaviour of the assistant
- HumanMessage: what the user says
- AIMessage: what the model replied, possibly including tool calls
- ToolMessage: the result of a tool call, sent back to the model
Sending a List of Messages
from langchain_ollama import ChatOllama
from langchain_core.messages import SystemMessage, HumanMessage
llm = ChatOllama(model="llama3.2", temperature=0)
messages = [
SystemMessage("You are a pirate. Answer in one short sentence."),
HumanMessage("What is the weather like today?"),
]
print(llm.invoke(messages).content)Sample output: Arr, the skies be clear and the winds be fair, matey!
The system message steers tone and rules. Models give it high priority, so it is the right place for persona, format requirements and safety rules.
Shorthand Formats
You do not always need the classes. LangChain also accepts tuples or dictionaries with role and content:
reply = llm.invoke([
("system", "You translate English to French."),
("human", "Good morning"),
])
print(reply.content)
reply = llm.invoke([{"role": "user", "content": "Say hi in Spanish"}])
print(reply.content)Sample output: Bonjour Hola
Building a Multi-Turn Conversation
Models are stateless: they only know what you send. To continue a conversation, append the AI reply and the next human message, then send the whole list again.
history = [SystemMessage("You are a helpful math tutor.")]
history.append(HumanMessage("What is 15% of 80?"))
ai = llm.invoke(history)
history.append(ai)
history.append(HumanMessage("And 20% of the same number?"))
print(llm.invoke(history).content)Sample output: 20% of 80 is 16.
The model understood "the same number" only because the earlier turns were included. The chat history lesson shows how to manage this automatically.
Inspecting an AIMessage
Besides content, an AIMessage carries tool_calls (requested tool calls), usage_metadata (token counts), response_metadata (model name, stop reason) and content_blocks, a provider-neutral list of text, reasoning and tool-call blocks introduced in LangChain 1.0.
Frequently asked questions
Does a chat model remember previous messages?
No. Each call is independent. You must send the earlier messages again, or use a checkpointer or chat history helper that does it for you.
Can I have more than one system message?
Usually one system message at the start works best. Some providers reject or merge additional system messages.
What is content_blocks in LangChain?
It is a standard list of typed blocks (text, reasoning, tool_call, image and so on) that looks the same regardless of provider, making it easier to handle multimodal and reasoning models.