From edb82b8a84683d4199ab79da03aeb9e2313a0708 Mon Sep 17 00:00:00 2001 From: chelsealong Date: Fri, 25 Sep 2026 11:09:02 +0000 Subject: [PATCH] fix(tools): return a structured error when RestApiTool is missing a required path param RestApiTool._prepare_request_params substitutes filled path parameters into self.endpoint.path.format(**path_params). An omitted required path parameter leaves the template key unset, so format() raises an uncaught KeyError that aborts the whole agent invocation instead of giving the model a retryable {"error": ...} result, unlike FunctionTool which already reports missing mandatory args this way. call() now catches KeyError from _prepare_request_params and returns a structured error naming the missing path parameter. --- .../openapi_spec_parser/rest_api_tool.py | 13 +++++- .../openapi_spec_parser/test_rest_api_tool.py | 44 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) 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..9e229e8a902 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 @@ -571,7 +571,18 @@ async def call( api_args.update(auth_args) # Got all parameters. Call the API. - request_params = self._prepare_request_params(api_params, api_args) + try: + request_params = self._prepare_request_params(api_params, api_args) + except KeyError as e: + missing_param = e.args[0] if e.args else str(e) + return { + "error": ( + f"Tool {self.name} execution failed. Analyze this execution" + " error and your inputs. Retry with adjustments if" + " applicable. Execution Error: Missing required path" + f" parameter '{missing_param}'." + ) + } if self._ssl_verify is not None: request_params["verify"] = self._ssl_verify 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..c3a2b3813e0 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 @@ -1400,6 +1400,50 @@ async def test_call_with_required_param_defaults( assert call_kwargs["url"] == "https://example.com/users/me/messages" assert result == {"result": "success"} + @patch( + "google.adk.tools.openapi_tool.openapi_spec_parser.rest_api_tool._request" + ) + @pytest.mark.asyncio + async def test_call_missing_required_path_param_returns_error( + self, + mock_request, + mock_tool_context, + sample_auth_scheme, + sample_auth_credential, + ): + """A required path param with no default and no value must not crash.""" + mock_operation = Operation( + operationId="test_op", + parameters=[ + OpenAPIParameter(**{ + "name": "userId", + "in": "path", + "required": True, + "schema": OpenAPISchema(type="string"), + }) + ], + ) + + tool = RestApiTool( + name="test_tool", + description="Test Tool", + endpoint=OperationEndpoint( + base_url="https://example.com", + path="/users/{userId}/messages", + method="GET", + ), + operation=mock_operation, + auth_scheme=sample_auth_scheme, + auth_credential=sample_auth_credential, + ) + + # Call without providing userId and without a default to fall back on. + result = await tool.call(args={}, tool_context=mock_tool_context) + + assert not mock_request.called + assert "error" in result + assert "userId" in result["error"] + def test_prepare_request_params_query_body( self, sample_endpoint, sample_auth_credential, sample_auth_scheme ):