Search code examples
google-cloud-platformgoogle-cloud-vertex-ai

How do I get deployed model from `ListEndpointsRequest`?


My code is:

   client = aiplatform_v1.EndpointServiceClient(client_options=options)
    parent = client.common_location_path(project=project_id, location=location)
    

    # Initialize request argument(s)
    request = aiplatform_v1.ListEndpointsRequest(
        parent=parent
    )

    # Make the request
    endpoints_pager = client.list_endpoints(request=request)
    for endpoint in endpoints_pager.pages:
        latest_endpoint=endpoint
        print(endpoint.deployed_models.id)

If I print endpoint I see {...deployed_models { id: ...}} endpoint.deployed_models.id) doesn't work so how do I get the deployed model id?


Solution

  • As @DazWilkin mentioned in the comments, you need to iterate through deployed_models to get the id per model. Applying this, your code should look like this:

    IMPORTANT NOTE FOR FUTURE READERS: When creating client it is needed to define the api_endpoint in client_options. If not not defined you will encounter a google.api_core.exceptions.MethodNotImplemented: 501 Received http2 header with status: 404 error.

    from google.cloud import aiplatform_v1
    
    def sample_list_endpoints():
    
        project_id = "your-project-id"
        location = "us-central1"
    
        client = aiplatform_v1.EndpointServiceClient(client_options={"api_endpoint":"us-central1-aiplatform.googleapis.com"})
    
        parent = client.common_location_path(project=project_id, location=location)
        # Initialize request argument(s)
        request = aiplatform_v1.ListEndpointsRequest(
            parent=parent,
        )
    
        # Make the request
        page_result = client.list_endpoints(request=request)
    
        # Handle the response
        for response in page_result:
            for model in response.deployed_models:
                print(model.id)
    
    sample_list_endpoints()