Skip to content
Closed
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
3 changes: 2 additions & 1 deletion haystack/tools/toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def __contains__(self, item: Any) -> bool:
return item in self.tools
return False

def add(self, tool: Union[Tool, "Toolset"]) -> None:
def add(self, tool: Union[Tool, "Toolset"]) -> "Toolset":
"""
Add a new Tool or merge another Toolset.

Expand All @@ -207,6 +207,7 @@ def add(self, tool: Union[Tool, "Toolset"]) -> None:
_check_duplicate_tool_names(combined_tools)

self.tools.extend(new_tools)
return self

def to_dict(self) -> Dict[str, Any]:
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

enhancements:
- |
Made `Toolset.add()` method chainable and ensured it flattens nested toolsets. This allows chaining calls like `t.add(a).add(b)` and adds all tools correctly.
29 changes: 29 additions & 0 deletions test/tools/test_toolset.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,32 @@ def test_toolinvoker_deserialization_with_custom_toolset(self, weather_tool):
assert deserialized_invoker._tools_with_names == invoker._tools_with_names
assert deserialized_invoker.raise_on_failure == invoker.raise_on_failure
assert deserialized_invoker.convert_result_to_json_string == invoker.convert_result_to_json_string

def test_toolset_add_can_flatten_nested_toolsets(self):
tool_a = Tool(
name="add",
description="Add two numbers",
parameters={
"type": "object",
"properties": {"a": {"type": "integer"}, "b": {"type": "integer"}},
"required": ["a", "b"],
},
function=add_numbers,
)
tool_b = Tool(
name="multiply",
description="Multiply two numbers",
parameters={
"type": "object",
"properties": {"a": {"type": "integer"}, "b": {"type": "integer"}},
"required": ["a", "b"],
},
function=multiply_numbers,
)
tset_a = Toolset([tool_a])
tset_b = Toolset([tool_b])

master = Toolset()
master.add(tset_a).add(tset_b)

assert {t.name for t in master} == {"add", "multiply"}
Loading