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 527095f5366..f9d13ae8cbc 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 @@ -609,6 +609,11 @@ async def call( # Got all parameters. Call the API. 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 self._format_error_response( + f"Missing required path parameter '{missing_param}'." + ) except InputValidationError as e: return self._format_error_response(str(e)) if self._ssl_verify is not None: 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 7ec434f8096..c70b191a50c 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 @@ -1425,6 +1425,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 ):