Lesson 2 of 3 · AI Voice & Audio Engineering
Generating Speech
A developer at a language learning startup needed to generate audio for 12,000 vocabulary words in 14 languages by Friday. She had been recording native speakers at $0.15 per word -- $25,200 and six weeks of studio time. Instead, she wrote a 47-line script that called the TTS API, generating all 12,000 audio files in under three hours for about $18 total. The quality was good enough that user satisfaction scores actually increased -- the AI voices were more consistent in pronunciation and pacing than the rotating cast of human narrators.
This lesson teaches you how to make that API call, what every parameter does, and how to choose the right settings for your use case.
The Endpoint
All text-to-speech requests go to a single endpoint:
The request body is JSON with a small set of parameters that control everything about the output.
Required Parameters
model
One of gpt-4o-mini-tts, tts-1, or tts-1-hd. This determines voice quality, available features, and cost.
input
The text to convert to speech. Maximum length is 4,096 characters. For longer texts, you will need to chunk the input and concatenate the audio output -- more on this in the next lesson.
voice
The voice to use. Must be one of the voices supported by the chosen model. For gpt-4o-mini-tts: alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer, verse, marin, or cedar. For tts-1 and tts-1-hd: alloy, ash, coral, echo, fable, nova, onyx, sage, or shimmer.
Optional Parameters
instructions (gpt-4o-mini-tts only)
A natural language string that tells the model how to speak. This is where the real power lies.
The instructions can control:
- Accent: "Speak with a British accent" or "Use a subtle French accent"
- Emotion: "Sound excited" or "Speak with gentle concern"
- Pacing: "Speak slowly and deliberately" or "Quick, energetic delivery"
- Character: "Sound like a friendly teacher" or "Professional news anchor"
The model interprets instructions as guidance, not rigid rules. Asking it to "speak in iambic pentameter" will not produce metered speech. Asking it to "speak warmly and slowly" will reliably produce warmer, slower speech. Keep instructions focused on tone, emotion, and pacing rather than precise phonetic control.
response_format
The audio format of the output. Default is mp3. Options:
| Format | Use Case | File Size | Quality |
|---|---|---|---|
| mp3 | General purpose, web playback | Medium | Good |
| opus | Streaming, low-latency, WebRTC | Smallest | Good at low bitrates |
| aac | iOS/Safari, Apple ecosystem | Medium | Good |
| flac | Archival, lossless audio | Largest | Lossless |
| wav | Audio processing pipelines | Largest | Uncompressed |
| pcm | Raw audio for real-time processing | Largest | Uncompressed, no headers |
speed
A float between 0.25 and 4.0. Default is 1.0. Values below 1.0 slow the speech down; values above speed it up.
In practice, usable range is about 0.7 to 1.3. Below 0.7, speech sounds unnaturally stretched. Above 1.3, it becomes difficult to understand. The sweet spot for most applications is between 0.9 and 1.1.
A Complete Request
Here is a full example using gpt-4o-mini-tts with all parameters:
Using the OpenAI Python SDK (recommended for production):
The Python SDK handles retries, error parsing, and streaming for you. Use raw HTTP only when working in a language without an official SDK or when you need precise control over the HTTP connection (custom timeouts, proxy configuration, etc.).
Handling the Response
The API returns the audio file directly in the response body. The Content-Type header matches your requested format (audio/mpeg for mp3, audio/opus for opus, etc.).
For web applications, you can serve the audio directly to the browser:
Error Handling
Common failure modes:
- 400 Bad Request: Input text exceeds 4,096 characters, invalid voice name, or invalid model
- 401 Unauthorized: Invalid or missing API key
- 429 Too Many Requests: Rate limit exceeded -- implement exponential backoff
- 500 Internal Server Error: Transient server issue -- retry with backoff
Validate input length before sending the request. Implement retry logic with exponential backoff for 429 and 500 errors. Cache generated audio when the same text will be requested multiple times.
Send text longer than 4,096 characters without chunking. Retry immediately on rate limits -- you will make the problem worse. Generate the same audio repeatedly without caching -- it wastes money and adds latency.
Generate Your First Speech Audio
beginnerUse ← → to navigate, Space to mark complete