主题
消息是 LangChain 中模型上下文的基本单元。它们代表模型的输入和输出,承载着与 LLM 交互时表示对话状态所需的内容和元数据。
消息是包含以下内容的对象:
LangChain 提供了一个适用于所有模型提供商的标准消息类型,确保无论调用哪个模型,行为都保持一致。
基本用法
使用消息最简单的方法是创建消息对象,并在调用模型时将其传递给模型。
python
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage, AIMessage, SystemMessage
model = init_chat_model("gpt-5-nano")
system_msg = SystemMessage("You are a helpful assistant.")
human_msg = HumanMessage("Hello, how are you?")
# Use with chat models
messages = [system_msg, human_msg]
response = model.invoke(messages) # Returns AIMessage文本提示
文本提示是字符串 - 非常适合不需要保留对话历史的简单生成任务。
python
response = model.invoke("Write a haiku about spring")在以下情况下使用文本提示:
- 您有一个单一的、独立的请求
- 您不需要对话历史
- 您希望代码复杂度最低
消息提示
或者,您可以通过提供消息对象列表,将消息列表传递给模型。
python
from langchain.messages import SystemMessage, HumanMessage, AIMessage
messages = [
SystemMessage("You are a poetry expert"),
HumanMessage("Write a haiku about spring"),
AIMessage("Cherry blossoms bloom...")
]
response = model.invoke(messages)在以下情况下使用消息提示:
- 管理多轮对话
- 处理多模态内容(图像、音频、文件)
- 包含系统指令
字典格式
您也可以直接以 OpenAI 聊天补全格式指定消息。
python
messages = [
{"role": "system", "content": "You are a poetry expert"},
{"role": "user", "content": "Write a haiku about spring"},
{"role": "assistant", "content": "Cherry blossoms bloom..."}
]
response = model.invoke(messages)消息类型
系统消息
SystemMessage 代表一组初始指令,用于引导模型的行为。您可以使用系统消息来设定语气、定义模型的角色,并为响应建立指导原则。
Basic instructions
python
system_msg = SystemMessage("You are a helpful coding assistant.")
messages = [
system_msg,
HumanMessage("How do I create a REST API?")
]
response = model.invoke(messages)Detailed persona
python
from langchain.messages import SystemMessage, HumanMessage
system_msg = SystemMessage("""
You are a senior Python developer with expertise in web frameworks.
Always provide code examples and explain your reasoning.
Be concise but thorough in your explanations.
""")
messages = [
system_msg,
HumanMessage("How do I create a REST API?")
]
response = model.invoke(messages)人类消息
HumanMessage 代表用户输入和交互。它们可以包含文本、图像、音频、文件以及任何其他数量的多模态内容。
文本内容
python
response = model.invoke([
HumanMessage("What is machine learning?")
])python
# Using a string is a shortcut for a single HumanMessage
response = model.invoke("What is machine learning?")消息元数据
Add metadata
python
human_msg = HumanMessage(
content="Hello!",
name="alice", # Optional: identify different users
id="msg_123", # Optional: unique identifier for tracing
)name 字段的行为因提供商而异 - 有些将其用于用户识别,有些则忽略它。要检查,请参阅模型提供商的参考文档。
AI 消息
AIMessage 代表模型调用的输出。它们可以包含多模态数据、工具调用以及稍后可以访问的特定于提供商的元数据。
python
response = model.invoke("Explain AI")
print(type(response)) # <class 'langchain.messages.AIMessage'>AIMessage 对象在调用模型时由模型返回,其中包含响应中的所有关联元数据。
提供商对不同类型消息的权重/上下文处理方式不同,这意味着有时手动创建一个新的 AIMessage 对象并将其插入到消息历史中,就好像它来自模型一样,会很有帮助。
python
from langchain.messages import AIMessage, SystemMessage, HumanMessage
# Create an AI message manually (e.g., for conversation history)
ai_msg = AIMessage("I'd be happy to help you with that question!")
# Add to conversation history
messages = [
SystemMessage("You are a helpful assistant"),
HumanMessage("Can you help me?"),
ai_msg, # Insert as if it came from the model
HumanMessage("Great! What's 2+2?")
]
response = model.invoke(messages)属性
textstring消息的文本内容。
contentstring | dict[]消息的原始内容。
content_blocksContentBlock[]消息的标准化内容块。
tool_callsdict[] | None模型进行的工具调用。
如果未调用任何工具,则为空。
idstring消息的唯一标识符(由 LangChain 自动生成或在提供商响应中返回)
usage_metadatadict | None消息的使用元数据,在可用时可以包含令牌计数。
response_metadataResponseMetadata | None消息的响应元数据。
工具调用
当模型进行工具调用时,它们会包含在 AIMessage 中:
python
from langchain.chat_models import init_chat_model
model = init_chat_model("gpt-5-nano")
def get_weather(location: str) -> str:
"""Get the weather at a location."""
...
model_with_tools = model.bind_tools([get_weather])
response = model_with_tools.invoke("What's the weather in Paris?")
for tool_call in response.tool_calls:
print(f"Tool: {tool_call['name']}")
print(f"Args: {tool_call['args']}")
print(f"ID: {tool_call['id']}")其他结构化数据,例如推理或引用,也可能出现在消息内容中。
令牌使用情况
AIMessage 可以在其 usage_metadata 字段中保存令牌计数和其他使用元数据:
python
from langchain.chat_models import init_chat_model
model = init_chat_model("gpt-5-nano")
response = model.invoke("Hello!")
response.usage_metadata{'input_tokens': 8,
'output_tokens': 304,
'total_tokens': 312,
'input_token_details': {'audio': 0, 'cache_read': 0},
'output_token_details': {'audio': 0, 'reasoning': 256}}详情请参见 UsageMetadata。
流式传输和分块
在流式传输期间,您将收到可以组合成完整消息对象的 AIMessageChunk 对象:
python
chunks = []
full_message = None
for chunk in model.stream("Hi"):
chunks.append(chunk)
print(chunk.text)
full_message = chunk if full_message is None else full_message + chunk工具消息
对于支持工具调用的模型,AI 消息可以包含工具调用。工具消息用于将单个工具执行的结果传递回模型。
工具可以直接生成 ToolMessage 对象。下面,我们展示一个简单的例子。更多信息请参阅工具指南。
python
from langchain.messages import AIMessage
from langchain.messages import ToolMessage
# After a model makes a tool call
# (Here, we demonstrate manually creating the messages for brevity)
ai_message = AIMessage(
content=[],
tool_calls=[{
"name": "get_weather",
"args": {"location": "San Francisco"},
"id": "call_123"
}]
)
# Execute tool and create result message
weather_result = "Sunny, 72°F"
tool_message = ToolMessage(
content=weather_result,
tool_call_id="call_123" # Must match the call ID
)
# Continue conversation
messages = [
HumanMessage("What's the weather in San Francisco?"),
ai_message, # Model's tool call
tool_message, # Tool execution result
]
response = model.invoke(messages) # Model processes the result属性
contentstringrequired工具调用的字符串化输出。
tool_call_idstringrequired此消息所响应的工具调用的 ID。必须与 AIMessage 中工具调用的 ID 匹配。
namestringrequired被调用工具的名称。
artifactdict不会发送给模型但可以通过编程方式访问的附加数据。
artifact 字段存储不会发送给模型但可以通过编程方式访问的补充数据。这对于存储原始结果、调试信息或用于下游处理的数据非常有用,而不会使模型的上下文变得混乱。
示例:使用 artifact 存储检索元数据
例如,一个检索工具可以检索文档中的一段文本供模型参考。消息 content 包含模型将引用的文本,而 artifact 可以包含文档标识符或其他应用程序可以使用的元数据(例如,用于渲染页面)。参见下面的示例:
python
from langchain.messages import ToolMessage
# Sent to model
message_content = "It was the best of times, it was the worst of times."
# Artifact available downstream
artifact = {"document_id": "doc_123", "page": 0}
tool_message = ToolMessage(
content=message_content,
tool_call_id="call_123",
name="search_books",
artifact=artifact,
)消息内容
您可以将消息的内容视为发送给模型的数据负载。消息有一个 content 属性,它是松散类型的,支持字符串和无类型对象(例如字典)的列表。这允许在 LangChain 聊天模型中直接支持提供商原生结构,例如多模态内容和其他数据。
另外,LangChain 为文本、推理、引用、多模态数据、服务器端工具调用和其他消息内容提供了专用的内容类型。请参见下面的内容块。
LangChain 聊天模型接受 content 属性中的消息内容。
这可能包含:
- 一个字符串
- 提供商原生格式的内容块列表
- LangChain 标准内容块列表
请参见下面使用多模态输入的示例:
python
from langchain.messages import HumanMessage
# String content
human_message = HumanMessage("Hello, how are you?")
# Provider-native format (e.g., OpenAI)
human_message = HumanMessage(content=[
{"type": "text", "text": "Hello, how are you?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
])
# List of standard content blocks
human_message = HumanMessage(content_blocks=[
{"type": "text", "text": "Hello, how are you?"},
{"type": "image", "url": "https://example.com/image.jpg"},
])在初始化消息时指定 content_blocks 仍将填充消息的 content,但为此提供了一个类型安全的接口。
标准内容块
LangChain 提供了一个适用于所有提供商的消息内容标准表示。
消息对象实现了一个 content_blocks 属性,该属性将惰性地将 content 属性解析为标准化的、类型安全的表示。例如,从 ChatAnthropic 或 ChatOpenAI 生成的消息将包含各自提供商格式的 thinking 或 reasoning 块,但可以惰性地解析为一致的 ReasoningContentBlock 表示:
Anthropic
OpenAI
python
from langchain.messages import AIMessage
message = AIMessage(
content=[
{"type": "thinking", "thinking": "...", "signature": "WaUjzkyp..."},
{"type": "text", "text": "..."},
],
response_metadata={"model_provider": "anthropic"}
)
message.content_blocks[{'type': 'reasoning',
'reasoning': '...',
'extras': {'signature': 'WaUjzkyp...'}},
{'type': 'text', 'text': '...'}]请参阅集成指南以开始使用您选择的推理提供商。
序列化标准内容
如果 LangChain 之外的应用程序需要访问标准内容块表示,您可以选择将内容块存储在消息内容中。
为此,您可以将 LC_OUTPUT_VERSION 环境变量设置为 v1。或者,使用 output_version="v1" 初始化任何聊天模型:
python
from langchain.chat_models import init_chat_model
model = init_chat_model("gpt-5-nano", output_version="v1")多模态
多模态指的是处理不同形式数据的能力,例如文本、音频、图像和视频。LangChain 包含了可用于所有提供商的这些数据的标准类型。
聊天模型可以接受多模态数据作为输入并生成多模态数据作为输出。下面我们展示包含多模态数据的输入消息的简短示例。
额外的键可以包含在内容块的顶层或嵌套在 "extras": {"key": value} 中。
例如,OpenAI 和 AWS Bedrock Converse 要求 PDF 文件具有文件名。有关详细信息,请参阅您所选模型的提供商页面。
python
# From URL
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this image."},
{"type": "image", "url": "https://example.com/path/to/image.jpg"},
]
}
# From base64 data
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this image."},
{
"type": "image",
"base64": "AAAAIGZ0eXBtcDQyAAAAAGlzb21tcDQyAAACAGlzb2...",
"mime_type": "image/jpeg",
},
]
}
# From provider-managed File ID
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this image."},
{"type": "image", "file_id": "file-abc123"},
]
}python
# From URL
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this document."},
{"type": "file", "url": "https://example.com/path/to/document.pdf"},
]
}
# From base64 data
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this document."},
{
"type": "file",
"base64": "AAAAIGZ0eXBtcDQyAAAAAGlzb21tcDQyAAACAGlzb2...",
"mime_type": "application/pdf",
},
]
}
# From provider-managed File ID
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this document."},
{"type": "file", "file_id": "file-abc123"},
]
}python
# From base64 data
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this audio."},
{
"type": "audio",
"base64": "AAAAIGZ0eXBtcDQyAAAAAGlzb21tcDQyAAACAGlzb2...",
"mime_type": "audio/wav",
},
]
}
# From provider-managed File ID
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this audio."},
{"type": "audio", "file_id": "file-abc123"},
]
}python
# From base64 data
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this video."},
{
"type": "video",
"base64": "AAAAIGZ0eXBtcDQyAAAAAGlzb21tcDQyAAACAGlzb2...",
"mime_type": "video/mp4",
},
]
}
# From provider-managed File ID
message = {
"role": "user",
"content": [
{"type": "text", "text": "Describe the content of this video."},
{"type": "video", "file_id": "file-abc123"},
]
}并非所有模型都支持所有文件类型。请检查模型提供商的参考文档以了解支持的格式和大小限制。
内容块参考
内容块被表示(无论是在创建消息时还是访问 content_blocks 属性时)为类型化字典的列表。列表中的每个项目必须符合以下块类型之一:
核心
TextContentBlock
用途: 标准文本输出
typestringrequired始终为 "text"
textstringrequired文本内容
annotationsobject[]文本的注释列表
extrasobject额外的提供商特定数据
示例:
python
{
"type": "text",
"text": "Hello world",
"annotations": []
}ReasoningContentBlock
用途: 模型推理步骤
typestringrequired始终为 "reasoning"
reasoningstring推理内容
extrasobject额外的提供商特定数据
示例:
python
{
"type": "reasoning",
"reasoning": "The user is asking about...",
"extras": {"signature": "abc123"},
}多模态
ImageContentBlock
用途: 图像数据
typestringrequired始终为 "image"
urlstring指向图像位置的 URL。
base64stringBase64 编码的图像数据。
idstring此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_typestring图像 MIME 类型(例如,image/jpeg、image/png)。对于 base64 数据是必需的。
AudioContentBlock
用途: 音频数据
typestringrequired始终为 "audio"
urlstring指向音频位置的 URL。
base64stringBase64 编码的音频数据。
idstring此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_typestring音频 MIME 类型(例如,audio/mpeg、audio/wav)。对于 base64 数据是必需的。
VideoContentBlock
用途: 视频数据
typestringrequired始终为 "video"
urlstring指向视频位置的 URL。
base64stringBase64 编码的视频数据。
idstring此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_typestring视频 MIME 类型(例如,video/mp4、video/webm)。对于 base64 数据是必需的。
FileContentBlock
用途: 通用文件(PDF 等)
typestringrequired始终为 "file"
urlstring指向文件位置的 URL。
base64stringBase64 编码的文件数据。
idstring此内容块的唯一标识符(由提供商或 LangChain 生成)。
mime_typestring文件 MIME 类型(例如,application/pdf)。对于 base64 数据是必需的。
PlainTextContentBlock
用途: 文档文本(.txt、.md)
typestringrequired始终为 "text-plain"
textstring文本内容
mime_typestring文本的 MIME 类型(例如,text/plain、text/markdown)
工具调用
ToolCall
用途: 函数调用
typestringrequired始终为 "tool_call"
namestringrequired要调用的工具名称
argsobjectrequired传递给工具的参数
idstringrequired此工具调用的唯一标识符
示例:
python
{
"type": "tool_call",
"name": "search",
"args": {"query": "weather"},
"id": "call_123"
}ToolCallChunk
用途: 流式工具调用片段
typestringrequired始终为 "tool_call_chunk"
namestring被调用工具的名称
argsstring部分工具参数(可能是不完整的 JSON)
idstring工具调用标识符
indexnumber | string此片段在流中的位置
InvalidToolCall
用途: 格式错误的调用,旨在捕获 JSON 解析错误。
typestringrequired始终为 "invalid_tool_call"
namestring调用失败的工具名称
argsobject传递给工具的参数
errorstring错误描述
服务器端工具执行
ServerToolCall
用途: 在服务器端执行的工具调用。
typestringrequired始终为 "server_tool_call"
idstringrequired与工具调用关联的标识符。
namestringrequired要调用的工具名称。
argsstringrequired部分工具参数(可能是不完整的 JSON)
ServerToolCallChunk
用途: 流式服务器端工具调用片段
typestringrequired始终为 "server_tool_call_chunk"
idstring与工具调用关联的标识符。
namestring被调用工具的名称
argsstring部分工具参数(可能是不完整的 JSON)
indexnumber | string此片段在流中的位置
ServerToolResult
用途: 搜索结果
typestringrequired始终为 "server_tool_result"
tool_call_idstringrequired对应服务器工具调用的标识符。
idstring与服务器工具结果关联的标识符。
statusstringrequired服务器端工具的执行状态。"success" 或 "error"。
output已执行工具的输出。
提供商特定块
NonStandardContentBlock
用途: 提供商特定的逃生舱口
typestringrequired始终为 "non_standard"
valueobjectrequired提供商特定的数据结构
用法: 用于实验性或提供商独有的功能
额外的提供商特定内容类型可以在每个模型提供商的参考文档中找到。
请在 API 参考 中查看规范的类型定义。
内容块是 LangChain v1 中作为消息的新属性引入的,旨在跨供应商标准化内容格式,同时保持与现有代码的向后兼容性。
内容块不是 content 属性的替代品,而是一个新的属性,可用于以标准化格式访问消息的内容。
与聊天模型一起使用
聊天模型 接受一系列消息对象作为输入,并返回一个 AIMessage 作为输出。交互通常是无状态的,因此一个简单的对话循环涉及使用不断增长的消息列表来调用模型。
请参考以下指南了解更多信息:
- 用于持久化和管理对话历史记录的内置功能
- 管理上下文窗口的策略,包括修剪和总结消息