fix: status code in error message in RestApiTool

Merge https://github.com/google/adk-python/pull/2819

add status code to http error to make it more verbose

issue: https://github.com/google/adk-python/issues/2820

# testing plan
run new tests that have beed added in PR

Co-authored-by: Hangfei Lin <hangfei@google.com>
COPYBARA_INTEGRATE_REVIEW=https://github.com/google/adk-python/pull/2819 from milkisbad:rest_api_tool_verbose_error 79ac6d96174d414e7ca958379102cfa7ede0e883
PiperOrigin-RevId: 832367374
This commit is contained in:
milkisbad
2025-11-14 10:24:21 -08:00
committed by Copybara-Service
parent 0fa7e4619d
commit 9b754564b3
2 changed files with 45 additions and 1 deletions
@@ -428,7 +428,7 @@ class RestApiTool(BaseTool):
f"Tool {self.name} execution failed. Analyze this execution error"
" and your inputs. Retry with adjustments if applicable. But"
" make sure don't retry more than 3 times. Execution Error:"
f" {error_details}"
f" Status Code: {response.status_code}, {error_details}"
)
}
except ValueError:
@@ -34,6 +34,7 @@ from google.adk.tools.tool_context import ToolContext
from google.genai.types import FunctionDeclaration
from google.genai.types import Schema
import pytest
import requests
class TestRestApiTool:
@@ -224,6 +225,49 @@ class TestRestApiTool:
# Check the result
assert result == {"result": "success"}
@patch(
"google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool.requests.request"
)
@pytest.mark.asyncio
async def test_call_http_failure(
self,
mock_request,
mock_tool_context,
sample_endpoint,
sample_operation,
sample_auth_scheme,
sample_auth_credential,
):
mock_response = MagicMock()
mock_response.status_code = 500
mock_response.content = b"Internal Server Error"
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
"500 Server Error"
)
mock_request.return_value = mock_response
tool = RestApiTool(
name="test_tool",
description="Test Tool",
endpoint=sample_endpoint,
operation=sample_operation,
auth_scheme=sample_auth_scheme,
auth_credential=sample_auth_credential,
)
# Call the method
result = await tool.call(args={}, tool_context=mock_tool_context)
# Check the result
assert result == {
"error": (
"Tool test_tool execution failed. Analyze this execution error"
" and your inputs. Retry with adjustments if applicable. But"
" make sure don't retry more than 3 times. Execution Error:"
" Status Code: 500, Internal Server Error"
)
}
@patch(
"google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool.requests.request"
)