From 437fe6d2164e80699a1520f229d610c4308f8269 Mon Sep 17 00:00:00 2001 From: Vadym Barda Date: Fri, 21 Feb 2025 11:53:15 -0500 Subject: [PATCH] core[patch]: return ToolMessage from tools when tool call ID is empty string (#29921) --- libs/core/langchain_core/tools/base.py | 2 +- libs/core/tests/unit_tests/test_tools.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/libs/core/langchain_core/tools/base.py b/libs/core/langchain_core/tools/base.py index ff62f3091d4..3d98b50dda7 100644 --- a/libs/core/langchain_core/tools/base.py +++ b/libs/core/langchain_core/tools/base.py @@ -960,7 +960,7 @@ def _format_output( name: str, status: str, ) -> Union[ToolOutputMixin, Any]: - if isinstance(content, ToolOutputMixin) or not tool_call_id: + if isinstance(content, ToolOutputMixin) or tool_call_id is None: return content if not _is_message_content_type(content): content = _stringify(content) diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py index afe8c09113a..06c82946ebe 100644 --- a/libs/core/tests/unit_tests/test_tools.py +++ b/libs/core/tests/unit_tests/test_tools.py @@ -2457,3 +2457,14 @@ def test_simple_tool_args_schema_dict() -> None: assert tool.args == { "a": {"title": "A", "type": "integer"}, } + + +def test_empty_string_tool_call_id() -> None: + @tool + def foo(x: int) -> str: + """Foo.""" + return "hi" + + assert foo.invoke({"type": "tool_call", "args": {"x": 0}, "id": ""}) == ToolMessage( + content="hi", name="foo", tool_call_id="" + )