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

Python调用Google TTS synthesize_speech报位置参数数量错误求助

问题原因

这个报错和TensorFlow版本升级无关,是Google TTS SDK版本迭代后接口传参规则变更导致的。
你当前用的texttospeech_v1版本中,synthesize_speech方法除了客户端实例自身的隐式参数外,仅支持1~2个位置参数,你按旧版写法直接顺序传入synthesis_input、voice、audio_config三个位置参数,合计传入了4个位置参数,直接触发参数数量不匹配的报错。

修复方案

选任意一种改法即可:

  • 最小改动方案:改用关键字参数传参
    把原有调用行替换为显式指定参数名的写法,不需要调整其他逻辑:
    response = client.synthesize_speech(
        input=synthesis_input,
        voice=voice,
        audio_config=audio_config
    )
    
  • 官方标准写法:构造统一请求对象传入
    先导入请求类,将所有参数封装到请求对象中再传入接口:
    # 顶部导入区新增导入
    from google.cloud.texttospeech_v1 import SynthesizeSpeechRequest
    
    def say(text):
        """assumes ogg is the output format"""
        synthesis_input = texttospeech_v1.SynthesisInput(text=text)
        # 封装请求
        request = SynthesizeSpeechRequest(
            input=synthesis_input,
            voice=voice,
            audio_config=audio_config
        )
        response = client.synthesize_speech(request=request)
        # 下方原有文件写入、播放逻辑保持不变
        tmpfile = tempfile.NamedTemporaryFile(delete=False)
        tmpfile.write(response.audio_content)
        wave_obj = sa.WaveObject.from_wave_file(tmpfile.name)
        audio = wave_obj.play()
        while audio.is_playing():
            continue
    
其他待修正问题
  • 代码中simpleaudio库的导入被注释了,运行前需要取消#import simpleaudio as sa的注释,否则会触发sa未定义的错误。
  • 函数注释标注输出格式为ogg,但实际音频配置中指定的是LINEAR16编码(对应wav格式),如果后续调整音频编码,记得同步修改注释和播放逻辑,避免格式不匹配导致播放失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 15:36:23