Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions agents/utils/tool_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ async def _execute_single_tool(
response = {"type": "tool_result", "tool_use_id": call.id}

try:
# Execute the tool directly
result = await tool_dict[call.name].execute(**call.input)
response["content"] = str(result)
tool = tool_dict[call.name]
except KeyError:
response["content"] = f"Tool '{call.name}' not found"
response["is_error"] = True
return response

try:
result = await tool.execute(**call.input)
response["content"] = str(result)
except Exception as e:
response["content"] = f"Error executing tool: {str(e)}"
response["is_error"] = True
Expand Down
29 changes: 29 additions & 0 deletions tests/agents/test_tool_keyerror_diagnostics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock

from utils.tool_util import _execute_single_tool


def test_execution_keyerror_is_not_reported_as_missing_tool():
tool = SimpleNamespace(execute=AsyncMock(side_effect=KeyError("required_field")))
call = SimpleNamespace(id="call_1", name="example", input={})
result = asyncio.run(_execute_single_tool(call, {"example": tool}))
assert result["is_error"] is True
assert result["content"] == "Error executing tool: 'required_field'"
assert result["tool_use_id"] == "call_1"


def test_missing_tool_keeps_lookup_error():
call = SimpleNamespace(id="call_1", name="missing", input={})
result = asyncio.run(_execute_single_tool(call, {}))
assert result["content"] == "Tool 'missing' not found"
assert result["is_error"] is True


def test_successful_tool_result():
tool = SimpleNamespace(execute=AsyncMock(return_value="done"))
call = SimpleNamespace(id="call_1", name="example", input={"value": 1})
result = asyncio.run(_execute_single_tool(call, {"example": tool}))
assert result == {"type": "tool_result", "tool_use_id": "call_1", "content": "done"}
tool.execute.assert_awaited_once_with(value=1)
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent / "agents"))