Skip to content
This repository was archived by the owner on Aug 27, 2025. It is now read-only.

Commit 11159c6

Browse files
authored
fix: throw tool errors correctly. (#35)
* fix: throw tool errors correctly. * fix tests * fix tests
1 parent 0f8b533 commit 11159c6

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/toolbox_llamaindex/async_tools.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,16 +179,25 @@ async def acall(self, **kwargs: Any) -> ToolOutput: # type: ignore
179179

180180
# Merge bound parameters with the provided arguments
181181
kwargs.update(evaluated_params)
182-
response = await _invoke_tool(
183-
self.__url, self.__session, self.__name, kwargs, self.__auth_tokens
184-
)
185-
return ToolOutput(
186-
content=str(response),
187-
tool_name=self.__name,
188-
raw_input=kwargs,
189-
raw_output=response,
190-
is_error=False,
191-
)
182+
try:
183+
response = await _invoke_tool(
184+
self.__url, self.__session, self.__name, kwargs, self.__auth_tokens
185+
)
186+
return ToolOutput(
187+
content=str(response),
188+
tool_name=self.__name,
189+
raw_input=kwargs,
190+
raw_output=response,
191+
is_error=False,
192+
)
193+
except ClientResponseError as e:
194+
return ToolOutput(
195+
content="Encountered error: " + str(e),
196+
tool_name=self.__name,
197+
raw_input=kwargs,
198+
raw_output=str(e),
199+
is_error=True,
200+
)
192201

193202
def __validate_auth(self, strict: bool = True) -> None:
194203
"""

tests/test_e2e.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,21 @@ async def test_run_tool_no_auth(self, toolbox):
127127
tool = await toolbox.aload_tool(
128128
"get-row-by-id-auth",
129129
)
130-
with pytest.raises(ClientResponseError, match="401, message='Unauthorized'"):
131-
await tool.acall(id="2")
130+
response = await tool.acall(id="2")
131+
assert response.is_error == True
132+
assert "401, message='Unauthorized'" in response.content
133+
assert isinstance(response.raw_output, str)
132134

133135
async def test_run_tool_wrong_auth(self, toolbox, auth_token2):
134136
"""Tests running a tool with incorrect auth."""
135137
tool = await toolbox.aload_tool(
136138
"get-row-by-id-auth",
137139
)
138140
auth_tool = tool.add_auth_token("my-test-auth", lambda: auth_token2)
139-
with pytest.raises(ClientResponseError, match="401, message='Unauthorized'"):
140-
await auth_tool.acall(id="2")
141+
response = await auth_tool.acall(id="2")
142+
assert response.is_error == True
143+
assert "401, message='Unauthorized'" in response.content
144+
assert isinstance(response.raw_output, str)
141145

142146
async def test_run_tool_auth(self, toolbox, auth_token1):
143147
"""Tests running a tool with correct auth."""
@@ -173,8 +177,10 @@ async def test_run_tool_param_auth_no_field(self, toolbox, auth_token1):
173177
tool = await toolbox.aload_tool(
174178
"get-row-by-content-auth", auth_tokens={"my-test-auth": lambda: auth_token1}
175179
)
176-
with pytest.raises(ClientResponseError, match="400, message='Bad Request'"):
177-
await tool.acall()
180+
response = await tool.acall()
181+
assert response.is_error == True
182+
assert "400, message='Bad Request'" in response.content
183+
assert isinstance(response.raw_output, str)
178184

179185

180186
@pytest.mark.usefixtures("toolbox_server")
@@ -261,18 +267,21 @@ def test_run_tool_no_auth(self, toolbox):
261267
tool = toolbox.load_tool(
262268
"get-row-by-id-auth",
263269
)
264-
with pytest.raises(ClientResponseError, match="401, message='Unauthorized'"):
265-
tool.call(id="2")
270+
response = tool.call(id="2")
271+
assert response.is_error == True
272+
assert "401, message='Unauthorized'" in response.content
273+
assert isinstance(response.raw_output, str)
266274

267275
def test_run_tool_wrong_auth(self, toolbox, auth_token2):
268276
"""Tests running a tool with incorrect auth."""
269277
tool = toolbox.load_tool(
270278
"get-row-by-id-auth",
271279
)
272280
auth_tool = tool.add_auth_token("my-test-auth", lambda: auth_token2)
273-
274-
with pytest.raises(ClientResponseError, match="401, message='Unauthorized'"):
275-
auth_tool.call(id="2")
281+
response = auth_tool.call(id="2")
282+
assert response.is_error == True
283+
assert "401, message='Unauthorized'" in response.content
284+
assert isinstance(response.raw_output, str)
276285

277286
def test_run_tool_auth(self, toolbox, auth_token1):
278287
"""Tests running a tool with correct auth."""
@@ -308,5 +317,7 @@ def test_run_tool_param_auth_no_field(self, toolbox, auth_token1):
308317
tool = toolbox.load_tool(
309318
"get-row-by-content-auth", auth_tokens={"my-test-auth": lambda: auth_token1}
310319
)
311-
with pytest.raises(ClientResponseError, match="400, message='Bad Request'"):
312-
tool.call(id="2")
320+
response = tool.call()
321+
assert response.is_error == True
322+
assert "400, message='Bad Request'" in response.content
323+
assert isinstance(response.raw_output, str)

0 commit comments

Comments
 (0)