Sub topic 4
Tuning transcription: language, prompts, formats, timestamps
This chapter assumes you can already send one file to a transcription endpoint, as covered in the previous chapter. You should be comfortable with Python and an API key set in an environment variable. Here you change four request options that decide how accurate and how useful the output is.
The four options
| Option | What it controls | Typical use |
|---|---|---|
| `language` | The spoken language, as an ISO-639-1 code such as `en` | Skip auto-detection and avoid wrong guesses |
| `prompt` | Text that hints at spelling and style | Names, product terms, acronyms |
| `response_format` | The shape of the returned data | Plain text, subtitles, or structured JSON |
| `timestamp_granularities` | Whether you get segment or word times | Search, captions, jump-to-moment links |
Setting the language
By default the model detects the language from the first seconds of audio. Short clips, noise, or a speaker who switches languages can fool it. If you know the language, pass it. This usually improves accuracy and can reduce latency, because the model skips detection.
Using a prompt for vocabulary
The prompt is not an instruction. The model does not obey it like a chat message. It acts as prior text, so the model treats the words in it as likely spellings. A list of names and terms works well. A sentence in the style you want also works, such as one that uses punctuation and capitalization.
Keep prompts short. Very long prompts add little. Use the prompt only for vocabulary the model gets wrong, and check the result, since a prompt can also push the model toward words that were never spoken.
Choosing a response format
The default json format returns only the text. Other formats add structure.
textreturns a plain string.srtandvttreturn subtitle files with timing.verbose_jsonreturns the text plus segments, each with start and end times, and the detected language.
Requesting timestamps
Timestamps need verbose_json. Setting timestamp_granularities to ["segment"] gives one time range per segment. Adding "word" gives times for each word, at the cost of a little extra processing.
from openai import OpenAI
client = OpenAI()
with open("meeting.mp3", "rb") as audio_file:
result = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
language="en",
prompt="Acme, Kubernetes, OAuth",
response_format="verbose_json",
timestamp_granularities=["segment"],
)
for segment in result.segments:
print(f"{segment.start:.1f}-{segment.end:.1f}: {segment.text}")Each line prints a time range in seconds and the text spoken in it. You can store these ranges to link a search hit back to the exact moment in the recording.
Common errors
- Timestamps missing. If
timestamp_granularitiesis set but the format is notverbose_json, the request is rejected or the timing fields are absent. Set the format first. - Wrong language code. Use a two-letter code like
enorde, not a full name. - Prompt treated as a command. Writing "Ignore filler words" in the prompt does not work. Give example text instead.
- Reading
.segmentson a plain response. Onlyverbose_jsonprovides segments, so the attribute will not exist on other formats.
Exercise
A recording of a support call has product names such as "Kubernetes" and "OAuth" misspelled, and you also need to jump to the moment each topic starts. Which options do you set?
Show answer
Set prompt to a short list of the product terms, set response_format to verbose_json, and set timestamp_granularities to ["segment"]. Add language if you know it. The prompt improves spelling, and the segment start times let you link to each topic.