Skip to content

Commit ce0a09c

Browse files
authored
fix: Remove _trigger_tool_invoker_breakpoint since no longer needed after Haystack 2.19 was released (#386)
* Make changes based on new Haystack release * linting * fix typing issue with ToolsType * fix toolstype in openaichatgenerator
1 parent ea74456 commit ce0a09c

File tree

3 files changed

+14
-72
lines changed

3 files changed

+14
-72
lines changed

haystack_experimental/components/agents/agent.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
hs_breakpoint._create_agent_snapshot = exp_breakpoint._create_agent_snapshot
2222
hs_breakpoint._create_pipeline_snapshot_from_tool_invoker = exp_breakpoint._create_pipeline_snapshot_from_tool_invoker # type: ignore[assignment]
23-
hs_breakpoint._trigger_tool_invoker_breakpoint = exp_breakpoint._trigger_tool_invoker_breakpoint
2423

2524
from haystack import logging
2625
from haystack.components.agents.agent import Agent as HaystackAgent
@@ -39,7 +38,7 @@
3938
from haystack.dataclasses import ChatMessage
4039
from haystack.dataclasses.breakpoints import AgentBreakpoint, ToolBreakpoint
4140
from haystack.dataclasses.streaming_chunk import StreamingCallbackT
42-
from haystack.tools import Tool, Toolset, deserialize_tools_or_toolset_inplace
41+
from haystack.tools import ToolsType, deserialize_tools_or_toolset_inplace
4342
from haystack.utils.callable_serialization import deserialize_callable
4443
from haystack.utils.deserialization import deserialize_chatgenerator_inplace
4544

@@ -122,7 +121,7 @@ def __init__(
122121
self,
123122
*,
124123
chat_generator: ChatGenerator,
125-
tools: Optional[Union[list[Tool], Toolset]] = None,
124+
tools: Optional[ToolsType] = None,
126125
system_prompt: Optional[str] = None,
127126
exit_conditions: Optional[list[str]] = None,
128127
state_schema: Optional[dict[str, Any]] = None,
@@ -172,7 +171,7 @@ def _initialize_fresh_execution(
172171
requires_async: bool,
173172
*,
174173
system_prompt: Optional[str] = None,
175-
tools: Optional[Union[list[Tool], Toolset, list[str]]] = None,
174+
tools: Optional[Union[ToolsType, list[str]]] = None,
176175
**kwargs: dict[str, Any],
177176
) -> _ExecutionContext:
178177
"""
@@ -213,7 +212,7 @@ def _initialize_from_snapshot( # type: ignore[override]
213212
streaming_callback: Optional[StreamingCallbackT],
214213
requires_async: bool,
215214
*,
216-
tools: Optional[Union[list[Tool], Toolset, list[str]]] = None,
215+
tools: Optional[Union[ToolsType, list[str]]] = None,
217216
) -> _ExecutionContext:
218217
"""
219218
Initialize execution context from an AgentSnapshot.
@@ -251,7 +250,7 @@ def run( # noqa: PLR0915
251250
break_point: Optional[AgentBreakpoint] = None,
252251
snapshot: Optional[AgentSnapshot] = None, # type: ignore[override]
253252
system_prompt: Optional[str] = None,
254-
tools: Optional[Union[list[Tool], Toolset, list[str]]] = None,
253+
tools: Optional[Union[ToolsType, list[str]]] = None,
255254
**kwargs: Any,
256255
) -> dict[str, Any]:
257256
"""
@@ -435,7 +434,7 @@ async def run_async(
435434
break_point: Optional[AgentBreakpoint] = None,
436435
snapshot: Optional[AgentSnapshot] = None, # type: ignore[override]
437436
system_prompt: Optional[str] = None,
438-
tools: Optional[Union[list[Tool], Toolset, list[str]]] = None,
437+
tools: Optional[Union[ToolsType, list[str]]] = None,
439438
**kwargs: Any,
440439
) -> dict[str, Any]:
441440
"""

haystack_experimental/components/generators/chat/openai.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
from dataclasses import replace
6-
from typing import Any, Optional, Union
6+
from typing import Any, Optional
77

88
from haystack import component
99
from haystack.components.generators.chat.openai import OpenAIChatGenerator as BaseOpenAIChatGenerator
1010
from haystack.dataclasses import ChatMessage, StreamingCallbackT
11-
from haystack.tools import Tool, Toolset
11+
from haystack.tools import ToolsType
1212

1313
from haystack_experimental.utils.hallucination_risk_calculator.dataclasses import HallucinationScoreConfig
1414
from haystack_experimental.utils.hallucination_risk_calculator.openai_planner import calculate_hallucination_metrics
@@ -59,7 +59,7 @@ def run(
5959
streaming_callback: Optional[StreamingCallbackT] = None,
6060
generation_kwargs: Optional[dict[str, Any]] = None,
6161
*,
62-
tools: Optional[Union[list[Tool], Toolset]] = None,
62+
tools: Optional[ToolsType] = None,
6363
tools_strict: Optional[bool] = None,
6464
hallucination_score_config: Optional[HallucinationScoreConfig] = None,
6565
) -> dict[str, list[ChatMessage]]:
@@ -75,9 +75,8 @@ def run(
7575
override the parameters passed during component initialization.
7676
For details on OpenAI API parameters, see [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create).
7777
:param tools:
78-
A list of tools or a Toolset for which the model can prepare calls. If set, it will override the
79-
`tools` parameter set during component initialization. This parameter can accept either a list of
80-
`Tool` objects or a `Toolset` instance.
78+
A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
79+
If set, it will override the `tools` parameter provided during initialization.
8180
:param tools_strict:
8281
Whether to enable strict schema adherence for tool calls. If set to `True`, the model will follow exactly
8382
the schema provided in the `parameters` field of the tool definition, but this may increase latency.
@@ -127,7 +126,7 @@ async def run_async(
127126
streaming_callback: Optional[StreamingCallbackT] = None,
128127
generation_kwargs: Optional[dict[str, Any]] = None,
129128
*,
130-
tools: Optional[Union[list[Tool], Toolset]] = None,
129+
tools: Optional[ToolsType] = None,
131130
tools_strict: Optional[bool] = None,
132131
hallucination_score_config: Optional[HallucinationScoreConfig] = None,
133132
) -> dict[str, list[ChatMessage]]:
@@ -147,9 +146,8 @@ async def run_async(
147146
override the parameters passed during component initialization.
148147
For details on OpenAI API parameters, see [OpenAI documentation](https://platform.openai.com/docs/api-reference/chat/create).
149148
:param tools:
150-
A list of tools or a Toolset for which the model can prepare calls. If set, it will override the
151-
`tools` parameter set during component initialization. This parameter can accept either a list of
152-
`Tool` objects or a `Toolset` instance.
149+
A list of Tool and/or Toolset objects, or a single Toolset for which the model can prepare calls.
150+
If set, it will override the `tools` parameter provided during initialization.
153151
:param tools_strict:
154152
Whether to enable strict schema adherence for tool calls. If set to `True`, the model will follow exactly
155153
the schema provided in the `parameters` field of the tool definition, but this may increase latency.

haystack_experimental/core/pipeline/breakpoint.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
from typing import TYPE_CHECKING, Any, Optional
99

1010
from haystack import logging
11-
from haystack.core.errors import BreakpointException
12-
from haystack.core.pipeline.breakpoint import _save_pipeline_snapshot
13-
from haystack.dataclasses import ChatMessage
1411
from haystack.dataclasses.breakpoints import AgentBreakpoint, PipelineSnapshot, PipelineState, ToolBreakpoint
1512
from haystack.utils.base_serialization import _serialize_value_with_schema
1613
from haystack.utils.misc import _get_output_dir
@@ -120,55 +117,3 @@ def _create_pipeline_snapshot_from_tool_invoker(
120117
final_snapshot = replace(parent_snapshot, agent_snapshot=agent_snapshot)
121118

122119
return final_snapshot
123-
124-
125-
def _trigger_tool_invoker_breakpoint(*, llm_messages: list[ChatMessage], pipeline_snapshot: PipelineSnapshot) -> None:
126-
"""
127-
Check if a tool call breakpoint should be triggered before executing the tool invoker.
128-
129-
NOTE: Only difference to Haystack's native implementation is that it includes the fix from
130-
PR https://github.com/deepset-ai/haystack/pull/9853 where we make sure to check all tool calls in a chat message
131-
when checking if a BreakpointException should be made.
132-
133-
:param llm_messages: List of ChatMessage objects containing potential tool calls.
134-
:param pipeline_snapshot: PipelineSnapshot object containing the state of the pipeline and Agent snapshot.
135-
:raises BreakpointException: If the breakpoint is triggered, indicating a breakpoint has been reached for a tool
136-
call.
137-
"""
138-
if not pipeline_snapshot.agent_snapshot:
139-
raise ValueError("PipelineSnapshot must contain an AgentSnapshot to trigger a tool call breakpoint.")
140-
141-
if not isinstance(pipeline_snapshot.agent_snapshot.break_point.break_point, ToolBreakpoint):
142-
return
143-
144-
tool_breakpoint = pipeline_snapshot.agent_snapshot.break_point.break_point
145-
146-
# Check if we should break for this specific tool or all tools
147-
if tool_breakpoint.tool_name is None:
148-
# Break for any tool call
149-
should_break = any(msg.tool_call for msg in llm_messages)
150-
else:
151-
# Break only for the specific tool
152-
should_break = any(
153-
tc.tool_name == tool_breakpoint.tool_name for msg in llm_messages for tc in msg.tool_calls or []
154-
)
155-
156-
if not should_break:
157-
return # No breakpoint triggered
158-
159-
_save_pipeline_snapshot(pipeline_snapshot=pipeline_snapshot)
160-
161-
msg = (
162-
f"Breaking at {tool_breakpoint.component_name} visit count "
163-
f"{pipeline_snapshot.agent_snapshot.component_visits[tool_breakpoint.component_name]}"
164-
)
165-
if tool_breakpoint.tool_name:
166-
msg += f" for tool {tool_breakpoint.tool_name}"
167-
logger.info(msg)
168-
169-
raise BreakpointException(
170-
message=msg,
171-
component=tool_breakpoint.component_name,
172-
inputs=pipeline_snapshot.agent_snapshot.component_inputs,
173-
results=pipeline_snapshot.agent_snapshot.component_inputs["tool_invoker"]["serialized_data"]["state"],
174-
)

0 commit comments

Comments
 (0)