1616)
1717from haystack .dataclasses .streaming_chunk import FinishReason , StreamingChunk , SyncStreamingCallbackT
1818from haystack .tools import (
19- Tool ,
20- Toolset ,
19+ ToolsType ,
2120 _check_duplicate_tool_names ,
2221 deserialize_tools_or_toolset_inplace ,
22+ flatten_tools_or_toolsets ,
2323 serialize_tools_or_toolset ,
2424)
2525from haystack .utils import deserialize_callable , serialize_callable
@@ -196,7 +196,7 @@ def __init__(
196196 model_kwargs : Optional [Dict [str , Any ]] = None ,
197197 generation_kwargs : Optional [Dict [str , Any ]] = None ,
198198 * ,
199- tools : Optional [Union [ List [ Tool ], Toolset ] ] = None ,
199+ tools : Optional [ToolsType ] = None ,
200200 streaming_callback : Optional [StreamingCallbackT ] = None ,
201201 chat_handler_name : Optional [str ] = None ,
202202 model_clip_path : Optional [str ] = None ,
@@ -215,8 +215,8 @@ def __init__(
215215 For more information on the available kwargs, see
216216 [llama.cpp documentation](https://llama-cpp-python.readthedocs.io/en/latest/api-reference/#llama_cpp.Llama.create_chat_completion).
217217 :param tools:
218- A list of tools or a Toolset for which the model can prepare calls.
219- This parameter can accept either a list of `Tool` objects or a `Toolset` instance .
218+ A list of Tool and/ or Toolset objects, or a single Toolset for which the model can prepare calls.
219+ Each tool should have a unique name .
220220 :param streaming_callback: A callback function that is called when a new token is received from the stream.
221221 :param chat_handler_name: Name of the chat handler for multimodal models.
222222 Common options include: "Llava16ChatHandler", "MoondreamChatHandler", "Qwen25VLChatHandler".
@@ -235,7 +235,7 @@ def __init__(
235235 model_kwargs .setdefault ("n_ctx" , n_ctx )
236236 model_kwargs .setdefault ("n_batch" , n_batch )
237237
238- _check_duplicate_tool_names (list (tools or [] ))
238+ _check_duplicate_tool_names (flatten_tools_or_toolsets (tools ))
239239
240240 handler : Optional [Llava15ChatHandler ] = None
241241 # Validate multimodal requirements
@@ -325,7 +325,7 @@ def run(
325325 messages : List [ChatMessage ],
326326 generation_kwargs : Optional [Dict [str , Any ]] = None ,
327327 * ,
328- tools : Optional [Union [ List [ Tool ], Toolset ] ] = None ,
328+ tools : Optional [ToolsType ] = None ,
329329 streaming_callback : Optional [StreamingCallbackT ] = None ,
330330 ) -> Dict [str , List [ChatMessage ]]:
331331 """
@@ -337,8 +337,9 @@ def run(
337337 For more information on the available kwargs, see
338338 [llama.cpp documentation](https://llama-cpp-python.readthedocs.io/en/latest/api-reference/#llama_cpp.Llama.create_chat_completion).
339339 :param tools:
340- A list of tools or a Toolset for which the model can prepare calls. If set, it will override the `tools`
341- parameter set during component initialization.
340+ A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
341+ Each tool should have a unique name. If set, it will override the `tools` parameter set during
342+ component initialization.
342343 :param streaming_callback: A callback function that is called when a new token is received from the stream.
343344 If set, it will override the `streaming_callback` parameter set during component initialization.
344345 :returns: A dictionary with the following keys:
@@ -355,13 +356,12 @@ def run(
355356 formatted_messages = [_convert_message_to_llamacpp_format (msg ) for msg in messages ]
356357
357358 tools = tools or self .tools
358- if isinstance (tools , Toolset ):
359- tools = list (tools )
360- _check_duplicate_tool_names (tools )
359+ flattened_tools = flatten_tools_or_toolsets (tools )
360+ _check_duplicate_tool_names (flattened_tools )
361361
362362 llamacpp_tools : List [ChatCompletionTool ] = []
363- if tools :
364- for t in tools :
363+ if flattened_tools :
364+ for t in flattened_tools :
365365 llamacpp_tools .append (
366366 {
367367 "type" : "function" ,
0 commit comments