Repository navigation
Description
Describe the bug
When an Agent is configured with confirmation_strategies for at least one tool, and the chat model hallucinates a tool call for a tool name that isn't in the tools list, human_in_the_loop raises a raw, unhandled KeyError instead of the clean ToolNotFoundException that ToolInvoker normally raises for the exact same situation.
This means enabling human-in-the-loop confirmation makes the agent less robust to LLM tool-call hallucination: with no confirmation strategy configured, ToolInvoker catches the unknown tool name and (depending on raise_on_failure) either raises a descriptive ToolNotFoundException listing the available tools, or returns an error tool message without crashing at all. With a confirmation strategy configured, the lookup happens earlier in _run_confirmation_strategies / _run_confirmation_strategies_async via a raw dict access, and raise_on_failure is never consulted.
Error message
KeyError: 'does_not_exist'
Raised from:
haystack/human_in_the_loop/strategies.py:376, in _run_confirmation_strategies
tool_to_invoke = tools_with_names[tool_name]
(and the identical pattern in _run_confirmation_strategies_async)
Expected behavior
A hallucinated/unknown tool name should be handled consistently regardless of whether confirmation_strategies is configured — ideally surfacing the same ToolNotFoundException (and respecting raise_on_failure) that ToolInvoker already provides, rather than an unhandled KeyError.
To Reproduce
from haystack.components.agents.agent import _ExecutionContext
from haystack.components.agents.state.state import State
from haystack.dataclasses import ChatMessage, ToolCall
from haystack.human_in_the_loop import BlockingConfirmationStrategy, NeverAskPolicy, SimpleConsoleUI
from haystack.human_in_the_loop.strategies import _run_confirmation_strategies
from haystack.tools import create_tool_from_function
def addition_tool(a: int, b: int) -> int:
return a + b
add_tool = create_tool_from_function(function=addition_tool, name="addition_tool", description="adds two ints")
execution_context = _ExecutionContext(
state=State(schema={"messages": {"type": list[ChatMessage]}}),
component_visits={"chat_generator": 0, "tool_invoker": 0},
chat_generator_inputs={},
tool_invoker_inputs={"tools": [add_tool]},
counter=0,
skip_chat_generator=False,
)
strategy = BlockingConfirmationStrategy(confirmation_policy=NeverAskPolicy(), confirmation_ui=SimpleConsoleUI())
# Simulate the chat model hallucinating a tool call for a tool that isn't in `tools`
teds = _run_confirmation_strategies(
confirmation_strategies={add_tool.name: strategy},
messages_with_tool_calls=[
ChatMessage.from_assistant(tool_calls=[ToolCall(tool_name="does_not_exist", arguments={})])
],
execution_context=execution_context,
)
# -> KeyError: 'does_not_exist'Additional context
For comparison, ToolInvoker handles the same scenario gracefully (haystack/components/tools/tool_invoker.py):
if tool_name not in tools_with_names:
error = ToolNotFoundException(tool_name, list(tools_with_names.keys()))
if self.raise_on_failure:
raise error
...This issue happens earlier in the pipeline, in human_in_the_loop/strategies.py's _run_confirmation_strategies/_run_confirmation_strategies_async, which run before ToolInvoker and don't have equivalent handling.
I'd like to work on a fix for this (replacing the raw dict lookup with the same ToolNotFoundException handling ToolInvoker uses) and submit a PR.
System:
- OS: Windows
- Haystack version: main (85ef972)
Activity