You are viewing the v1 docs for LangChain, which is currently under active development. Learn more.
Overview
Messages are the fundamental unit of context for models in LangChain. They represent the input and output of models, carrying both the content and metadata needed to represent the state of a conversation when interacting with an LLM. Messages are objects that contain:- Role - Identifies the
message type (e.g.
system
,user
) - Content - Represents the actual content of the message (like text, images, audio, documents, etc.)
- Metadata - Optional fields such as response information, message IDs, and token usage
Message types
System Message
Tells the model how to behave and provide context for interactions
Human Message
Represents user input and interactions with the model
AI Message
Responses generated by the model, including text content, tool calls,
and metadata
Tool Message
Represents the outputs of tool calls
Basic usage
The simplest way to use messages is to create message objects and pass them to a model when invoking.Text prompts
Text prompts are strings - ideal for straightforward generation tasks where you don’t need to retain conversation history.- You have a single, standalone request
- You don’t need conversation history
- You want minimal code complexity
Message prompts
Alternatively, you can pass in a list of messages to the model by providing a list of message objects.- Managing multi-turn conversations
- Working with multimodal content (images, audio, files)
- Including system instructions
Dictionary format
You can also specify messages directly in OpenAI chat completions format.Message types
System Message
ASystemMessage
represent an initial set of instructions that primes the
model’s behavior. You can use a system message to set the tone, define the
model’s role, and establish guidelines for responses.
Basic instructions
Detailed persona
Human Message
AHumanMessage
represents user input and interactions. They can contain text,
images, audio, files, and any other amount of multimodal content.
Text content
Message object
String shortcut
Message metadata
The
name
field behavior varies by provider - some use it for user
identification, others ignore it. To check, refer to the model provider’s
reference.AI Message
AnAIMessage
represents the output of a model invocation. They can include
multimodal data, tool calls, and provider-specific metadata that you can later
access.
AIMessage
objects are returned by the model when calling it, which contains
all of the associated metadata in the response. However, that doesn’t mean that’s
the only place they can be created/ modified from.
Providers weight/contextualize types of messages differently, which means it is
sometimes helpful to create a new AIMessage
object and insert it into the
message history as if it came from the model.
Attributes
The text content of the message.
The raw content of the message.
The tool calls made by the model. Empty if no tools are called.
A unique identifier for the message (either automatically generated by LangChain or returned in the provider response)
The usage metadata of the message, which can contain token counts when available.
The response metadata of the message.
Tool calling responses
When models make tool calls, they’re included in the AI message:Streaming and chunks
During streaming, you’ll receiveAIMessageChunk
objects that can be combined:
Tool Message
For models that support tool calling, AI messages can contain tool calls. Tool messages are used to pass the results of a single tool execution back to the model.Attributes
The stringified output of the tool call.
The ID of the tool call that this message is responding to. (this must match the ID of the tool call in the AI message)
The name of the tool that was called.
Additional data not sent to the model but can be accessed programmatically.
The
artifact
field stores supplementary data that won’t be sent to the
model but can be accessed programmatically. This is useful for storing raw
results, debugging information, or data for downstream processing without
cluttering the model’s context.Example: Using artifact for raw data
Example: Using artifact for raw data
Content
You can think of a message’s content as the payload of data that gets sent to the model. Within a message, you can content either as a string or a list of LangChain content blocks.AIMessage
objects will store the output of the model inside of the
content
field. If you want to access content in a way that won’t change
between providers, you can access the .contentBlocks
field, which will return
a list of content blocks that adhere to the
standard content format.
Access content blocks
content
differently, you can also initialize a
message with a list of content blocks. This will ensure that the message is
always in the standard format, regardless of the provider.
Examples
Multimodal message
Not all models support all file types. Check the model provider’s
reference for supported formats and size limits.
Content block reference
Content blocks are represented (either when creating a message or accessing thecontentBlocks
field) as a list of typed objects. Each item in the list
must adhere to one of the following block types:
Core
Core
Multimodal
Multimodal
ContentBlock.Multimodal.PlainText
ContentBlock.Multimodal.PlainText
ContentBlock.Multimodal.Image
ContentBlock.Multimodal.Image
ContentBlock.Multimodal.Audio
ContentBlock.Multimodal.Audio
ContentBlock.Multimodal.Video
ContentBlock.Multimodal.Video
ContentBlock.Multimodal.File
ContentBlock.Multimodal.File
Tool Calling
Tool Calling
ContentBlock.Tools.ToolCall
ContentBlock.Tools.ToolCall
ContentBlock.Tools.ToolCallChunk
ContentBlock.Tools.ToolCallChunk
Server-Side Tool Execution
Server-Side Tool Execution
ContentBlock.Tools.WebSearchCall
ContentBlock.Tools.WebSearchCall
Purpose: Built-in web search
The search query to execute
ContentBlock.Tools.WebSearchResult
ContentBlock.Tools.WebSearchResult
Purpose: Search resultsReturns: Top search results with optional snippets
URLs of the search results
ContentBlock.Tools.CodeInterpreterCall
ContentBlock.Tools.CodeInterpreterCall
ContentBlock
type.
Content blocks were introduced as a new property on messages in LangChain v1
to standardize content formats across providers while maintaining backward
compatibility with existing code. Content blocks are not a replacement for
the
content
property, but rather a new property that can be used to access
the content of a message in a standardized format.