Skip to Content
API 参考OpenAI 兼容协议Audio Speech

Audio Speech API

将文字转为语音。兼容 OpenAI 的 POST /v1/audio/speech,响应体直接是音频文件(二进制),不是 JSON。

系列模型说明
ElevenLabselevenlabs/eleven_v4见 ElevenLabs
Gemini TTSgoogle/gemini-3.8-flash-tts 等见 Gemini TTS

voice、response_format 等参数的可选值因模型而异,请以系列页为准。

快速开始

base_urlhttps://api.ofox.ai/v1
接口地址https://api.ofox.ai/v1/audio/speech
模型 IDgoogle/gemini-3.8-flash-tts
speech.py
import os from openai import OpenAI client = OpenAI(base_url="https://api.ofox.ai/v1", api_key=os.environ["OFOX_API_KEY"]) with client.audio.speech.with_streaming_response.create( model="google/gemini-3.8-flash-tts", voice="Kore", input="你好,欢迎使用 OfoxAI 语音合成。", response_format="wav", ) as resp: resp.stream_to_file("speech.wav")

请求参数

model必填string

模型 ID,见上表。

input必填string

要合成的文字。为空时返回 400。

voicestring

音色。Gemini TTS 必填,取值为 Google 的音色名(如 Kore);ElevenLabs 取值为音色 ID(如 21m00Tcm4TlvDq8ikWAM)。OpenAI 的音色名(如 alloy)不适用于这两个系列。

音频格式。可选值因模型而异:Gemini TTS 支持 wav、pcm;ElevenLabs 支持 mp3、opus、wav、pcm。传入不支持的格式返回 400。

语气与风格说明,如「用欢快的语气朗读」。适用于 Gemini TTS。

语言代码,如 cmn-CN(Gemini TTS)、zh(ElevenLabs)。不传时由模型根据文字判断。

stream_formatstring· 可选sse

传 sse 时以 Server-Sent Events 流式返回音频分片,适用于 Gemini TTS。

供应商专有参数,透传给对应供应商,例如 ElevenLabs 的 {"elevenlabs": {"voice_settings": {...}}}。见各系列页。

响应

成功时 HTTP 200,响应体为音频文件,Content-Type 与格式对应:

response_formatContent-Type
mp3audio/mpeg
wavaudio/wav
opusaudio/ogg
pcmaudio/L16;rate=<采样率>;channels=1,无文件头的 16 位 PCM

请将响应体直接写入文件。出错时响应为 JSON,Content-Type 为 application/json,请先检查状态码,避免把错误信息写成音频文件。

常见报错

HTTP报错原文原因处理方法
400model and input are required.缺少 model,或 input 为空补全参数
400voice is required for speech completionGemini TTS 未传 voice传入音色名,如 Kore
400unsupported Gemini response_format: mp3该模型不支持所选格式改用系列页列出的格式
400Request contains an invalid argument.Gemini TTS 的音色名不存在改用有效的音色名
404A voice with voice_id 'alloy' was not found.ElevenLabs 的音色 ID 不存在改用有效的音色 ID
400 endpoint_not_supportedModel 'openai/gpt-5.4' does not support the /v1/audio/speech endpoint on this platform.所选模型不是语音合成模型改用上表中的模型

完整的错误码说明见错误处理。

最后更新于