ListRecommendationsRequest constructor error in Python script

Question:

I am trying to prepare a VM Resizing Recommendations Report using a Python 3.7 script. My code is as follows:

import datetime
import logging
from google.cloud import bigquery
from google.cloud import recommender
from google.cloud.exceptions import NotFound
from googleapiclient import discovery

def main(event, context):
 client = recommender.RecommenderClient()
 recommender_type = "google.compute.instance.MachineTypeRecommender"
 projects=list_projects() #This gives list of projects
 # I hard-code the zones below:
 zones = ["us-east1-b","us-east1-c","us-east1-d","us-east4-c","us-east4-b","us-east4-a"
        ,"us-central1-c","us-central1-a","us-central1-f","us-central1-b"
        ,"us-west1-b","us-west1-c","us-west1-a"]

 for zone in zones:
            parent = client.recommender_path("my-project", zone, recommender_type)
            for element in client.list_recommendations(parent): #In this line I am getting this error and these are logs

****Parent****   projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender
Traceback (most recent call last):
  File "main.py", line 187, in main
    x=list(client.list_recommendations(parent))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/google/cloud/recommender_v1/services/recommender/client.py", line 734, in list_recommendations
    request = recommender_service.ListRecommendationsRequest(request)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/proto/message.py", line 441, in __init__
    raise TypeError(
TypeError: Invalid constructor input for ListRecommendationsRequest: 'projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender'

During handling of the above exception, another exception occurred:

"projects/my-project/locations/us-east1-b/recommenders/google.compute.instance.MachineTypeRecommender" is the parameter being passed to the list_recommendations function. I am not sure what is wrong in the constructor as I am getting this: Invalid constructor input for ListRecommendationsRequest

I am new to this Google api. What can I try next?

Asked By: user1403505

||

Answers:

I have checked the code on my end an it seems to be a syntax error when calling the method. As per the library documentation the first parameter should be a request object or None and the parent parameter has to be passed by name. Changing the line as follows I didn’t have the error:

client.list_recommendations(parent=parent)
Answered By: Happy-Monad