LangChain/Core Concepts
Beginner8 min

Messages

Messages are the fundamental unit of context in LangChain. HumanMessage, AIMessage, SystemMessage, and ToolMessage carry content, tool calls, and metadata through every model interaction.

Quick Reference

  • Four types: SystemMessage, HumanMessage, AIMessage, ToolMessage
  • model.invoke('text') works — LangChain wraps strings in HumanMessage automatically
  • AIMessage has .content, .tool_calls, .usage_metadata, .content_blocks
  • ToolMessage.tool_call_id must match the AIMessage tool call ID
  • Pass dict format: {'role': 'user', 'content': '...'} — equivalent to HumanMessage

Message Types

Every model interaction in LangChain uses messages. A conversation is a list of messages passed to model.invoke(). LangChain defines four core message types that map to the roles all providers support.

TypeRoleWhen to use
SystemMessagesystemPrime model behavior — instructions, persona, constraints
HumanMessageuserUser input — text, images, audio, files
AIMessageassistantModel response — returned by model.invoke(); also insert manually for history
ToolMessagetoolTool execution result — pass back to model after calling a tool
Basic message usage