使用dfcx-scrapi导出Dialogflow Agent至GCS Bucket时遇格式错误
问题描述
我编写了一个用于定期将Dialogflow Agent导出至GCS Bucket的Python脚本。运行时指定data_format为JSON或JSON_PACKAGE均触发ValueError,提示“4 is not a valid ExportAgentRequest.DataFormat”;使用的dfcx-scrapi版本为1.8.1,尝试指定BLOB格式仍报错。修改agents.py相关代码后脚本可运行,但无法导出所需的JSON格式(必须导出JSON而非BLOB)。
脚本代码
from dfcx_scrapi.core.agents import Agents gcs_bucket = "gs://<my-bucket-name>" print("Starting connection to Dialogflow using credentials") agent_obj = Agents(creds_path, agent_id) print("Beginning Agent export to Bucket") lro_id = agent_obj.export_agent(agent_id, gcs_bucket_uri=gcs_bucket, data_format="JSON") print("Long Running Operation ID: "+lro_id)
错误信息
Starting connection to Dialogflow using credentials Beginning Agent export to Bucket Traceback (most recent call last): File "redacted-path/my_custom_script", line redacted, in <module> lro_id = agent_obj.export_agent(agent_id, gcs_bucket_uri=gcs_bucket, data_format="JSON_PACKAGE") File "redacted-path/dfcx_scrapi/core/scrapi_base.py", line 322, in wrapper return func(self, *args, **kwargs) File "redacted-path/dfcx_scrapi/core/agents.py", line 397, in export_agent json_format = types.agent.ExportAgentRequest.DataFormat(4) File "/redacted-path/env/lib/python3.9/enum.py", line 384, in __call__ return cls.__new__(cls, value) File "redacted-path/env/lib/python3.9/enum.py", line 702, in __new__ raise ve_exc ValueError: 4 is not a valid ExportAgentRequest.DataFormat
解决方案
问题根源
dfcx-scrapi v1.8.1的export_agent方法中硬编码了DataFormat(4)对应JSON格式,但该版本依赖的Dialogflow CX Python客户端库中,ExportAgentRequest.DataFormat枚举值并不包含4,导致枚举转换失败。
修复方法
方法1:升级dfcx-scrapi到最新版本
官方已修复枚举值不匹配问题,执行以下命令升级:
pip install --upgrade dfcx-scrapi
升级后直接运行原脚本,指定data_format="JSON"或JSON_PACKAGE即可正常导出。
方法2:手动修改agents.py(无法升级时使用)
- 找到本地环境中
dfcx_scrapi/core/agents.py文件,定位到export_agent方法中报错的行(约第397行)。 - 将硬编码的枚举值代码替换为字符串映射:
# 原代码 json_format = types.agent.ExportAgentRequest.DataFormat(4) # 修改为 json_format = types.agent.ExportAgentRequest.DataFormat[data_format] - 保存修改后重新运行脚本,即可正确传入
JSON或JSON_PACKAGE格式参数导出。
内容的提问来源于stack exchange,提问作者Sudharshana VP
相关产品推荐
相关产品推荐

