get_buffer_string(
messages: Sequence[BaseMessage],
human_prefix: str = 'Human',
ai_prefix:| Name | Type | Description |
|---|---|---|
messages* | Sequence[BaseMessage] | Messages to be converted to strings. |
human_prefix | str | Default: 'Human'The prefix to prepend to contents of |
ai_prefix | str | Default: 'AI' |
system_prefix | str | Default: 'System' |
function_prefix | str | Default: 'Function' |
tool_prefix | str | Default: 'Tool' |
message_separator | str | Default: '\n' |
format | Literal['prefix', 'xml'] | Default: 'prefix' |
Convert a sequence of messages to strings and concatenate them into one string.
If a message is an AIMessage and contains both tool calls under tool_calls
and a function call under additional_kwargs["function_call"], only the tool
calls will be appended to the string representation.
When using format='xml':
<message type="role">content</message> format.type attribute uses human_prefix (lowercased) for HumanMessage,
ai_prefix (lowercased) for AIMessage, system_prefix (lowercased)
for SystemMessage, function_prefix (lowercased) for FunctionMessage,
tool_prefix (lowercased) for ToolMessage, and the original role
(unchanged) for ChatMessage.xml.sax.saxutils.escape().xml.sax.saxutils.quoteattr().<content> and
<tool_call> elements.text, reasoning, image (URL/file_id only), image_url
(OpenAI-style, URL only), audio (URL/file_id only), video (URL/file_id
only), text-plain, server_tool_call, and server_tool_result.base64 field or data: URLs).text-plain), server tool call arguments,
and server tool result outputs are truncated to 500 characters.Example:
Default prefix format:
from langchain_core.messages import AIMessage, HumanMessage, get_buffer_string
messages = [
HumanMessage(content="Hi, how are you?"),
AIMessage(content="Good, how are you?"),
]
get_buffer_string(messages)
# -> "Human: Hi, how are you?\nAI: Good, how are you?"
XML format (useful when content contains role-like prefixes):
messages = [
HumanMessage(content="Example: Human: some text"),
AIMessage(content="I see the example."),
]
get_buffer_string(messages, format="xml")
# -> '<message type="human">Example: Human: some text</message>\\n'
# -> '<message type="ai">I see the example.</message>'
XML format with special characters (automatically escaped):
messages = [
HumanMessage(content="Is 5 < 10 & 10 > 5?"),
]
get_buffer_string(messages, format="xml")
# -> '<message type="human">Is 5 < 10 & 10 > 5?</message>'
XML format with tool calls:
messages = [
AIMessage(
content="I'll search for that.",
tool_calls=[
{"id": "call_123", "name": "search", "args": {"query": "weather"}}
],
),
]
get_buffer_string(messages, format="xml")
# -> '<message type="ai">\\n'
# -> ' <content>I\\'ll search for that.</content>\\n'
# -> ' <tool_call id="call_123" name="search">'
# -> '{"query": "weather"}</tool_call>\\n'
# -> '</message>'The prefix to prepend to contents of AIMessage.
The prefix to prepend to contents of SystemMessages.
The prefix to prepend to contents of FunctionMessages.
The prefix to prepend to contents of ToolMessages.
The separator to use between messages.
The output format. 'prefix' uses Role: content format (default).
'xml' uses XML-style <message type='role'> format with proper character
escaping, which is useful when message content may contain role-like
prefixes that could cause ambiguity.