What encoding does the metadata element of the Google Cloud Speech API use?

24 Views Asked by At

I am using v2 of the Google Cloud Speech API to check on the status of a long running recognition job.

from google.cloud.speech_v2 import SpeechClient

client_options_ = client_options.ClientOptions(
    api_endpoint = "us-west1-speech.googleapis.com"
)

client = SpeechClient(client_options = client_options_)

ops_obj = { "name": "{{name-of-my-job}}" }
resp_obj = client.get_operation(ops_obj)

resp_metadata = resp_obj.metadata.value

print(resp_metadata)

I am having trouble using the data I get back (snippet below):

b'\n\x0c\x08\xf0\x8a\xc7\xaf\x06\x10\xa0\xdf\xb3\xce\x03\x12\x0c\x08\x89\xac\xc7\xaf\x06\x10\xe0\xef...

When I try to decode this data:

resp_metadata.decode('utf-8')

I am getting this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 3: invalid continuation byte

Can anyone please point me in the right direction? I am trying to make this metadata usable.

1

There are 1 best solutions below

0
SeanMaday On

I figured it out. Thank you to Frank Yellin for pointing out that I was dealing with a protobuf. This got me thinking in the right direction.

Here is what I used:

from google.protobuf.json_format import MessageToJson

print(MessageToJson(resp_obj))

The result was beautifully formatted JSON.