Sub topic 1
How speech fits around an LLM
Prerequisites
This chapter assumes you can:
- Write basic Python, including functions and file handling.
- Call an HTTP or SDK-based LLM chat API.
- Store an API key in an environment variable.
- Recognize an audio file such as MP3 or WAV.
The core idea
A text-based LLM reads text and writes text. It cannot hear or speak. To build a voice app, you add two components around it. Speech-to-text (STT), also called transcription, turns recorded audio into text. Text-to-speech (TTS), also called speech synthesis, turns text into audio.
The result is a three-stage pipeline, often called a cascaded pipeline:
- STT converts the user's audio to a transcript.
- The LLM reads the transcript and writes a reply.
- TTS converts the reply to audio.
Each stage is a separate API call. The LLM stage does not change: it still takes text in and returns text. This is why you can add speech to an existing chat app without rewriting its logic.
What each stage does
| Stage | Input | Output | Typical call |
|---|---|---|---|
| STT | Audio file | Text, optionally with timestamps | `client.audio.transcriptions.create` |
| LLM | Text and chat history | Reply text | `client.chat.completions.create` |
| TTS | Text | Audio bytes | `client.audio.speech.create` |
The examples in this tutorial use the OpenAI Python SDK. Other providers expose the same three roles under different names.
A first look in code
The sketch below runs the whole pipeline for one turn. Later chapters explain each call in detail, so focus on the shape for now.
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
# Stage 1: speech to text
with open("question.mp3", "rb") as f:
user_text = client.audio.transcriptions.create(model="whisper-1", file=f).text
# Stage 2: the LLM
completion = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "Answer in one or two short spoken sentences."},
{"role": "user", "content": user_text},
],
)
reply_text = completion.choices[0].message.content
# Stage 3: text to speech
with client.audio.speech.with_streaming_response.create(
model="tts-1", voice="alloy", input=reply_text
) as response:
response.stream_to_file("reply.mp3")
print(user_text)
print(reply_text)The system prompt asks for short answers on purpose. A long written answer is tiring to hear, and it takes longer to synthesize.
Where each stage adds latency
Latency is the delay between the user finishing a sentence and hearing the start of the reply. In a cascaded pipeline, the delays add up, because each stage normally waits for the one before it.
- STT must receive the whole recording before it can return a transcript. Longer audio means a longer wait.
- LLM time depends on the model and on the length of the reply.
- TTS must generate audio before playback. Streaming the audio lets playback start earlier.
If the three stages take 1.0, 1.5, and 1.0 seconds, the user waits about 3.5 seconds, plus network time and audio upload. That is long for a conversation. Later chapters return to this with a latency budget and ways to shorten it.
Why use a cascaded pipeline
- Control. You can read, log, filter, or edit the text between stages.
- Flexibility. You can swap the STT, LLM, or TTS provider independently.
- Reuse. Existing prompts, tools, and chat history keep working.
The cost is added latency, and the loss of vocal detail such as tone and pauses, since the LLM sees only the transcript. A later chapter compares this design with realtime speech-to-speech models.
Exercise
A user speaks a question and your app takes about 4 seconds to start playing the answer. Name the three stages in the pipeline in order, and explain why the delays add up.
Show answer
The stages are speech-to-text, the LLM, and text-to-speech. Each stage normally waits for the previous stage to finish, so the user waits for the sum of all three delays, plus network and upload time.