mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-05 21:12:48 +00:00
core: fix get_buffer_string
output for structured message content (#31600)
This commit is contained in:
@@ -21,6 +21,7 @@ from langchain_core.messages.utils import (
|
||||
convert_to_openai_messages,
|
||||
count_tokens_approximately,
|
||||
filter_messages,
|
||||
get_buffer_string,
|
||||
merge_message_runs,
|
||||
trim_messages,
|
||||
)
|
||||
@@ -1395,3 +1396,64 @@ def test_count_tokens_approximately_mixed_content_types() -> None:
|
||||
|
||||
# Ensure that count is consistent if we do one message at a time
|
||||
assert sum(count_tokens_approximately([m]) for m in messages) == token_count
|
||||
|
||||
|
||||
def test_get_buffer_string_with_structured_content() -> None:
|
||||
"""Test get_buffer_string with structured content in messages."""
|
||||
messages = [
|
||||
HumanMessage(content=[{"type": "text", "text": "Hello, world!"}]),
|
||||
AIMessage(content=[{"type": "text", "text": "Hi there!"}]),
|
||||
SystemMessage(content=[{"type": "text", "text": "System message"}]),
|
||||
]
|
||||
expected = "Human: Hello, world!\nAI: Hi there!\nSystem: System message"
|
||||
actual = get_buffer_string(messages)
|
||||
assert actual == expected
|
||||
|
||||
|
||||
def test_get_buffer_string_with_mixed_content() -> None:
|
||||
"""Test get_buffer_string with mixed content types in messages."""
|
||||
messages = [
|
||||
HumanMessage(content="Simple text"),
|
||||
AIMessage(content=[{"type": "text", "text": "Structured text"}]),
|
||||
SystemMessage(content=[{"type": "text", "text": "Another structured text"}]),
|
||||
]
|
||||
expected = (
|
||||
"Human: Simple text\nAI: Structured text\nSystem: Another structured text"
|
||||
)
|
||||
actual = get_buffer_string(messages)
|
||||
assert actual == expected
|
||||
|
||||
|
||||
def test_get_buffer_string_with_function_call() -> None:
|
||||
"""Test get_buffer_string with function call in additional_kwargs."""
|
||||
messages = [
|
||||
HumanMessage(content="Hello"),
|
||||
AIMessage(
|
||||
content="Hi",
|
||||
additional_kwargs={
|
||||
"function_call": {
|
||||
"name": "test_function",
|
||||
"arguments": '{"arg": "value"}',
|
||||
}
|
||||
},
|
||||
),
|
||||
]
|
||||
# TODO: consider changing this
|
||||
expected = (
|
||||
"Human: Hello\n"
|
||||
"AI: Hi{'name': 'test_function', 'arguments': '{\"arg\": \"value\"}'}"
|
||||
)
|
||||
actual = get_buffer_string(messages)
|
||||
assert actual == expected
|
||||
|
||||
|
||||
def test_get_buffer_string_with_empty_content() -> None:
|
||||
"""Test get_buffer_string with empty content in messages."""
|
||||
messages = [
|
||||
HumanMessage(content=[]),
|
||||
AIMessage(content=""),
|
||||
SystemMessage(content=[]),
|
||||
]
|
||||
expected = "Human: \nAI: \nSystem: "
|
||||
actual = get_buffer_string(messages)
|
||||
assert actual == expected
|
||||
|
Reference in New Issue
Block a user