Create speech
audio.speech.create(SpeechCreateParams**kwargs) -> BinaryResponseContent
POST/audio/speech
Generates audio from the input text.
Returns the audio file content, or a stream of audio events.
Parameters
input: str
The text to generate audio for. The maximum length is 4096 characters.
maxLength4096
instructions: Optional[str]
Control the voice of your generated audio with additional instructions. Does not work with tts-1 or tts-1-hd.
maxLength4096
speed: Optional[float]
The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default.
minimum0.25
maximum4
Returns
BinaryResponseContent
Create speech
from pathlib import Path
import openai
speech_file_path = Path(__file__).parent / "speech.mp3"
with openai.audio.speech.with_streaming_response.create(
model="gpt-4o-mini-tts",
voice="alloy",
input="The quick brown fox jumped over the lazy dog."
) as response:
response.stream_to_file(speech_file_path)