Search code examples
google-cloud-composergoogle-cloud-automlgoogle-cloud-python

using add_done_callback() and reponse.metadata() for automl


I'm trying to use a standard example for automl. I would expect the create_model to launch a long running operation that will update the operation response once it's done and to then access the metadata (to get the model_id of the newly trained model) . but the script fails right away on metadata = response.metadata() with "TypeError: 'OperationMetadata' object is not callable".

I'm running this code inside a PythonOperator in Airflow/google composer if it makes any difference. I can see in the AutoML UI that the model starts training correctly.

My code is this, but it's basically the example usage that's in the docs.:

from google.cloud import automl

client = automl.AutoMlClient()

...
response = client.create_model(project_location, my_model)

def callback(operation_future):
   # Handle result.
   result = operation_future.result()

response.add_done_callback(callback)
metadata = response.metadata()

I'm using google-cloud-automl==0.9.0


Solution

  • I ran into the same problem. That example in the docs really led me astray. If you call the result() method on the response, it will actually wait until the model is finished training before it returns anything, so that's all you need to do. You can then get the model_id from that result.

    import time
    from google.cloud import automl
    
    client = automl.AutoMlClient()
    
    ...
    response = client.create_model(project_location, my_model)
    
    model_id = response.result().name.split('/')[-1]