Why does this fail with "'async for' requires an object with __aiter__ method, got coroutine"

7.8k Views Asked by At

I am attempting to call an external API (one that is provided by Google) using an async client library (the library is also provided by Google).

The async method that I am attempting to call is async list_featurestores() (documentation). It provides the following sample code:

from google.cloud import aiplatform_v1

async def sample_list_featurestores():
    # Create a client
    client = aiplatform_v1.FeaturestoreServiceAsyncClient()

    # Initialize request argument(s)
    request = aiplatform_v1.ListFeaturestoresRequest(
        parent="parent_value",
    )

    # Make the request
    page_result = client.list_featurestores(request=request)

    # Handle the response
    async for response in page_result:
        print(response)

(I've literally copy/pasted that code from the above linked page).

In order to run that code I've slightly adapted it to:

import asyncio
from google.cloud import aiplatform_v1

async def sample_list_featurestores():
    client = aiplatform_v1.FeaturestoreServiceAsyncClient()
    request = aiplatform_v1.ListFeaturestoresRequest(parent="projects/MY_GCP_PROJECT/locations/europe-west2",)
    page_result  = client.list_featurestores(request=request)
    async for response in page_result:
        print(response)


if __name__ == "__main__":
    asyncio.run(sample_list_featurestores())

When I run it it fails on this line: async for response in page_result: with error:

'async for' requires an object with aiter method, got coroutine

This is my first foray into async python development and, given I (think I) have followed the supplied code to the letter I don't know why I'm getting this error.

Am I missing something obvious here? Can someone explain how to get past this error?

1

There are 1 best solutions below

0
AckThpfft On

Try using "await":

# Make the request
page_result = await client.list_featurestores(request=request)