Coverage for haystack/tools/__init__.py: 100%

7 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-21 13:53 +0000

1# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4 

5import sys 

6from typing import TYPE_CHECKING 

7 

8from lazy_imports import LazyImporter 

9 

10# The `tool` decorator (from `from_function`) shares its name with the `tool` submodule (`tool.py`). 

11# `LazyImporter` registers every key in `import_structure` as a submodule symbol, so adding `"tool"` as a key collides 

12# with the `tool` decorator and raises a duplicate-symbol error at construction. We therefore eagerly import the 

13# decorator and pass it via `extra_objects`, keeping `"tool"` out of `import_structure`. 

14# 

15# Guidance for adding future exports: 

16# - Symbols defined in `tool.py` (e.g. `Tool`) CANNOT be lazy: doing so would require `"tool"` as a key, which 

17# re-introduces the collision with the `tool` decorator. Import them eagerly here and add them to `extra_objects`. 

18# - Symbols from `from_function.py` (e.g. `create_tool_from_function`) could be lazy, but since we already eagerly 

19# import this module for the `tool` decorator, we keep its exports eager in `extra_objects` too rather than 

20# splitting them. 

21# - Symbols from any other submodule have no name collision and should be added lazily to `import_structure` (plus 

22# the `TYPE_CHECKING` block below) like the existing entries. 

23from haystack.tools.from_function import create_tool_from_function, tool 

24from haystack.tools.tool import Tool, _check_duplicate_tool_names 

25 

26_import_structure = { 

27 "toolset": ["Toolset"], 

28 "searchable_toolset": ["SearchableToolset"], 

29 "skills": ["SkillToolset"], 

30 "component_tool": ["ComponentTool"], 

31 "pipeline_tool": ["PipelineTool"], 

32 "agent_tool": ["AgentTool"], 

33 "serde_utils": ["deserialize_tools_or_toolset_inplace", "serialize_tools_or_toolset"], 

34 "utils": ["flatten_tools_or_toolsets", "warm_up_tools"], 

35 "tool_types": ["ToolsType"], 

36} 

37 

38if TYPE_CHECKING: 

39 from haystack.tools.agent_tool import AgentTool as AgentTool 

40 from haystack.tools.component_tool import ComponentTool as ComponentTool 

41 from haystack.tools.pipeline_tool import PipelineTool as PipelineTool 

42 from haystack.tools.searchable_toolset import SearchableToolset as SearchableToolset 

43 from haystack.tools.serde_utils import deserialize_tools_or_toolset_inplace as deserialize_tools_or_toolset_inplace 

44 from haystack.tools.serde_utils import serialize_tools_or_toolset as serialize_tools_or_toolset 

45 from haystack.tools.skills import SkillToolset as SkillToolset 

46 from haystack.tools.tool_types import ToolsType as ToolsType 

47 from haystack.tools.toolset import Toolset as Toolset 

48 from haystack.tools.utils import flatten_tools_or_toolsets as flatten_tools_or_toolsets 

49 from haystack.tools.utils import warm_up_tools as warm_up_tools 

50else: 

51 sys.modules[__name__] = LazyImporter( 

52 name=__name__, 

53 module_file=__file__, 

54 import_structure=_import_structure, 

55 extra_objects={ 

56 "create_tool_from_function": create_tool_from_function, 

57 "tool": tool, 

58 "Tool": Tool, 

59 "_check_duplicate_tool_names": _check_duplicate_tool_names, 

60 }, 

61 )