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

使用google-cloud-texttospeech1.0.1报无SynthesisInput属性错误如何解决

报错原因

SynthesisInput 是 google-cloud-texttospeech 2.0.0及以上版本才新增的接口,你当前使用的1.0.1版本属于旧版本,不存在该顶层接口,因此触发属性缺失报错。


解决方案1:升级包版本(推荐)

直接升级到2.0.0以上版本即可直接适配官方最新示例代码,对应安装命令:

  • conda安装(conda-forge源):conda install -c conda-forge google-cloud-texttospeech>=2.0.0
  • pip安装:pip install --upgrade google-cloud-texttospeech

解决方案2:适配旧版本API写法

如果当前环境无法升级依赖,将代码中对应的接口调用改为1.x版本的写法即可,完整示例如下:

from google.cloud import texttospeech

# 初始化客户端
client = texttospeech.TextToSpeechClient()

# 构造文本输入(旧版本需要加.types前缀)
synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")

# 构造语音参数
voice = texttospeech.types.VoiceSelectionParams(
    language_code="en-US",
    ssml_gender=texttospeech.enums.SsmlVoiceGender.NEUTRAL
)

# 构造音频输出配置
audio_config = texttospeech.types.AudioConfig(
    audio_encoding=texttospeech.enums.AudioEncoding.MP3
)

# 调用接口生成音频
response = client.synthesize_speech(
    input=synthesis_input,
    voice=voice,
    audio_config=audio_config
)

# 保存音频文件
with open("output.mp3", "wb") as out:
    out.write(response.audio_content)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 20:45:03