Search code examples
python-3.xgoogle-speech-api

How to get cloud speech api response in python if I only have the operation name?


I am sending a long speech recognition file for speech recognition.

operation = client.long_running_recognize(config, audio)
operation_name = operation._operation.name

There is another file where I have to use operation_name (returned by google speech API) to get back the response again.

ref: Python way of polling longrunning operations from operation name in Google Cloud?

I tried get_operation method of the "Long-Running Operations Client":

from google.api_core import operations_v1
api = operations_v1.OperationsClient()
name = ...
response = api.get_operation(name)

but I get the following error for the line:

api = operations_v1.OperationsClient()

TypeError: init() missing 1 required positional argument: 'channel'


Solution

  • Got the answer on Github issues:

    from google.cloud import speech
    client = speech.SpeechClient()
    
    from google.api_core import operations_v1
    api = operations_v1.OperationsClient(client.transport.channel)
    name = ...
    response = api.get_operation(name)
    

    reference : Github