Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion libs/core/langchain_core/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,9 @@ def _is_message_content_type(obj: Any) -> bool:
`True` if the object is valid message content, `False` otherwise.
"""
return isinstance(obj, str) or (
isinstance(obj, list) and all(_is_message_content_block(e) for e in obj)
isinstance(obj, list)
and bool(obj)
and all(_is_message_content_block(e) for e in obj)
)


Expand Down
29 changes: 28 additions & 1 deletion libs/core/tests/unit_tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2134,6 +2134,7 @@ def test__is_message_content_block(obj: Any, *, expected: bool) -> None:
("foo", True),
(valid_tool_result_blocks, True),
(invalid_tool_result_blocks, False),
([], False),
],
)
def test__is_message_content_type(obj: Any, *, expected: bool) -> None:
Expand Down Expand Up @@ -2718,7 +2719,33 @@ def foo(x: int) -> str:
)


def test_tool_decorator_description() -> None:
def test_tool_returning_empty_list_is_stringified() -> None:
"""Empty list from tool should be JSON-stringified, not passed as content blocks.

Regression test for https://github.com/langchain-ai/langchain/issues/30578
"""

class ListTool(BaseTool):
name: str = "list_tool"
description: str = "Returns a list"

def _run(self, n: int) -> list:
return [{"item": str(i)} for i in range(n)]

list_tool = ListTool()

result_one = list_tool.invoke(
{"type": "tool_call", "args": {"n": 1}, "id": "1", "name": "list_tool"}
)
assert isinstance(result_one, ToolMessage)
assert result_one.content == '[{"item": "0"}]'

result_zero = list_tool.invoke(
{"type": "tool_call", "args": {"n": 0}, "id": "2", "name": "list_tool"}
)
assert isinstance(result_zero, ToolMessage)
assert result_zero.content == "[]"

# test basic tool
@tool
def foo(x: int) -> str:
Expand Down