diff --git a/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py b/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py index b9f91f8e212..fca6c834e02 100644 --- a/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py +++ b/src/google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py @@ -640,7 +640,7 @@ async def call( response.raise_for_status() # Raise HTTPStatusError for bad responses return response.json() # Try to decode JSON except httpx.HTTPStatusError: - error_details = response.content.decode("utf-8") + error_details = response.text self._logger.warning( "API call failed for tool %s: Status %d - %s", self.name, diff --git a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py index 5cd2d340e87..87e0e0c87b0 100644 --- a/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +++ b/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py @@ -355,7 +355,7 @@ async def test_call_http_failure( ): mock_response = MagicMock() mock_response.status_code = 500 - mock_response.content = b"Internal Server Error" + mock_response.text = "Internal Server Error" # Create a proper HTTPStatusError with request and response mock_http_request = MagicMock(spec=httpx.Request) @@ -390,6 +390,37 @@ async def test_call_http_failure( ) } + @patch( + "google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool._request" + ) + @pytest.mark.asyncio + async def test_call_http_failure_decodes_body_with_declared_charset( + self, + mock_request, + mock_tool_context, + sample_endpoint, + sample_operation, + ): + """A non-UTF-8 error body must reach the model, not abort the run.""" + mock_request.return_value = httpx.Response( + status_code=404, + request=httpx.Request("GET", "https://example.com/test"), + content="Commande introuvable : échec".encode("latin-1"), + headers={"content-type": "text/plain; charset=iso-8859-1"}, + ) + tool = RestApiTool( + name="test_tool", + description="Test Tool", + endpoint=sample_endpoint, + operation=sample_operation, + ) + + result = await tool.call(args={}, tool_context=mock_tool_context) + + assert result["error"].endswith( + "Status Code: 404, Commande introuvable : échec" + ) + @patch( "google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool._request" )