Skip to content

Commit 9b75456

Browse files
milkisbadhangfei
authored andcommitted
fix: status code in error message in RestApiTool
Merge #2819 add status code to http error to make it more verbose issue: #2820 # testing plan run new tests that have beed added in PR Co-authored-by: Hangfei Lin <hangfei@google.com> COPYBARA_INTEGRATE_REVIEW=#2819 from milkisbad:rest_api_tool_verbose_error 79ac6d9 PiperOrigin-RevId: 832367374
1 parent 0fa7e46 commit 9b75456

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ async def call(
428428
f"Tool {self.name} execution failed. Analyze this execution error"
429429
" and your inputs. Retry with adjustments if applicable. But"
430430
" make sure don't retry more than 3 times. Execution Error:"
431-
f" {error_details}"
431+
f" Status Code: {response.status_code}, {error_details}"
432432
)
433433
}
434434
except ValueError:

tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from google.genai.types import FunctionDeclaration
3535
from google.genai.types import Schema
3636
import pytest
37+
import requests
3738

3839

3940
class TestRestApiTool:
@@ -224,6 +225,49 @@ async def test_call_success(
224225
# Check the result
225226
assert result == {"result": "success"}
226227

228+
@patch(
229+
"google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool.requests.request"
230+
)
231+
@pytest.mark.asyncio
232+
async def test_call_http_failure(
233+
self,
234+
mock_request,
235+
mock_tool_context,
236+
sample_endpoint,
237+
sample_operation,
238+
sample_auth_scheme,
239+
sample_auth_credential,
240+
):
241+
mock_response = MagicMock()
242+
mock_response.status_code = 500
243+
mock_response.content = b"Internal Server Error"
244+
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
245+
"500 Server Error"
246+
)
247+
mock_request.return_value = mock_response
248+
249+
tool = RestApiTool(
250+
name="test_tool",
251+
description="Test Tool",
252+
endpoint=sample_endpoint,
253+
operation=sample_operation,
254+
auth_scheme=sample_auth_scheme,
255+
auth_credential=sample_auth_credential,
256+
)
257+
258+
# Call the method
259+
result = await tool.call(args={}, tool_context=mock_tool_context)
260+
261+
# Check the result
262+
assert result == {
263+
"error": (
264+
"Tool test_tool execution failed. Analyze this execution error"
265+
" and your inputs. Retry with adjustments if applicable. But"
266+
" make sure don't retry more than 3 times. Execution Error:"
267+
" Status Code: 500, Internal Server Error"
268+
)
269+
}
270+
227271
@patch(
228272
"google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool.requests.request"
229273
)

0 commit comments

Comments
 (0)