In my program I have been using google-cloud-vision 3.4.5 in Python 3.11 for months. Today, I tried to set up my program to run on a new computer, and I noticed that the program always failed at the point in the code where I called proto.Message.to_dict to convert my image annotation result to a dictionary. Python was reporting that the result was not of type “Message”. I was then able to reproduce the problem on my old computer by creating a new Python 3.11 Anaconda environment, which has a bunch of dependencies newer than the existing one. When I debugged on my old environment vs the new environment, I noticed on the old one the AnnotateImageResponse has the metaclass MessageMeta, whereas on the new one the type is GeneratedProtocolMessageType. In PyCharm:
I tried downgrading some packages (googleapis-common-protos, proto-plus, protobuf) and it didn’t work, I also tried upgrading google-cloud-vision to latest 3.7.2 and it didn’t work either. The only way I stumbled upon that works is to call google.protobuf.json_format.MessageToDict, which is called internally by proto.Message.to_dict so I’m very confused why this makes a difference:
def protobuf_message_to_dict(message):
try:
return proto.Message.to_dict(message)
except:
logger.error("proto.Message.to_dict failed, retrying with MessageToDict")
# arguments set to make result consistent with defaults set in proto.Message.to_dict
return MessageToDict(message, use_integers_for_enums=True, preserving_proto_field_name=True, including_default_value_fields=True)
As shown above, I also had to keep proto.Message.to_dict around too, because calling MessageToDict directly doesn’t work on the old environment and fails with this error that I don’t understand:
Does anybody know what has changed recently that is causing me to encounter this problem?