基本用法
使用消息的最简单方法是创建消息对象并在调用时将它们传递给模型。文本提示
文本提示是字符串 - 适用于不需要保留对话历史记录的简单生成任务。- 您有一个单一的独立请求
- 您不需要对话历史记录
- 您希望代码复杂性最小
消息提示
或者,您可以通过提供消息对象列表向模型传递消息列表。- 管理多轮对话
- 处理多模态内容(图像、音频、文件)
- 包含系统指令
字典格式
您也可以直接以 OpenAI 聊天完成格式指定消息。消息类型
系统消息
SystemMessage 表示一组初始指令,用于引导模型的行为。您可以使用系统消息来设置语气、定义模型的角色并建立响应指南。
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 metadata
Add metadata
name 字段的行为因提供商而异 - 有些将其用于用户识别,有些则忽略它。要检查,请参阅模型提供商的参考。AI 消息
AIMessage 表示模型调用的输出。它们可以包括多模态数据、工具调用和您稍后可以访问的提供商特定元数据。
AIMessage 对象在调用模型时由模型返回,它包含响应中的所有相关元数据。
提供商对消息类型的权衡/上下文化方式不同,这意味着有时手动创建一个新的 AIMessage 对象并将其插入消息历史记录中,就像它来自模型一样,会很有帮助。
Attributes
Attributes
The text content of the message.
The raw content of the message.
The standardized content blocks 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 calls
When models make tool calls, they’re included in theAIMessage:
Token usage
AnAIMessage can hold token counts and other usage metadata in its usage_metadata field:
UsageMetadata for details.
Streaming and chunks
During streaming, you’ll receiveAIMessageChunk objects that can be combined into a full message object:
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. Tools can generateToolMessage objects directly. Below, we show a simple example. Read more in the tools guide.
Attributes
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
AIMessage)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 retrieval metadata
Example: Using artifact for retrieval metadata
For example, a retrieval tool could retrieve a passage from a document for reference by a model. Where message See the RAG tutorial for an end-to-end example of building retrieval agents with LangChain.
content contains text that the model will reference, an artifact can contain document identifiers or other metadata that an application can use (e.g., to render a page). See example below:Message content
You can think of a message’s content as the payload of data that gets sent to the model. Messages have acontent attribute that is loosely-typed, supporting strings and lists of untyped objects (e.g., dictionaries). This allows support for provider-native structures directly in LangChain chat models, such as multimodal content and other data.
Separately, LangChain provides dedicated content types for text, reasoning, citations, multi-modal data, server-side tool calls, and other message content. See content blocks below.
LangChain chat models accept message content in the content attribute, and can contain:
- A string
- A list of content blocks in a provider-native format
- A list of LangChain’s standard content blocks
Standard content blocks
LangChain provides a standard representation for message content that works across providers. Message objects implement acontent_blocks property that will lazily parse the content attribute into a standard, type-safe representation. For example, messages generated from ChatAnthropic or ChatOpenAI will include thinking or reasoning blocks in the format of the respective provider, but can be lazily parsed into a consistent ReasoningContentBlock representation:
- Anthropic
- OpenAI
Serializing standard contentIf an application outside of LangChain needs access to the standard content block
representation, you can opt-in to storing content blocks in message content.To do this, you can set the
LC_OUTPUT_VERSION environment variable to v1. Or,
initialize any chat model with output_version="v1":Multimodal
Multimodality refers to the ability to work with data that comes in different forms, such as text, audio, images, and video. LangChain includes standard types for these data that can be used across providers. Chat models can accept multimodal data as input and generate it as output. Below we show short examples of input messages featuring multimodal data.Extra keys can be included top-level in the content block or nested in
"extras": {"key": value}.OpenAI and AWS Bedrock Converse,
for example, require a filename for PDFs. See the provider page
for your chosen model for specifics.Content block reference
Content blocks are represented (either when creating a message or accessing thecontent_blocks property) as a list of typed dictionaries. Each item in the list must adhere to one of the following block types:
Core
Core
TextContentBlock
TextContentBlock
Multimodal
Multimodal
ImageContentBlock
ImageContentBlock
Purpose: Image data
Always
"image"URL pointing to the image location.
Base64-encoded image data.
Reference ID to an externally stored image (e.g., in a provider’s file system or in a bucket).
AudioContentBlock
AudioContentBlock
Purpose: Audio data
Always
"audio"URL pointing to the audio location.
Base64-encoded audio data.
Reference ID to an externally stored audio file (e.g., in a provider’s file system or in a bucket).
VideoContentBlock
VideoContentBlock
Purpose: Video data
Always
"video"URL pointing to the video location.
Base64-encoded video data.
Reference ID to an externally stored video file (e.g., in a provider’s file system or in a bucket).
FileContentBlock
FileContentBlock
Purpose: Generic files (PDF, etc)
Always
"file"URL pointing to the file location.
Base64-encoded file data.
Reference ID to an externally stored file (e.g., in a provider’s file system or in a bucket).
Tool Calling
Tool Calling
ToolCall
ToolCall
ToolCallChunk
ToolCallChunk
Server-Side Tool Execution
Server-Side Tool Execution
ServerToolCall
ServerToolCall
ServerToolCallChunk
ServerToolCallChunk
Purpose: Streaming server-side tool call fragments
Always
"server_tool_call_chunk"An identifier associated with the tool call.
Name of the tool being called
Partial tool arguments (may be incomplete JSON)
Position of this chunk in the stream
ServerToolResult
ServerToolResult
Purpose: Search results
Always
"server_tool_result"Identifier of the corresponding server tool call.
Identifier associated with the server tool result.
Execution status of the server-side tool.
"success" or "error".output
Output of the executed tool.
Provider-Specific Blocks
Provider-Specific Blocks
NonStandardContentBlock
NonStandardContentBlock
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.Use with chat models
Chat models accept a sequence of message objects as input and return anAIMessage as output. Interactions are often stateless, so that a simple conversational loop involves invoking a model with a growing list of messages.
Refer to the below guides to learn more:
- Built-in features for persisting and managing conversation histories
- Strategies for managing context windows, including trimming and summarizing messages