Messages¶
langchain.messages
¶
Message types.
Includes message types for different roles (e.g., human, AI, system), as well as types for message content blocks (e.g., text, image, audio) and tool calls.
Reference docs
This page contains reference documentation for Messages. See the docs for conceptual guides, tutorials, and examples on using Messages.
CLASS | DESCRIPTION |
---|---|
AIMessage |
Message from an AI. |
AIMessageChunk |
Message chunk from an AI (yielded when streaming). |
HumanMessage |
Message from the user. |
SystemMessage |
Message for priming AI behavior. |
ToolMessage |
Message for passing the result of executing a tool back to a model. |
ToolCall |
Represents an AI's request to call a tool. |
InvalidToolCall |
Allowance for errors made by LLM. |
ToolCallChunk |
A chunk of a tool call (yielded when streaming). |
ServerToolCall |
Tool call that is executed server-side. |
ServerToolCallChunk |
A chunk of a server-side tool call (yielded when streaming). |
ServerToolResult |
Result of a server-side tool call. |
TextContentBlock |
Text output from a LLM. |
Citation |
Annotation for citing data from a document. |
NonStandardAnnotation |
Provider-specific annotation format. |
ReasoningContentBlock |
Reasoning output from a LLM. |
ImageContentBlock |
Image data. |
VideoContentBlock |
Video data. |
AudioContentBlock |
Audio data. |
PlainTextContentBlock |
Plaintext data (e.g., from a document). |
FileContentBlock |
File data that doesn't fit into other multimodal block types. |
NonStandardContentBlock |
Provider-specific data. |
FUNCTION | DESCRIPTION |
---|---|
trim_messages |
Trim messages to be below a token count. |
ATTRIBUTE | DESCRIPTION |
---|---|
AnyMessage |
"A type representing any defined
|
MessageLikeRepresentation |
A type representing the various ways a message can be represented.
|
ContentBlock |
A union of all defined
|
Annotation |
A union of all defined
|
DataContentBlock |
A union of all defined multimodal data
|
AIMessage
¶
Bases: BaseMessage
Message from an AI.
An AIMessage
is returned from a chat model as a response to a prompt.
This message represents the output of the model and consists of both the raw output as returned by the model and standardized fields (e.g., tool calls, usage metadata) added by the LangChain framework.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initialize an |
pretty_repr |
Return a pretty representation of the message for display. |
ATTRIBUTE | DESCRIPTION |
---|---|
tool_calls |
If present, tool calls associated with the message. |
invalid_tool_calls |
If present, tool calls with parsing errors associated with the message.
TYPE:
|
usage_metadata |
If present, usage metadata for a message, such as token counts.
TYPE:
|
type |
The type of the message (used for deserialization).
TYPE:
|
lc_attributes |
Attributes to be serialized.
TYPE:
|
content_blocks |
Return standard, typed
TYPE:
|
tool_calls
class-attribute
instance-attribute
¶
If present, tool calls associated with the message.
invalid_tool_calls
class-attribute
instance-attribute
¶
invalid_tool_calls: list[InvalidToolCall] = []
If present, tool calls with parsing errors associated with the message.
usage_metadata
class-attribute
instance-attribute
¶
If present, usage metadata for a message, such as token counts.
This is a standard representation of token usage that is consistent across models.
type
class-attribute
instance-attribute
¶
type: Literal['ai'] = 'ai'
The type of the message (used for deserialization).
__init__
¶
__init__(
content: str | list[str | dict] | None = None,
content_blocks: list[ContentBlock] | None = None,
**kwargs: Any
) -> None
Initialize an AIMessage
.
Specify content
as positional arg or content_blocks
for typing.
PARAMETER | DESCRIPTION |
---|---|
content
|
The content of the message. |
content_blocks
|
Typed standard content.
TYPE:
|
**kwargs
|
Additional arguments to pass to the parent class.
TYPE:
|
lc_attributes
property
¶
lc_attributes: dict
Attributes to be serialized.
Includes all attributes, even if they are derived from other initialization arguments.
content_blocks
property
¶
content_blocks: list[ContentBlock]
Return standard, typed ContentBlock
dicts from the message.
If the message has a known model provider, use the provider-specific translator
first before falling back to best-effort parsing. For details, see the property
on BaseMessage
.
AIMessageChunk
¶
Bases: AIMessage
, BaseMessageChunk
Message chunk from an AI (yielded when streaming).
METHOD | DESCRIPTION |
---|---|
init_tool_calls |
Initialize tool calls from tool call chunks. |
init_server_tool_calls |
Parse |
__add__ |
Message chunks support concatenation with other message chunks. |
ATTRIBUTE | DESCRIPTION |
---|---|
type |
The type of the message (used for deserialization).
TYPE:
|
tool_call_chunks |
If provided, tool call chunks associated with the message.
TYPE:
|
chunk_position |
Optional span represented by an aggregated
TYPE:
|
lc_attributes |
Attributes to be serialized, even if they are derived from other initialization args.
TYPE:
|
content_blocks |
Return standard, typed
TYPE:
|
type
class-attribute
instance-attribute
¶
type: Literal['AIMessageChunk'] = 'AIMessageChunk'
The type of the message (used for deserialization).
tool_call_chunks
class-attribute
instance-attribute
¶
tool_call_chunks: list[ToolCallChunk] = []
If provided, tool call chunks associated with the message.
chunk_position
class-attribute
instance-attribute
¶
chunk_position: Literal['last'] | None = None
Optional span represented by an aggregated AIMessageChunk
.
If a chunk with chunk_position="last"
is aggregated into a stream,
tool_call_chunks
in message content will be parsed into tool_calls
.
lc_attributes
property
¶
lc_attributes: dict
Attributes to be serialized, even if they are derived from other initialization args.
content_blocks
property
¶
content_blocks: list[ContentBlock]
Return standard, typed ContentBlock
dicts from the message.
init_tool_calls
¶
init_tool_calls() -> Self
Initialize tool calls from tool call chunks.
RETURNS | DESCRIPTION |
---|---|
Self
|
The values with tool calls initialized. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the tool call chunks are malformed. |
__add__
¶
__add__(other: Any) -> BaseMessageChunk
Message chunks support concatenation with other message chunks.
This functionality is useful to combine message chunks yielded from a streaming model into a complete message.
PARAMETER | DESCRIPTION |
---|---|
other
|
Another message chunk to concatenate with this one.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
BaseMessageChunk
|
A new message chunk that is the concatenation of this message chunk |
BaseMessageChunk
|
and the other message chunk. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the other object is not a message chunk. |
For example,
AIMessageChunk(content="Hello") + AIMessageChunk(content=" World")
will give AIMessageChunk(content="Hello World")
HumanMessage
¶
Bases: BaseMessage
Message from the user.
A HumanMessage
is a message that is passed in from a user to the model.
Example
METHOD | DESCRIPTION |
---|---|
__init__ |
Specify |
ATTRIBUTE | DESCRIPTION |
---|---|
type |
The type of the message (used for serialization).
TYPE:
|
SystemMessage
¶
Bases: BaseMessage
Message for priming AI behavior.
The system message is usually passed in as the first of a sequence of input messages.
Example
METHOD | DESCRIPTION |
---|---|
__init__ |
Specify |
ATTRIBUTE | DESCRIPTION |
---|---|
type |
The type of the message (used for serialization).
TYPE:
|
AnyMessage
module-attribute
¶
AnyMessage = Annotated[
Annotated[AIMessage, Tag(tag="ai")]
| Annotated[HumanMessage, Tag(tag="human")]
| Annotated[ChatMessage, Tag(tag="chat")]
| Annotated[SystemMessage, Tag(tag="system")]
| Annotated[FunctionMessage, Tag(tag="function")]
| Annotated[ToolMessage, Tag(tag="tool")]
| Annotated[AIMessageChunk, Tag(tag="AIMessageChunk")]
| Annotated[HumanMessageChunk, Tag(tag="HumanMessageChunk")]
| Annotated[ChatMessageChunk, Tag(tag="ChatMessageChunk")]
| Annotated[SystemMessageChunk, Tag(tag="SystemMessageChunk")]
| Annotated[FunctionMessageChunk, Tag(tag="FunctionMessageChunk")]
| Annotated[ToolMessageChunk, Tag(tag="ToolMessageChunk")],
Field(discriminator=Discriminator(_get_type)),
]
"A type representing any defined Message
or MessageChunk
type.
MessageLikeRepresentation
module-attribute
¶
A type representing the various ways a message can be represented.
ToolMessage
¶
Bases: BaseMessage
, ToolOutputMixin
Message for passing the result of executing a tool back to a model.
ToolMessage
objects contain the result of a tool invocation. Typically, the result
is encoded inside the content
field.
Example: A ToolMessage
representing a result of 42
from a tool call with id
from langchain_core.messages import ToolMessage
ToolMessage(content="42", tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL")
Example: A ToolMessage
where only part of the tool output is sent to the model
and the full output is passed in to artifact.
from langchain_core.messages import ToolMessage
tool_output = {
"stdout": "From the graph we can see that the correlation between "
"x and y is ...",
"stderr": None,
"artifacts": {"type": "image", "base64_data": "/9j/4gIcSU..."},
}
ToolMessage(
content=tool_output["stdout"],
artifact=tool_output,
tool_call_id="call_Jja7J89XsjrOLA5r!MEOW!SL",
)
The tool_call_id
field is used to associate the tool call request with the
tool call response. Useful in situations where a chat model is able
to request multiple tool calls in parallel.
METHOD | DESCRIPTION |
---|---|
coerce_args |
Coerce the model arguments to the correct types. |
__init__ |
Initialize a |
ATTRIBUTE | DESCRIPTION |
---|---|
tool_call_id |
Tool call that this message is responding to.
TYPE:
|
type |
The type of the message (used for serialization).
TYPE:
|
artifact |
Artifact of the Tool execution which is not meant to be sent to the model.
TYPE:
|
status |
Status of the tool invocation.
TYPE:
|
additional_kwargs |
Currently inherited from
TYPE:
|
response_metadata |
Currently inherited from
TYPE:
|
type
class-attribute
instance-attribute
¶
type: Literal['tool'] = 'tool'
The type of the message (used for serialization).
artifact
class-attribute
instance-attribute
¶
artifact: Any = None
Artifact of the Tool execution which is not meant to be sent to the model.
Should only be specified if it is different from the message content, e.g. if only a subset of the full tool output is being passed as message content but the full output is needed in other parts of the code.
status
class-attribute
instance-attribute
¶
status: Literal['success', 'error'] = 'success'
Status of the tool invocation.
additional_kwargs
class-attribute
instance-attribute
¶
Currently inherited from BaseMessage
, but not used.
response_metadata
class-attribute
instance-attribute
¶
Currently inherited from BaseMessage
, but not used.
coerce_args
classmethod
¶
Coerce the model arguments to the correct types.
PARAMETER | DESCRIPTION |
---|---|
values
|
The model arguments.
TYPE:
|
__init__
¶
__init__(
content: str | list[str | dict] | None = None,
content_blocks: list[ContentBlock] | None = None,
**kwargs: Any
) -> None
Initialize a ToolMessage
.
Specify content
as positional arg or content_blocks
for typing.
PARAMETER | DESCRIPTION |
---|---|
content
|
The contents of the message. |
content_blocks
|
Typed standard content.
TYPE:
|
**kwargs
|
Additional fields.
TYPE:
|
ToolCall
¶
Bases: TypedDict
Represents an AI's request to call a tool.
Example
This represents a request to call the tool named 'foo'
with arguments
{"a": 1}
and an identifier of '123'
.
ATTRIBUTE | DESCRIPTION |
---|---|
name |
The name of the tool to be called.
TYPE:
|
args |
The arguments to the tool call. |
id |
An identifier associated with the tool call.
TYPE:
|
InvalidToolCall
¶
Bases: TypedDict
Allowance for errors made by LLM.
Here we add an error
key to surface errors made during generation
(e.g., invalid JSON arguments.)
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Used for discrimination.
TYPE:
|
id |
An identifier associated with the tool call.
TYPE:
|
name |
The name of the tool to be called.
TYPE:
|
args |
The arguments to the tool call.
TYPE:
|
error |
An error message associated with the tool call.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
extras |
Provider-specific metadata.
TYPE:
|
id
instance-attribute
¶
id: str | None
An identifier associated with the tool call.
An identifier is needed to associate a tool call request with a tool call result in events when multiple concurrent tool calls are made.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
ToolCallChunk
¶
Bases: TypedDict
A chunk of a tool call (yielded when streaming).
When merging ToolCallChunk
s (e.g., via AIMessageChunk.__add__
),
all string attributes are concatenated. Chunks are only merged if their
values of index
are equal and not None.
Example:
left_chunks = [ToolCallChunk(name="foo", args='{"a":', index=0)]
right_chunks = [ToolCallChunk(name=None, args="1}", index=0)]
(
AIMessageChunk(content="", tool_call_chunks=left_chunks)
+ AIMessageChunk(content="", tool_call_chunks=right_chunks)
).tool_call_chunks == [ToolCallChunk(name="foo", args='{"a":1}', index=0)]
ATTRIBUTE | DESCRIPTION |
---|---|
name |
The name of the tool to be called.
TYPE:
|
args |
The arguments to the tool call.
TYPE:
|
id |
An identifier associated with the tool call.
TYPE:
|
index |
The index of the tool call in a sequence.
TYPE:
|
ServerToolCall
¶
Bases: TypedDict
Tool call that is executed server-side.
For example: code execution, web search, etc.
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Used for discrimination.
TYPE:
|
id |
An identifier associated with the tool call.
TYPE:
|
name |
The name of the tool to be called.
TYPE:
|
args |
The arguments to the tool call. |
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
extras |
Provider-specific metadata.
TYPE:
|
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
ServerToolCallChunk
¶
Bases: TypedDict
A chunk of a server-side tool call (yielded when streaming).
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Used for discrimination.
TYPE:
|
name |
The name of the tool to be called.
TYPE:
|
args |
JSON substring of the arguments to the tool call.
TYPE:
|
id |
An identifier associated with the tool call.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
extras |
Provider-specific metadata.
TYPE:
|
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
ServerToolResult
¶
Bases: TypedDict
Result of a server-side tool call.
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Used for discrimination.
TYPE:
|
id |
An identifier associated with the server tool result.
TYPE:
|
tool_call_id |
ID of the corresponding server tool call.
TYPE:
|
status |
Execution status of the server-side tool.
TYPE:
|
output |
Output of the executed tool.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
extras |
Provider-specific metadata.
TYPE:
|
ContentBlock
module-attribute
¶
ContentBlock = (
TextContentBlock
| InvalidToolCall
| ReasoningContentBlock
| NonStandardContentBlock
| DataContentBlock
| ToolContentBlock
)
A union of all defined ContentBlock
types and aliases.
TextContentBlock
¶
Bases: TypedDict
Text output from a LLM.
This typically represents the main text content of a message, such as the response from a language model or the text of a user message.
Factory function
create_text_block
may also be used as a factory to create a
TextContentBlock
. Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
text |
Block text.
TYPE:
|
annotations |
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
extras |
Provider-specific metadata.
TYPE:
|
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
annotations
instance-attribute
¶
annotations: NotRequired[list[Annotation]]
Citation
s and other annotations.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
Annotation
module-attribute
¶
Annotation = Citation | NonStandardAnnotation
A union of all defined Annotation
types.
Citation
¶
Bases: TypedDict
Annotation for citing data from a document.
Note
start
/end
indices refer to the response text,
not the source text. This means that the indices are relative to the model's
response, not the original document (as specified in the url
).
Factory function
create_citation
may also be used as a factory to create a Citation
.
Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
url |
URL of the document source.
TYPE:
|
title |
Source document title.
TYPE:
|
start_index |
Start index of the response text (
TYPE:
|
end_index |
End index of the response text (
TYPE:
|
cited_text |
Excerpt of source text being cited.
TYPE:
|
extras |
Provider-specific metadata.
TYPE:
|
type
instance-attribute
¶
type: Literal['citation']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
title
instance-attribute
¶
title: NotRequired[str]
Source document title.
For example, the page title for a web page or the title of a paper.
start_index
instance-attribute
¶
start_index: NotRequired[int]
Start index of the response text (TextContentBlock.text
).
end_index
instance-attribute
¶
end_index: NotRequired[int]
End index of the response text (TextContentBlock.text
)
NonStandardAnnotation
¶
Bases: TypedDict
Provider-specific annotation format.
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
value |
Provider-specific annotation data. |
type
instance-attribute
¶
type: Literal['non_standard_annotation']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
ReasoningContentBlock
¶
Bases: TypedDict
Reasoning output from a LLM.
Factory function
create_reasoning_block
may also be used as a factory to create a
ReasoningContentBlock
. Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
reasoning |
Reasoning text.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
extras |
Provider-specific metadata.
TYPE:
|
type
instance-attribute
¶
type: Literal['reasoning']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
reasoning
instance-attribute
¶
reasoning: NotRequired[str]
Reasoning text.
Either the thought summary or the raw reasoning text itself. This is often parsed
from <think>
tags in the model's response.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
DataContentBlock
module-attribute
¶
DataContentBlock = (
ImageContentBlock
| VideoContentBlock
| AudioContentBlock
| PlainTextContentBlock
| FileContentBlock
)
A union of all defined multimodal data ContentBlock
types.
ImageContentBlock
¶
Bases: TypedDict
Image data.
Factory function
create_image_block
may also be used as a factory to create a
ImageContentBlock
. Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
file_id |
ID of the image file, e.g., from a file storage system.
TYPE:
|
mime_type |
MIME type of the image. Required for base64.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
url |
URL of the image.
TYPE:
|
base64 |
Data as a base64 string.
TYPE:
|
extras |
Provider-specific metadata. This shouldn't be used for the image data itself.
TYPE:
|
type
instance-attribute
¶
type: Literal['image']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
file_id
instance-attribute
¶
file_id: NotRequired[str]
ID of the image file, e.g., from a file storage system.
mime_type
instance-attribute
¶
mime_type: NotRequired[str]
MIME type of the image. Required for base64.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
extras
instance-attribute
¶
extras: NotRequired[dict[str, Any]]
Provider-specific metadata. This shouldn't be used for the image data itself.
VideoContentBlock
¶
Bases: TypedDict
Video data.
Factory function
create_video_block
may also be used as a factory to create a
VideoContentBlock
. Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
file_id |
ID of the video file, e.g., from a file storage system.
TYPE:
|
mime_type |
MIME type of the video. Required for base64.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
url |
URL of the video.
TYPE:
|
base64 |
Data as a base64 string.
TYPE:
|
extras |
Provider-specific metadata. This shouldn't be used for the video data itself.
TYPE:
|
type
instance-attribute
¶
type: Literal['video']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
file_id
instance-attribute
¶
file_id: NotRequired[str]
ID of the video file, e.g., from a file storage system.
mime_type
instance-attribute
¶
mime_type: NotRequired[str]
MIME type of the video. Required for base64.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
extras
instance-attribute
¶
extras: NotRequired[dict[str, Any]]
Provider-specific metadata. This shouldn't be used for the video data itself.
AudioContentBlock
¶
Bases: TypedDict
Audio data.
Factory function
create_audio_block
may also be used as a factory to create an
AudioContentBlock
. Benefits include:
* Automatic ID generation (when not provided)
* Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
file_id |
ID of the audio file, e.g., from a file storage system.
TYPE:
|
mime_type |
MIME type of the audio. Required for base64.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
url |
URL of the audio.
TYPE:
|
base64 |
Data as a base64 string.
TYPE:
|
extras |
Provider-specific metadata. This shouldn't be used for the audio data itself.
TYPE:
|
type
instance-attribute
¶
type: Literal['audio']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
file_id
instance-attribute
¶
file_id: NotRequired[str]
ID of the audio file, e.g., from a file storage system.
mime_type
instance-attribute
¶
mime_type: NotRequired[str]
MIME type of the audio. Required for base64.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
extras
instance-attribute
¶
extras: NotRequired[dict[str, Any]]
Provider-specific metadata. This shouldn't be used for the audio data itself.
PlainTextContentBlock
¶
Bases: TypedDict
Plaintext data (e.g., from a document).
Note
A PlainTextContentBlock
existed in langchain-core<1.0.0
. Although the
name has carried over, the structure has changed significantly. The only shared
keys between the old and new versions are type
and text
, though the
type
value has changed from 'text'
to 'text-plain'
.
Note
Title and context are optional fields that may be passed to the model. See Anthropic example.
Factory function
create_plaintext_block
may also be used as a factory to create a
PlainTextContentBlock
. Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
file_id |
ID of the plaintext file, e.g., from a file storage system.
TYPE:
|
mime_type |
MIME type of the file. Required for base64.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
url |
URL of the plaintext.
TYPE:
|
base64 |
Data as a base64 string.
TYPE:
|
text |
Plaintext content. This is optional if the data is provided as base64.
TYPE:
|
title |
Title of the text data, e.g., the title of a document.
TYPE:
|
context |
Context for the text, e.g., a description or summary of the text's content.
TYPE:
|
extras |
Provider-specific metadata. This shouldn't be used for the data itself.
TYPE:
|
type
instance-attribute
¶
type: Literal['text-plain']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
file_id
instance-attribute
¶
file_id: NotRequired[str]
ID of the plaintext file, e.g., from a file storage system.
mime_type
instance-attribute
¶
mime_type: Literal['text/plain']
MIME type of the file. Required for base64.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
text
instance-attribute
¶
text: NotRequired[str]
Plaintext content. This is optional if the data is provided as base64.
title
instance-attribute
¶
title: NotRequired[str]
Title of the text data, e.g., the title of a document.
context
instance-attribute
¶
context: NotRequired[str]
Context for the text, e.g., a description or summary of the text's content.
extras
instance-attribute
¶
extras: NotRequired[dict[str, Any]]
Provider-specific metadata. This shouldn't be used for the data itself.
FileContentBlock
¶
Bases: TypedDict
File data that doesn't fit into other multimodal block types.
This block is intended for files that are not images, audio, or plaintext. For example, it can be used for PDFs, Word documents, etc.
If the file is an image, audio, or plaintext, you should use the corresponding
content block type (e.g., ImageContentBlock
, AudioContentBlock
,
PlainTextContentBlock
).
Factory function
create_file_block
may also be used as a factory to create a
FileContentBlock
. Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
file_id |
ID of the file, e.g., from a file storage system.
TYPE:
|
mime_type |
MIME type of the file. Required for base64.
TYPE:
|
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
url |
URL of the file.
TYPE:
|
base64 |
Data as a base64 string.
TYPE:
|
extras |
Provider-specific metadata. This shouldn't be used for the file data itself.
TYPE:
|
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
file_id
instance-attribute
¶
file_id: NotRequired[str]
ID of the file, e.g., from a file storage system.
mime_type
instance-attribute
¶
mime_type: NotRequired[str]
MIME type of the file. Required for base64.
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
extras
instance-attribute
¶
extras: NotRequired[dict[str, Any]]
Provider-specific metadata. This shouldn't be used for the file data itself.
NonStandardContentBlock
¶
Bases: TypedDict
Provider-specific data.
This block contains data for which there is not yet a standard type.
The purpose of this block should be to simply hold a provider-specific payload.
If a provider's non-standard output includes reasoning and tool calls, it should be
the adapter's job to parse that payload and emit the corresponding standard
ReasoningContentBlock
and ToolCalls
.
Has no extras
field, as provider-specific data should be included in the
value
field.
Factory function
create_non_standard_block
may also be used as a factory to create a
NonStandardContentBlock
. Benefits include:
- Automatic ID generation (when not provided)
- Required arguments strictly validated at creation time
ATTRIBUTE | DESCRIPTION |
---|---|
type |
Type of the content block. Used for discrimination.
TYPE:
|
id |
Content block identifier.
TYPE:
|
value |
Provider-specific data. |
index |
Index of block in aggregate response. Used during streaming.
TYPE:
|
type
instance-attribute
¶
type: Literal['non_standard']
Type of the content block. Used for discrimination.
id
instance-attribute
¶
id: NotRequired[str]
Content block identifier.
Either:
- Generated by the provider (e.g., OpenAI's file ID)
- Generated by LangChain upon creation (
UUID4
prefixed with'lc_'
))
index
instance-attribute
¶
index: NotRequired[int | str]
Index of block in aggregate response. Used during streaming.
trim_messages
¶
trim_messages(
messages: Iterable[MessageLikeRepresentation] | PromptValue,
*,
max_tokens: int,
token_counter: (
Callable[[list[BaseMessage]], int]
| Callable[[BaseMessage], int]
| BaseLanguageModel
),
strategy: Literal["first", "last"] = "last",
allow_partial: bool = False,
end_on: str | type[BaseMessage] | Sequence[str | type[BaseMessage]] | None = None,
start_on: str | type[BaseMessage] | Sequence[str | type[BaseMessage]] | None = None,
include_system: bool = False,
text_splitter: Callable[[str], list[str]] | TextSplitter | None = None
) -> list[BaseMessage]
Trim messages to be below a token count.
trim_messages
can be used to reduce the size of a chat history to a specified
token or message count.
In either case, if passing the trimmed chat history back into a chat model directly, the resulting chat history should usually satisfy the following properties:
- The resulting chat history should be valid. Most chat models expect that chat
history starts with either (1) a
HumanMessage
or (2) aSystemMessage
followed by aHumanMessage
. To achieve this, setstart_on='human'
. In addition, generally aToolMessage
can only appear after anAIMessage
that involved a tool call. - It includes recent messages and drops old messages in the chat history.
To achieve this set the
strategy='last'
. - Usually, the new chat history should include the
SystemMessage
if it was present in the original chat history since theSystemMessage
includes special instructions to the chat model. TheSystemMessage
is almost always the first message in the history if present. To achieve this set theinclude_system=True
.
Note
The examples below show how to configure trim_messages
to achieve a behavior
consistent with the above properties.
PARAMETER | DESCRIPTION |
---|---|
messages
|
Sequence of Message-like objects to trim.
TYPE:
|
max_tokens
|
Max token count of trimmed messages.
TYPE:
|
token_counter
|
Function or llm for counting tokens in a Note Use
TYPE:
|
strategy
|
Strategy for trimming.
-
TYPE:
|
allow_partial
|
Whether to split a message if only part of the message can be
included. If
TYPE:
|
end_on
|
The message type to end on. If specified then every message after the
last occurrence of this type is ignored. If
TYPE:
|
start_on
|
The message type to start on. Should only be specified if
TYPE:
|
include_system
|
Whether to keep the
TYPE:
|
text_splitter
|
Function or
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list[BaseMessage]
|
List of trimmed |
RAISES | DESCRIPTION |
---|---|
ValueError
|
if two incompatible arguments are specified or an unrecognized
|
Example
Trim chat history based on token count, keeping the SystemMessage
if
present, and ensuring that the chat history starts with a HumanMessage
(
or a SystemMessage
followed by a HumanMessage
).
from langchain_core.messages import (
AIMessage,
HumanMessage,
BaseMessage,
SystemMessage,
trim_messages,
)
messages = [
SystemMessage("you're a good assistant, you always respond with a joke."),
HumanMessage("i wonder why it's called langchain"),
AIMessage(
'Well, I guess they thought "WordRope" and "SentenceString" just '
"didn't have the same ring to it!"
),
HumanMessage("and who is harrison chasing anyways"),
AIMessage(
"Hmmm let me think.\n\nWhy, he's probably chasing after the last "
"cup of coffee in the office!"
),
HumanMessage("what do you call a speechless parrot"),
]
trim_messages(
messages,
max_tokens=45,
strategy="last",
token_counter=ChatOpenAI(model="gpt-4o"),
# Most chat models expect that chat history starts with either:
# (1) a HumanMessage or
# (2) a SystemMessage followed by a HumanMessage
start_on="human",
# Usually, we want to keep the SystemMessage
# if it's present in the original history.
# The SystemMessage has special instructions for the model.
include_system=True,
allow_partial=False,
)
[
SystemMessage(
content="you're a good assistant, you always respond with a joke."
),
HumanMessage(content="what do you call a speechless parrot"),
]
Trim chat history based on the message count, keeping the SystemMessage
if
present, and ensuring that the chat history starts with a HumanMessage
(
or a SystemMessage
followed by a HumanMessage
).
trim_messages(
messages,
# When `len` is passed in as the token counter function,
# max_tokens will count the number of messages in the chat history.
max_tokens=4,
strategy="last",
# Passing in `len` as a token counter function will
# count the number of messages in the chat history.
token_counter=len,
# Most chat models expect that chat history starts with either:
# (1) a HumanMessage or
# (2) a SystemMessage followed by a HumanMessage
start_on="human",
# Usually, we want to keep the SystemMessage
# if it's present in the original history.
# The SystemMessage has special instructions for the model.
include_system=True,
allow_partial=False,
)
[
SystemMessage(
content="you're a good assistant, you always respond with a joke."
),
HumanMessage(content="and who is harrison chasing anyways"),
AIMessage(
content="Hmmm let me think.\n\nWhy, he's probably chasing after "
"the last cup of coffee in the office!"
),
HumanMessage(content="what do you call a speechless parrot"),
]
messages = [
SystemMessage("This is a 4 token text. The full message is 10 tokens."),
HumanMessage(
"This is a 4 token text. The full message is 10 tokens.", id="first"
),
AIMessage(
[
{"type": "text", "text": "This is the FIRST 4 token block."},
{"type": "text", "text": "This is the SECOND 4 token block."},
],
id="second",
),
HumanMessage(
"This is a 4 token text. The full message is 10 tokens.", id="third"
),
AIMessage(
"This is a 4 token text. The full message is 10 tokens.",
id="fourth",
),
]
def dummy_token_counter(messages: list[BaseMessage]) -> int:
# treat each message like it adds 3 default tokens at the beginning
# of the message and at the end of the message. 3 + 4 + 3 = 10 tokens
# per message.
default_content_len = 4
default_msg_prefix_len = 3
default_msg_suffix_len = 3
count = 0
for msg in messages:
if isinstance(msg.content, str):
count += (
default_msg_prefix_len
+ default_content_len
+ default_msg_suffix_len
)
if isinstance(msg.content, list):
count += (
default_msg_prefix_len
+ len(msg.content) * default_content_len
+ default_msg_suffix_len
)
return count
First 30 tokens, allowing partial messages: