Protobuf: AttributeError: 'DESCRIPTOR' when converting Vision API response using MessageToJson

Created on 5 Oct 2020  路  3Comments  路  Source: protocolbuffers/protobuf

I have been using MessageToJson to convert Vision API response to JSON. But today it suddenly stopped working.

After doing,
response = client.document_text_detection(image=image)

Then I convert it to JSON using,
json_string = MessageToJson(response)

But I am getting this error
Screenshot 2020-10-05 at 3 48 06 PM

I need your help as this is being used in a production environment and I am not able to figure out why I suddenly start getting this error.

python

Most helpful comment

I think I know what's happening.
I am assuming that the client library version was updated recently. The new versions of Google Cloud API client libraries use Proto Plus to define message types.
The new means of converting to/from json is now:

response = client.document_text_detection(image=image)
json_string = type(response).to_json(response)  # Also works to use the message type directly, e.g.
                                                # json_string = MyMessageType.to_json(response)

to_json and from_json aren't visible to message instances; they _must_ be called from the message class, and the message itself is then passed in.
https://proto-plus-python.readthedocs.io/en/stable/messages.html#serialization

Let us know if that solves the problem!

All 3 comments

@monark12 Could you verify what library has the client.document_text_detection function?

I think I know what's happening.
I am assuming that the client library version was updated recently. The new versions of Google Cloud API client libraries use Proto Plus to define message types.
The new means of converting to/from json is now:

response = client.document_text_detection(image=image)
json_string = type(response).to_json(response)  # Also works to use the message type directly, e.g.
                                                # json_string = MyMessageType.to_json(response)

to_json and from_json aren't visible to message instances; they _must_ be called from the message class, and the message itself is then passed in.
https://proto-plus-python.readthedocs.io/en/stable/messages.html#serialization

Let us know if that solves the problem!

I've had the same problem as OP with proto 3.13 and the latest google's cloud speech library -- @software-dov's solution works for me :+1:

Was this page helpful?
0 / 5 - 0 ratings