Skip to content

Commit 1401c51

Browse files
sjrljulian-risch
authored andcommitted
fix: Move deserialize_tools_inplace to original import path (#9301)
* Fix for preserving import path * Move deserialize_tools_inplace back to original import path * Add reno * Make mypy happy * Remove unused import
1 parent 7b42b60 commit 1401c51

5 files changed

Lines changed: 46 additions & 29 deletions

File tree

haystack/components/generators/openai_dalle.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,14 @@ def run(
127127
response = self.client.images.generate(
128128
model=self.model, prompt=prompt, size=size, quality=quality, response_format=response_format, n=1
129129
)
130-
image: Image = response.data[0]
131-
image_str = image.url or image.b64_json or ""
132-
return {"images": [image_str], "revised_prompt": image.revised_prompt or ""}
130+
if response.data is not None:
131+
image: Image = response.data[0]
132+
image_str = image.url or image.b64_json or ""
133+
revised_prompt = image.revised_prompt or ""
134+
else:
135+
image_str = ""
136+
revised_prompt = ""
137+
return {"images": [image_str], "revised_prompt": revised_prompt}
133138

134139
def to_dict(self) -> Dict[str, Any]:
135140
"""

haystack/tools/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
# ruff: noqa: I001 (ignore import order as we need to import Tool before ComponentTool)
88
from .from_function import create_tool_from_function, tool
9-
from .tool import Tool, _check_duplicate_tool_names
9+
from .tool import Tool, _check_duplicate_tool_names, deserialize_tools_inplace
1010
from .component_tool import ComponentTool
11-
from .serde_utils import deserialize_tools_or_toolset_inplace, serialize_tools_or_toolset, deserialize_tools_inplace
11+
from .serde_utils import deserialize_tools_or_toolset_inplace, serialize_tools_or_toolset
1212
from .toolset import Toolset
1313

1414
__all__ = [

haystack/tools/serde_utils.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,27 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
import warnings
6-
from typing import Any, Dict, List, Union
5+
from typing import TYPE_CHECKING, Any, Dict, List, Union
76

87
from haystack.core.errors import DeserializationError
98
from haystack.core.serialization import import_class_by_name
10-
from haystack.tools.tool import Tool
11-
from haystack.tools.toolset import Toolset
9+
10+
if TYPE_CHECKING:
11+
from haystack.tools.tool import Tool
12+
from haystack.tools.toolset import Toolset
1213

1314

1415
def serialize_tools_or_toolset(
15-
tools: Union[Toolset, List[Tool], None],
16+
tools: Union["Toolset", List["Tool"], None],
1617
) -> Union[Dict[str, Any], List[Dict[str, Any]], None]:
1718
"""
1819
Serialize a Toolset or a list of Tools to a dictionary or a list of tool dictionaries.
1920
2021
:param tools: A Toolset, a list of Tools, or None
2122
:returns: A dictionary, a list of tool dictionaries, or None if tools is None
2223
"""
24+
from haystack.tools.toolset import Toolset
25+
2326
if tools is None:
2427
return None
2528
if isinstance(tools, Toolset):
@@ -36,6 +39,9 @@ def deserialize_tools_or_toolset_inplace(data: Dict[str, Any], key: str = "tools
3639
:param key:
3740
The key in the dictionary where the list of Tools or Toolset is stored.
3841
"""
42+
from haystack.tools.tool import Tool
43+
from haystack.tools.toolset import Toolset
44+
3945
if key in data:
4046
serialized_tools = data[key]
4147

@@ -72,22 +78,3 @@ def deserialize_tools_or_toolset_inplace(data: Dict[str, Any], key: str = "tools
7278
deserialized_tools.append(tool_class.from_dict(tool))
7379

7480
data[key] = deserialized_tools
75-
76-
77-
def deserialize_tools_inplace(data: Dict[str, Any], key: str = "tools"):
78-
"""
79-
Deserialize a list of Tools or a Toolset in a dictionary inplace.
80-
81-
Deprecated in favor of `deserialize_tools_or_toolset_inplace`. It will be removed in Haystack 2.14.0.
82-
83-
:param data:
84-
The dictionary with the serialized data.
85-
:param key:
86-
The key in the dictionary where the list of Tools or Toolset is stored.
87-
"""
88-
warnings.warn(
89-
"`deserialize_tools_inplace` is deprecated and will be removed in Haystack 2.14.0. "
90-
"Use `deserialize_tools_or_toolset_inplace` instead.",
91-
DeprecationWarning,
92-
)
93-
deserialize_tools_or_toolset_inplace(data, key)

haystack/tools/tool.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
import warnings
56
from dataclasses import asdict, dataclass
67
from typing import Any, Callable, Dict, List, Optional
78

@@ -10,6 +11,7 @@
1011

1112
from haystack.core.serialization import generate_qualified_class_name
1213
from haystack.tools.errors import ToolInvocationError
14+
from haystack.tools.serde_utils import deserialize_tools_or_toolset_inplace
1315
from haystack.utils import deserialize_callable, serialize_callable
1416

1517

@@ -173,3 +175,22 @@ def _check_duplicate_tool_names(tools: Optional[List[Tool]]) -> None:
173175
duplicate_tool_names = {name for name in tool_names if tool_names.count(name) > 1}
174176
if duplicate_tool_names:
175177
raise ValueError(f"Duplicate tool names found: {duplicate_tool_names}")
178+
179+
180+
def deserialize_tools_inplace(data: Dict[str, Any], key: str = "tools"):
181+
"""
182+
Deserialize a list of Tools or a Toolset in a dictionary inplace.
183+
184+
Deprecated in favor of `deserialize_tools_or_toolset_inplace`. It will be removed in Haystack 2.14.0.
185+
186+
:param data:
187+
The dictionary with the serialized data.
188+
:param key:
189+
The key in the dictionary where the list of Tools or Toolset is stored.
190+
"""
191+
warnings.warn(
192+
"`deserialize_tools_inplace` is deprecated and will be removed in Haystack 2.14.0. "
193+
"Use `deserialize_tools_or_toolset_inplace` instead.",
194+
DeprecationWarning,
195+
)
196+
deserialize_tools_or_toolset_inplace(data, key)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Move deserialize_tools_inplace back to original import path of from haystack.tools.tool import deserialize_tools_inplace.

0 commit comments

Comments
 (0)