Skip to content

Commit 1130ea4

Browse files
Merge pull request #2383 from onmete/dont-leak-token-to-llm
Don't capture the literal error in case of wrong tool args
2 parents beb5e9f + 033e9d9 commit 1130ea4

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

ols/src/tools/tools.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ def execute_oc_tool_calls(
4949
logger.error(tool_output)
5050
else:
5151
try:
52-
# inject token into tool args and immediately remove it
53-
# to avoid leaking
54-
tool_args["token"] = token
55-
tool_output = tool.invoke(tool_args)
56-
del tool_args["token"]
52+
# create a new dict with the tool args and the token
53+
tool_output = tool.invoke({**tool_args, "token": token})
5754
except ValidationError:
5855
tool_output = (
5956
f"Error executing {tool_name}: tool arguments are in wrong format"
@@ -64,11 +61,6 @@ def execute_oc_tool_calls(
6461
except Exception as e:
6562
tool_output = f"Error executing {tool_name}: {e}"
6663
logger.exception(tool_output)
67-
finally:
68-
# remove token from tool args if it was not removed
69-
# in the try block
70-
if "token" in tool_args:
71-
del tool_args["token"]
7264

7365
logger.debug(
7466
"Tool: %s | Args: %s | Output: %s", tool_name, tool_args, tool_output

tests/unit/tools/test_tools.py

+27
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,30 @@ def tool1(some_arg: str):
6969
in caplog.text
7070
)
7171
assert "fake-token" not in caplog.text
72+
73+
74+
def test_execute_oc_tool_calls_not_leaks_token_into_output(caplog):
75+
"""Test execute_oc_tool_calls does not leak token into output."""
76+
caplog.set_level(10) # set debug level
77+
78+
@tool
79+
def tool1(some_args: list):
80+
"""Tool 1."""
81+
return "bla"
82+
83+
tools_map = {"tool1": tool1}
84+
85+
# missing args
86+
tool_calls = [{"id": 1, "name": "tool1"}]
87+
tool_messages = execute_oc_tool_calls(tools_map, tool_calls, "fake-token")
88+
assert len(tool_messages) == 1
89+
assert "fake-token" not in tool_messages[0].content
90+
91+
# unknown args
92+
tool_calls = [{"id": 1, "name": "tool1", "args": {"unknown_args": "blo"}}]
93+
tool_messages = execute_oc_tool_calls(tools_map, tool_calls, "fake-token")
94+
assert len(tool_messages) == 1
95+
assert "fake-token" not in tool_messages[0].content
96+
97+
# ensure the token is also not in the logs
98+
assert "fake-token" not in caplog.text

0 commit comments

Comments
 (0)