LangChain Messages API


All Message Types

TyperoletypeKey AttributesDescription
HumanMessageuserhumancontentUser Message
AIMessageassistantaicontent, tool_calls, usage_metadataAI Reply
AIMessageChunkassistantaicontent (incremental)Token chunk of streaming output
SystemMessagesystemsystemcontentSystem Instruction
ToolMessagetooltoolcontent, tool_call_id, nameTool Execution Result
RemoveMessage-removeidDelete Specified Message

ContentBlock Types (Multimodal Content)

TypeDescriptionUse Cases
PlainTextContentBlockPlain TextPlain text
ImageContentBlockImage (base64 or URL)Image input for multimodal models
AudioContentBlockAudioVoice input
VideoContentBlockVideoVideo input
FileContentBlockFileDocument input
ToolCallTool Call RequestModel requests to call a tool
ServerToolCallServer-side tool callBuilt-in/MCP tool call

Common Helper Functions

FunctionDescriptionSignature
trim_messages()Trim message history to fit context windowtrim_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,
)
Other Extensions