Message
Carrying both the content and metadata which represent the state
Messages contain:
- Role: identifies the message type(system,user)
- Content: the actual content
- Metadata: optional fields: such as information,message IDs,and token usage
This is the standard message type
Basic usage
The easiest way to use message is to create message objects and pass them to a model when invoking
Text prompts
String Ideal for generation tasks where you don’t need to retain.
| |
When to use:
- a single ,standalone request
- no need for conversation history
- minimal code complexity
Message prompts
Providing a list of messages
| |
When:
- Managing multi-turn conversations
- Working with multimodal content (images, audio, files)
- Including system instructions
Dictionary format
Specify message in OpenAI chat completions format:
| |
Message types
- System messages
- Human messages
- Al messages:
- Tool messages:
System message
An initial set of instructions that primes the model’s behavior
Human message
user input and interactions
AI message
The LLM sometimes treat the message differently, so sometimes we can make up a AI message manually.
Tool calls
are included in AI messages
Token usage
in AI message in usage_metadata field
Streaming and chunks
When streaming, u can receive AIMessageChunk objects and it can be combined into a full message by summing
Tool message
tool result in ToolMessage object
Message content
An loosely-typed attribute,supporting strings and lists if untyped objects
Beside, LangChain provides dedicated content types for text, reasoning, citations, multi-modal data, server-side tool calls, and other message content
This may contain either:
- 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.
消息对象实现了一个 content_blocks 属性,该属性会延迟将 content 属性解析为标准的、类型安全的表示形式。例如,从 ChatAnthropic 或 ChatOpenAI 生成的消息将包含各自提供商格式的 thinking 或 reasoning 块,但可以延迟解析为一致的 ReasoningContentBlock 表示形式
| |
| |
将不同的provider的统一接口,是通过content_blocks属性来实现的
Multimodal
Multimodality refers to the ability to work with data that comes in different forms, such as text, audio, images, and video
Chat models can accept multimodal data as input and generate it as output. Below we show short examples of input messages featuring multimodal data.
| |
Content blocks are represented (either when creating a message or accessing the content_blocks property) as a list of typed dictionaries
Serialization
You can serialize messages to plain objects for storage and deserialize back to message instances. This is useful for persisting conversation history and resuming sessions.
| |