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.

Parameters:
  • messages (Sequence[BaseMessage]) – Messages to be converted to strings.

  • human_prefix (str) – The prefix to prepend to contents of HumanMessages. Default is β€œHuman”.

  • ai_prefix (str) – 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.

Return type:

str

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?\nAI: Good, how are you?"