Sub topic 3
Transcribing audio with an API call
This chapter assumes you can write basic Python, have called an LLM chat API through an SDK, and keep API keys in environment variables. You also need to know what an MP3 or WAV file is. Here you will send one audio file to a hosted transcription endpoint and read the text it returns.
The working model
A transcription endpoint accepts an audio file and returns text. It is a speech-to-text (STT) call, the first stage of the pipeline around an LLM. The request is a multipart HTTP upload, so the SDK sends the file bytes together with a few form fields. The server decodes the audio, runs a speech recognition model, and returns the transcript.
Three things are required:
- model: the name of the speech model. This chapter uses
whisper-1. - file: a binary file object, opened in
"rb"mode. - credentials: the SDK reads
OPENAI_API_KEYfrom the environment.
A first request
from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
with open("meeting.mp3", "rb") as audio_file:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
)
print(transcript.text)The with block closes the file after the upload finishes. The return value is an object, and transcript.text holds the full transcript as one string. With the default response format, the string has no timestamps and no speaker labels.
Run the script from a directory that contains meeting.mp3. A short clip of a minute or two is a good first test, because it returns quickly and costs little.
What happens inside the call
- The SDK builds a multipart form with the file and the model name.
- The service decodes the container and codec, then resamples the audio internally. You do not need to convert a normal MP3 or WAV file first.
- The model returns text, and the SDK parses it into the response object.
The call is synchronous. Your code waits until the whole file has been processed, so a long file means a long wait. The next chapters cover how to shape the output and how to deal with long recordings.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `FileNotFoundError` | Wrong working directory or filename | Use an absolute path, or check with `os.path.exists` |
| 401 authentication error | Missing or wrong API key | Check that `OPENAI_API_KEY` is set in the shell that runs Python |
| 400 error about file format | Unsupported or corrupt container | Re-export the audio as MP3 or WAV |
| 413 error, or an error about size | File is larger than the provider's limit | Split the recording into chunks, covered later |
| Text from the wrong language or garbled text | Silence, noise, or mixed languages | Set the language explicitly, covered next |
| Opening the file in text mode raises a decode error | Used `"r"` instead of `"rb"` | Always open audio in binary mode |
Checking the result
A transcript is a model's best guess, not ground truth. Names, product terms, and numbers are the most common mistakes. Read the output against the audio at least once before you build anything on it. A later chapter shows how to measure accuracy with word error rate instead of relying on listening.
Exercise
Your script raises an error as soon as it calls the transcription endpoint, and the traceback mentions a decoding problem. You wrote open("meeting.mp3") with no mode. What is wrong, and how do you fix it?
Show answer
Without a mode, Python opens the file in text mode and tries to decode the bytes as text. Audio is binary data, so open it with open("meeting.mp3", "rb") and pass that file object as file.
Exercise
What does transcript.text contain when you call client.audio.transcriptions.create with only model and file?
Show answer
It contains the full transcript as a single plain string, with no timestamps or speaker labels.