Home / Function/ get_buffer_string() — langchain Function Reference

get_buffer_string() — langchain Function Reference

Architecture documentation for the get_buffer_string() function in utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  cbb21a1a_871c_8af4_2881_e51d99e3c0a1["get_buffer_string()"]
  0b528c80_0ce7_1c74_8932_bc433bcb03c6["utils.py"]
  cbb21a1a_871c_8af4_2881_e51d99e3c0a1 -->|defined in| 0b528c80_0ce7_1c74_8932_bc433bcb03c6
  1af2071a_e678_b8cd_900b_54874aebb4bc["_get_message_type_str()"]
  cbb21a1a_871c_8af4_2881_e51d99e3c0a1 -->|calls| 1af2071a_e678_b8cd_900b_54874aebb4bc
  8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd["_format_content_block_xml()"]
  cbb21a1a_871c_8af4_2881_e51d99e3c0a1 -->|calls| 8c272a75_04d0_f5f2_a5cd_cd4a21d4dfdd
  style cbb21a1a_871c_8af4_2881_e51d99e3c0a1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/utils.py lines 287–507

def get_buffer_string(
    messages: Sequence[BaseMessage],
    human_prefix: str = "Human",
    ai_prefix: str = "AI",
    *,
    system_prefix: str = "System",
    function_prefix: str = "Function",
    tool_prefix: str = "Tool",
    message_separator: str = "\n",
    format: Literal["prefix", "xml"] = "prefix",  # noqa: A002
) -> str:
    r"""Convert a sequence of messages to strings and concatenate them into one string.

    Args:
        messages: Messages to be converted to strings.
        human_prefix: The prefix to prepend to contents of `HumanMessage`s.
        ai_prefix: The prefix to prepend to contents of `AIMessage`.
        system_prefix: The prefix to prepend to contents of `SystemMessage`s.
        function_prefix: The prefix to prepend to contents of `FunctionMessage`s.
        tool_prefix: The prefix to prepend to contents of `ToolMessage`s.
        message_separator: The separator to use between messages.
        format: 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.

    Returns:
        A single string concatenation of all input messages.

    Raises:
        ValueError: If an unsupported message type is encountered.

    !!! warning

        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.

    !!! note "XML format"

        When using `format='xml'`:

        - All messages use uniform `<message type="role">content</message>` format.
        - The `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`.
        - Message content is escaped using `xml.sax.saxutils.escape()`.
        - Attribute values are escaped using `xml.sax.saxutils.quoteattr()`.
        - AI messages with tool calls use nested structure with `<content>` and
            `<tool_call>` elements.
        - For multi-modal content (list of content blocks), supported block types
            are: `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`.
        - Content blocks with base64-encoded data are skipped (including blocks
            with `base64` field or `data:` URLs).
        - Unknown block types are skipped.
        - Plain text document content (`text-plain`), server tool call arguments,
            and server tool result outputs are truncated to 500 characters.

    Example:
        Default prefix format:

        ```python
        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):

        ```python
        messages = [

Subdomains

Frequently Asked Questions

What does get_buffer_string() do?
get_buffer_string() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/utils.py.
Where is get_buffer_string() defined?
get_buffer_string() is defined in libs/core/langchain_core/messages/utils.py at line 287.
What does get_buffer_string() call?
get_buffer_string() calls 2 function(s): _format_content_block_xml, _get_message_type_str.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free