You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grpc转换问题:RepeatedCompositeContainer转dict时属性缺失报错

解决google._upb._message.RepeatedCompositeContainer转字典的报错问题

错误原因

MessageToDict是为单个Protobuf Message实例设计的工具,而user.results属于RepeatedCompositeContainer——这是一个类似列表的集合,内部存储多个Message对象。直接将集合传入MessageToDict时,函数会尝试访问集合本身的Protobuf描述符属性(如file),但该集合类型并不具备这类属性,因此触发报错。

正确处理方式

遍历RepeatedCompositeContainer中的每个Message实例,逐个用MessageToDict转换,最终得到由字典组成的列表:

from google.protobuf.json_format import MessageToDict

# 遍历容器内的每个消息对象,逐个转换为字典
convert_list = [MessageToDict(item, descriptor_pool=True) for item in user.results]

如果需要将结果包装为带键名的字典(比如给列表指定一个标识键),可以再嵌套一层:

convert_dict = {"results": convert_list}

内容的提问来源于stack exchange,提问作者Tripti Kothari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:47:02