HITL: `ToolExecutionDecision(execute=True)` without `final_tool_params` runs the tool with `{}` and reports a user modification
Describe the bug
A custom ConfirmationStrategy answers each tool call with a ToolExecutionDecision. final_tool_params is documented as optional ("Optional final parameters for the tool if execution is confirmed or modified", default None), and execute as "whether to execute the tool with the provided parameters". A strategy that approves a call as proposed with
ToolExecutionDecision(tool_name=tool_name, execute=True, tool_call_id=tool_call_id)does not get that call executed. _apply_tool_execution_decisions reads final_tool_params=None as {}, so:
- the decision is classified as a modification, because the call's arguments differ from
{}; - the call's arguments are replaced with
{}: the tool runs with its own defaults, or fails with a missing-argument error if a parameter is required; - a user message
"The parameters for tool 'fs_write' were updated by the user to:\n{}"is inserted into the chat history, and the model's own tool call in the history is rewritten to{}.
No error or warning is raised. The built-in BlockingConfirmationStrategy always fills final_tool_params on confirm, so only application-provided strategies are affected.
To Reproduce
"""Haystack: approving with ToolExecutionDecision(execute=True) and no final_tool_params
runs the tool with {} instead of the model's arguments, and tells the model the user
changed them.
Offline, no API key: a scripted chat generator asks for fs_write(path="/tmp/a") once,
then answers "done". Nothing is written to disk.
Run: python repro_approval_without_params.py
"""
from importlib.metadata import version
from haystack import component
from haystack.components.agents import Agent
from haystack.dataclasses import ChatMessage, ToolCall
from haystack.hooks.human_in_the_loop import ConfirmationHook, ToolExecutionDecision
from haystack.tools import Tool
RECEIVED = []
def fs_write(path: str = "<default>") -> str:
RECEIVED.append(path)
return f"wrote {path}"
TOOL = Tool(name="fs_write", description="Write a file.", function=fs_write,
parameters={"type": "object", "properties": {"path": {"type": "string"}}})
@component
class ScriptedChatGenerator:
def __init__(self):
self.turn = 0
@component.output_types(replies=list[ChatMessage])
def run(self, messages: list[ChatMessage], tools=None, **kwargs):
self.turn += 1
if self.turn == 1:
call = ToolCall(tool_name="fs_write", arguments={"path": "/tmp/a"}, id="c1")
return {"replies": [ChatMessage.from_assistant(tool_calls=[call])]}
return {"replies": [ChatMessage.from_assistant("done")]}
class ApproveAll:
"""An application's own ConfirmationStrategy: approves every call as proposed."""
def __init__(self, pass_params: bool):
self.pass_params = pass_params
def run(self, *, tool_name, tool_description, tool_params, tool_call_id=None,
confirmation_strategy_context=None):
if self.pass_params:
return ToolExecutionDecision(tool_name=tool_name, execute=True, tool_call_id=tool_call_id,
final_tool_params=tool_params)
return ToolExecutionDecision(tool_name=tool_name, execute=True, tool_call_id=tool_call_id)
def run(pass_params: bool) -> None:
RECEIVED.clear()
hook = ConfirmationHook(confirmation_strategies={"fs_write": ApproveAll(pass_params)})
agent = Agent(chat_generator=ScriptedChatGenerator(), tools=[TOOL], hooks={"before_tool": [hook]})
messages = agent.run(messages=[ChatMessage.from_user("write /tmp/a")])["messages"]
print(" tool received: ", f"path={RECEIVED[0]!r}" if RECEIVED else "(not run)")
print(" tool call in history:", [tc.arguments for m in messages for tc in (m.tool_calls or [])])
print(" user messages added: ", [m.text for m in messages[1:] if m.is_from("user")])
if __name__ == "__main__":
print("haystack-ai", version("haystack-ai"))
print("\napproved with final_tool_params=tool_params:")
run(pass_params=True)
print("\napproved with final_tool_params left unset (default None):")
run(pass_params=False)Output on 3.2.0 (identical on 3.0.0, 3.1.1 and main, see below):
haystack-ai 3.2.0
approved with final_tool_params=tool_params:
tool received: path='/tmp/a'
tool call in history: [{'path': '/tmp/a'}]
user messages added: []
approved with final_tool_params left unset (default None):
tool received: path='<default>'
tool call in history: [{}]
user messages added: ["The parameters for tool 'fs_write' were updated by the user to:\n{}"]With path made a required parameter (def fs_write(path: str), "required": ["path"]; run on 3.2.0), the second case does not run the tool at all; the tool result the model receives is Failed to invoke Tool `fs_write` with parameters {}. Error: fs_write() missing 1 required positional argument: 'path'.
Impact
- What runs is not what was approved. The strategy approved
fs_write(path="/tmp/a");fs_write()ran with the tool's defaults. For a tool whose parameters have defaults (a default path, recipient, scope), an action that nobody approved is performed. With required parameters, the approved action fails instead and the model is left to recover from the error. - The chat history is falsified. The model reads a user message saying the user changed the parameters to
{}, and sees its own call rewritten to{}. Per the comment in (2), that message exists to keep the model from retrying the original parameters, so the model is steered away from the call the user actually approved. - It is silent and easy to hit.
ToolExecutionDecision(tool_name=..., execute=True)is a complete-looking approval given the documented defaults. The docs table (human-in-the-loop.mdx) listsfinal_tool_paramsasdict | None, "Final parameters to use for execution", without saying whatNonedoes.
Source: deepset-ai/haystack