Skip to content

The agent engineering platform.

fix(core): use tool_calls instead of deprecated function_call in get_buffer_string (#34355)

rari404d84eef667af9
diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.py
index 776f63e724..8146c2d35b 100644
--- a/libs/core/langchain_core/messages/utils.py
+++ b/libs/core/langchain_core/messages/utils.py
@@ -109,6 +109,11 @@ def get_buffer_string(
Raises:
ValueError: If an unsupported message type is encountered.
+ Note:
+ 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.
+
Example:
```python
from langchain_core import AIMessage, HumanMessage
@@ -139,8 +144,12 @@ def get_buffer_string(
msg = f"Got unsupported message type: {m}"
raise ValueError(msg) # noqa: TRY004
message = f"{role}: {m.text}"
- if isinstance(m, AIMessage) and "function_call" in m.additional_kwargs:
- message += f"{m.additional_kwargs['function_call']}"
+ if isinstance(m, AIMessage):
+ if m.tool_calls:
+ message += f"{m.tool_calls}"
+ elif "function_call" in m.additional_kwargs:
+ # Legacy behavior assumes only one function call per message
+ message += f"{m.additional_kwargs['function_call']}"
string_messages.append(message)
return "\n".join(string_messages)
diff --git a/libs/core/tests/unit_tests/messages/test_utils.py b/libs/core/tests/unit_tests/messages/test_utils.py
index c41cb9d65e..3aa27351c0 100644
--- a/libs/core/tests/unit_tests/messages/test_utils.py
+++ b/libs/core/tests/unit_tests/messages/test_utils.py
@@ -1652,6 +1652,72 @@ def test_get_buffer_string_with_empty_content() -> None:
assert actual == expected
+def test_get_buffer_string_with_tool_calls() -> None:
+ """Test `get_buffer_string` with `tool_calls` field."""
+ messages = [
+ HumanMessage(content="What's the weather?"),
+ AIMessage(
+ content="Let me check the weather",
+ tool_calls=[
+ {
+ "name": "get_weather",
+ "args": {"city": "NYC"},
+ "id": "call_1",
+ "type": "tool_call",
+ }
+ ],
+ ),
+ ]
+ result = get_buffer_string(messages)
+ assert "Human: What's the weather?" in result
+ assert "AI: Let me check the weather" in result
+ assert "get_weather" in result
+ assert "NYC" in result
+
+
+def test_get_buffer_string_with_tool_calls_empty_content() -> None:
+ """Test `get_buffer_string` with `tool_calls` and empty `content`."""
+ messages = [
+ AIMessage(
+ content="",
+ tool_calls=[
+ {
+ "name": "search",
+ "args": {"query": "test"},
+ "id": "call_2",
+ "type": "tool_call",
+ }
+ ],
+ ),
+ ]
+ result = get_buffer_string(messages)
+ assert "AI: " in result
+ assert "search" in result
+
+
+def test_get_buffer_string_tool_calls_preferred_over_function_call() -> None:
+ """Test that `tool_calls` takes precedence over legacy `function_call`."""
+ messages = [
+ AIMessage(
+ content="Calling tools",
+ tool_calls=[
+ {
+ "name": "modern_tool",
+ "args": {"key": "value"},
+ "id": "call_3",
+ "type": "tool_call",
+ }
+ ],
+ additional_kwargs={
+ "function_call": {"name": "legacy_function", "arguments": "{}"}
+ },
+ ),
+ ]
+ result = get_buffer_string(messages)
+ assert "modern_tool" in result
+ assert "legacy_function" not in result
+
+
def test_convert_to_openai_messages_reasoning_content() -> None:
"""Test convert_to_openai_messages with reasoning content blocks."""
# Test reasoning block with empty summary