google cloud speech - extracting uri from results in BatchRecognizeResponse in Python - Stack Overflow

admin2025-04-17  3

I'm using Google Cloud Speech-to-Text V2 API, specifically the BatchRecognizeResponse type. This response is structured as a protobuf object with multiple embedded fields, but I’m having difficulty figuring out the exact Python types they are converted into.

I have the following code:

operation = client.batch_recognize(request=request)
response = operation.result()

When I print response, I get:

results {
  key: "gs://my-path"
  value {
    metadata {
      total_billed_duration {
        seconds: 121
      }
    }
    cloud_storage_result {
      srt_format_uri: "my-output-path"
    }
  }
}
total_billed_duration {
  seconds: 121
}

I need to extract the value of srt_format_uri, which is "my-output-path".

I attempted to treat results as a dictionary using .items() and also tried iterating over operation.result() like this:

for result in operation.result():
    key = result.key 
    ...

None of these approaches worked. How can I correctly extract srt_format_uri from the BatchRecognizeResponse in Python?
Or how can I figure out exactly what are the internal python structures that protobuf is converted into? I.e. is there documentation for .proto files available for this?

I'm using Google Cloud Speech-to-Text V2 API, specifically the BatchRecognizeResponse type. This response is structured as a protobuf object with multiple embedded fields, but I’m having difficulty figuring out the exact Python types they are converted into.

I have the following code:

operation = client.batch_recognize(request=request)
response = operation.result()

When I print response, I get:

results {
  key: "gs://my-path"
  value {
    metadata {
      total_billed_duration {
        seconds: 121
      }
    }
    cloud_storage_result {
      srt_format_uri: "my-output-path"
    }
  }
}
total_billed_duration {
  seconds: 121
}

I need to extract the value of srt_format_uri, which is "my-output-path".

I attempted to treat results as a dictionary using .items() and also tried iterating over operation.result() like this:

for result in operation.result():
    key = result.key 
    ...

None of these approaches worked. How can I correctly extract srt_format_uri from the BatchRecognizeResponse in Python?
Or how can I figure out exactly what are the internal python structures that protobuf is converted into? I.e. is there documentation for .proto files available for this?

Share Improve this question asked Jan 30 at 20:23 KrisKris 971 gold badge1 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Oh well, this works:

for key, value in response.results.items():
    print(f"Key: {key}")
    print(f"Total Billed Duration: {value.metadata.total_billed_duration.seconds} seconds")
    print(f"SRT Format URI: {value.cloud_storage_result.srt_format_uri}")
转载请注明原文地址:http://anycun.com/QandA/1744891818a89096.html