LangChain Messages API
All Message Types
| Type | role | type | Key Attributes | Description |
|---|---|---|---|---|
| HumanMessage | user | human | content | User Message |
| AIMessage | assistant | ai | content, tool_calls, usage_metadata | AI Reply |
| AIMessageChunk | assistant | ai | content (incremental) | Token chunk of streaming output |
| SystemMessage | system | system | content | System Instruction |
| ToolMessage | tool | tool | content, tool_call_id, name | Tool Execution Result |
| RemoveMessage | - | remove | id | Delete Specified Message |
ContentBlock Types (Multimodal Content)
| Type | Description | Use Cases |
|---|---|---|
| PlainTextContentBlock | Plain Text | Plain text |
| ImageContentBlock | Image (base64 or URL) | Image input for multimodal models |
| AudioContentBlock | Audio | Voice input |
| VideoContentBlock | Video | Video input |
| FileContentBlock | File | Document input |
| ToolCall | Tool Call Request | Model requests to call a tool |
| ServerToolCall | Server-side tool call | Built-in/MCP tool call |
Common Helper Functions
| Function | Description | Signature |
|---|---|---|
| trim_messages() | Trim message history to fit context window | trim_messages(messages, *, max_tokens, strategy, token_counter, include_system, start_on) |
Common Usage Examples
Examples
from langchain.messages import (
HumanMessage, AIMessage, SystemMessage, ToolMessage, trim_messages
)
# Create message
human = HumanMessage(content="Hello")
system = SystemMessage(content="You are an assistant")
ai = AIMessage(content="Hello! How can I help you?")
tool = ToolMessage(content="Result", tool_call_id="call_1", name="my_tool")
# Shortcut
msg1 = ("user", "Hello") # Tuple
msg2 = {"role": "user", "content": "Hello"} # Dictionary
# Message attributes
print(human.type) # human
print(human.content) # Hello
print(ai.tool_calls) # [] or [ToolCall]
# Trim messages
trimmed = trim_messages(
messages, max_tokens=1000, strategy="last",
token_counter=model, include_system=True,
)
HumanMessage, AIMessage, SystemMessage, ToolMessage, trim_messages
)
# Create message
human = HumanMessage(content="Hello")
system = SystemMessage(content="You are an assistant")
ai = AIMessage(content="Hello! How can I help you?")
tool = ToolMessage(content="Result", tool_call_id="call_1", name="my_tool")
# Shortcut
msg1 = ("user", "Hello") # Tuple
msg2 = {"role": "user", "content": "Hello"} # Dictionary
# Message attributes
print(human.type) # human
print(human.content) # Hello
print(ai.tool_calls) # [] or [ToolCall]
# Trim messages
trimmed = trim_messages(
messages, max_tokens=1000, strategy="last",
token_counter=model, include_system=True,
)