messages#

Messages are objects used in prompts and chat conversations.

Class hierarchy:

BaseMessage --> SystemMessage, AIMessage, HumanMessage, ChatMessage, FunctionMessage, ToolMessage
            --> BaseMessageChunk --> SystemMessageChunk, AIMessageChunk, HumanMessageChunk, ChatMessageChunk, FunctionMessageChunk, ToolMessageChunk

Main helpers:

ChatPromptTemplate

Classes

messages.ai.AIMessage

Message from an AI.

messages.ai.AIMessageChunk

Message chunk from an AI.

messages.ai.UsageMetadata

Usage metadata for a message, such as token counts.

messages.base.BaseMessage

Base abstract message class.

messages.base.BaseMessageChunk

Message chunk, which can be concatenated with other Message chunks.

messages.chat.ChatMessage

Message that can be assigned an arbitrary speaker (i.e.

messages.chat.ChatMessageChunk

Chat Message chunk.

messages.function.FunctionMessage

Message for passing the result of executing a tool back to a model.

messages.function.FunctionMessageChunk

Function Message chunk.

messages.human.HumanMessage

Message from a human.

messages.human.HumanMessageChunk

Human Message chunk.

messages.modifier.RemoveMessage

messages.system.SystemMessage

Message for priming AI behavior.

messages.system.SystemMessageChunk

System Message chunk.

messages.tool.InvalidToolCall

Allowance for errors made by LLM.

messages.tool.ToolCall

Represents a request to call a tool.

messages.tool.ToolCallChunk

A chunk of a tool call (e.g., as part of a stream).

messages.tool.ToolMessage

Message for passing the result of executing a tool back to a model.

messages.tool.ToolMessageChunk

Tool Message chunk.

Functions

messages.ai.add_ai_message_chunks(left, *others)

Add multiple AIMessageChunks together.

messages.base.get_msg_title_repr(title, *[, ...])

Get a title representation for a message.

messages.base.merge_content(first_content, ...)

Merge two message contents.

messages.base.message_to_dict(message)

Convert a Message to a dictionary.

messages.base.messages_to_dict(messages)

Convert a sequence of Messages to a list of dictionaries.

messages.tool.default_tool_chunk_parser(...)

Best-effort parsing of tool chunks.

messages.tool.default_tool_parser(raw_tool_calls)

Best-effort parsing of tools.

messages.tool.invalid_tool_call(*[, name, ...])

messages.tool.tool_call(*, name, args, id)

messages.tool.tool_call_chunk(*[, name, ...])

messages.utils.convert_to_messages(messages)

Convert a sequence of messages to a list of messages.

messages.utils.filter_messages([messages])

Filter messages based on name, type or id.

messages.utils.get_buffer_string(messages[, ...])

Convert a sequence of Messages to strings and concatenate them into one string.

messages.utils.merge_message_runs([messages])

Merge consecutive Messages of the same type.

messages.utils.message_chunk_to_message(chunk)

Convert a message chunk to a message.

messages.utils.messages_from_dict(messages)

Convert a sequence of messages from dicts to Message objects.

messages.utils.trim_messages([messages])

Trim messages to be below a token count.