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

运行Google翻译API代码报AttributeError: TranslationServiceClient无translate_document属性如何解决

AttributeError: 'TranslationServiceClient' object has no attribute 'translate_document' 排查方案

错误产生原因

  • 安装的google-cloud-translate包版本与使用的v3beta1接口不匹配:版本过低时translate_document方法还未上线,版本过高时该方法已被更名或从beta接口中移除
  • 新版v3beta1 SDK已将单文档同步翻译方法更名,原translate_document方法名被废弃
  • 本地安装的SDK包未完整包含v3beta1模块,缺失对应接口实现

解决办法

  1. 先执行命令重新安装匹配版本的SDK:
pip uninstall google-cloud-translate -y
pip install google-cloud-translate==3.12.0
  1. 调整代码中的方法调用名,将client.translate_document替换为client.translate_document_sync,其余请求参数无需修改,修改后的调用代码如下:
response = client.translate_document_sync(
    request={
        "parent": parent,
        "target_language_code": "fr-FR",
        "document_input_config": document_input_config,
    }
)
  1. 若仍有问题,可直接切换到正式版v3接口,将导入语句修改为from google.cloud import translate_v3 as translate,直接使用client.translate_document方法即可正常调用。

内容的提问来源于stack exchange,提问作者Roopendra Rajput

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 14:09:03