The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(core): abbreviate long tool IDs in XML buffer strings (#40792)
ccurme19cadaa1a1e0
diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.pyindex aa87e4f92a..dd78b1e02c 100644--- a/libs/core/langchain_core/messages/utils.py+++ b/libs/core/langchain_core/messages/utils.py@@ -284,6 +284,16 @@ def _get_message_type_str(raise ValueError(msg)+_TOOL_CALL_ID_DISPLAY_LIMIT = 64+++def _display_tool_call_id(tool_call_id: str) -> str:+ """Abbreviate long tool-call IDs in formatted message strings."""+ if len(tool_call_id) > _TOOL_CALL_ID_DISPLAY_LIMIT:+ return f"{tool_call_id[:_TOOL_CALL_ID_DISPLAY_LIMIT]}..."+ return tool_call_id++def get_buffer_string(messages: Sequence[BaseMessage],human_prefix: str = "Human",@@ -327,6 +337,9 @@ def get_buffer_string(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 toolcalls will be appended to the string representation.+ In XML format, tool-call IDs longer than 64 characters are displayed+ as the first 64 characters followed by `...`; the original messages+ are not changed.!!! note "XML format"@@ -469,7 +482,7 @@ def get_buffer_string(if has_tool_calls:for tc in ai_msg.tool_calls:- tc_id = quoteattr(str(tc.get("id") or ""))+ tc_id = quoteattr(_display_tool_call_id(tc.get("id") or ""))tc_name = quoteattr(str(tc.get("name") or ""))tc_args = escape(json.dumps(tc.get("args", {}), ensure_ascii=False)diff --git a/libs/core/tests/unit_tests/messages/test_utils.py b/libs/core/tests/unit_tests/messages/test_utils.pyindex 13c0842ef9..ce44968606 100644--- a/libs/core/tests/unit_tests/messages/test_utils.py+++ b/libs/core/tests/unit_tests/messages/test_utils.py@@ -1792,6 +1792,30 @@ def test_get_buffer_string_with_tool_calls() -> None:assert "NYC" in result+def test_get_buffer_string_abbreviates_long_tool_call_ids_in_xml_only() -> None:+ """Long IDs are shortened in display text without modifying protocol messages."""+ long_id = "call_" + "thought_signature" * 100+ message = AIMessage(+ content="calling",+ tool_calls=[+ {"name": "search", "args": {"query": "weather"}, "id": long_id},+ {"name": "search", "args": {}, "id": "call_short"},+ ],+ )+ result = ToolMessage(content="result", tool_call_id=long_id)++ rendered = get_buffer_string([message, result], format="xml")+ prefix = get_buffer_string([message, result])++ assert f"{long_id[:64]}..." in rendered+ assert long_id not in rendered+ assert "call_short" in rendered+ assert "result" in rendered+ assert long_id in prefix+ assert message.tool_calls[0]["id"] == long_id+ assert result.tool_call_id == long_id++def test_get_buffer_string_with_tool_calls_empty_content() -> None:"""Test `get_buffer_string` with `tool_calls` and empty `content`."""messages = [