Give your app a voice. Turn text into natural-sounding audio, pick a voice and format, and stream it so playback starts before the whole clip is ready.
Why: a speech model takes text and returns audio bytes — the mirror image of transcription, and the last step in a voice loop (STT → LLM → TTS). When: use it for spoken responses, accessibility, and audio content. Where: you choose a voice and an output format (mp3, wav, opus); save or stream the bytes like any binary response.
# Text-to-speech is provider-specific (OpenAI here).
from openai import OpenAI
client = OpenAI()
resp = client.audio.speech.create(
model="tts-1",
voice="alloy",
input="Your refund has been processed and will arrive in three days.",
)
resp.stream_to_file("reply.mp3")Why: waiting for a whole clip to render before playback feels slow — streaming lets playback start while the rest is still generating, just like streaming text. When: stream any spoken response a user waits on live. Where: forward the audio chunks to the client as they arrive instead of buffering the full file.
# Stream the audio out chunk by chunk instead of buffering the whole file.
with client.audio.speech.with_streaming_response.create(
model="tts-1", voice="alloy",
input="Streaming audio starts playing before it has finished generating.",
) as resp:
with open("streamed.mp3", "wb") as f:
for chunk in resp.iter_bytes():
f.write(chunk) # in a web app, forward each chunk to the clientWhy: STT, an LLM, and TTS compose into a spoken assistant — audio in, understanding in the middle, audio out. When: this is the architecture behind voice agents; each stage is a separate call you can swap or cache. Where: latency compounds across three calls, so stream wherever possible and keep the LLM turn short.
user speaks
|
STT ------ audio -> transcript
|
LLM ------ transcript -> reply text (your prompt, tools, RAG...)
|
TTS ------ reply text -> audio
|
app plays it back
Three calls, three costs, three latencies. Stream the first and last.