get_buffer_string#

langchain_core.messages.utils.get_buffer_string(messages: Sequence[BaseMessage], human_prefix: str = 'Human', ai_prefix: str = 'AI') β†’ str[source]#

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 HumanMessages.

Default is β€œHuman”.

ai_prefix: THe prefix to prepend to contents of AIMessages. Default is β€œAI”.

Returns:

A single string concatenation of all input messages.

Raises:

ValueError: If an unsupported message type is encountered.

Example:
from langchain_core import AIMessage, HumanMessage

messages = [
    HumanMessage(content="Hi, how are you?"),
    AIMessage(content="Good, how are you?"),
]
get_buffer_string(messages)
# -> "Human: Hi, how are you?

AI: Good, how are you?”

Parameters:
  • messages (Sequence[BaseMessage]) –

  • human_prefix (str) –

  • ai_prefix (str) –

Return type:

str