Sub topic 4
Tuning transcription: language, prompts, formats, timestamps
This chapter assumes you can already send one audio file to a transcription endpoint and read transcript.text, as shown in the previous chapter. It covers the request options that change the accuracy and the shape of the result: language, prompt, response_format, and timestamp_granularities. The examples use the OpenAI Python SDK and the whisper-1 model. Other providers use different parameter names, but the same ideas apply.
What the options control
A default transcription call makes the model guess everything. It detects the language, picks its own spelling for unusual words, and returns plain text. Each option removes one guess or adds one piece of output.
| Option | What it changes | Typical use |
|---|---|---|
| `language` | Skips language detection and fixes the spoken language | You know the language ahead of time |
| `prompt` | Gives the model text to imitate in spelling and style | Names, jargon, acronyms |
| `response_format` | Chooses the output shape | Subtitles, timing data, plain text |
| `timestamp_granularities` | Adds segment or word timings | Search, captions, audio-to-text alignment |
| `temperature` | Controls randomness in decoding | Rarely needed; leave the default |
Setting the language
Whisper detects the language from the first seconds of audio. Detection is usually right, but it fails in predictable cases:
- The recording starts with silence, music, or a hold tone.
- The first speaker has a strong accent.
- The speaker mixes two languages.
- The clip is very short.
When detection fails, the model may transcribe a French recording as if it were English, or translate it into another language by accident. Setting language removes that risk. It also tends to improve accuracy and slightly reduce latency, because the model skips detection.
The value is an ISO 639-1 code such as en, fr, de, or ja. Passing a full name like "English" is an error in the OpenAI API.
from openai import OpenAI
client = OpenAI()
with open("interview_fr.mp3", "rb") as audio_file:
result = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
language="fr",
)
print(result.text)If you set the wrong language, the model does not refuse. It tries to force the audio into that language and produces poor text. Only set language when you are sure. If your app accepts uploads in unknown languages, leave it unset, or run detection once and reuse the result.
Mixed-language audio is a weak spot. If a speaker switches between English and Spanish, a fixed language will push the other language toward the one you chose. Test with representative recordings before you decide.
Using the prompt to fix vocabulary
The prompt parameter is not an instruction. The model does not "follow" it the way a chat model follows a system message. It is context text, treated as if it were the transcript of the audio just before your file. The model then tends to match its spelling, casing, and punctuation.
That makes the prompt good for:
- Product and company names (
Acme,Kubernetes). - Acronyms and jargon (
OAuth,gRPC). - Names of people and places.
- Style, such as punctuation, or a particular spelling convention.
A short list of terms works, and so does a sentence written in the style you want.
from openai import OpenAI
client = OpenAI()
with open("standup.mp3", "rb") as audio_file:
result = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
language="en",
prompt="Acme, Kubernetes, OAuth, gRPC, Priya Raghunathan.",
)
print(result.text)Keep these limits in mind:
- Length. Only the last 224 tokens of the prompt are used for
whisper-1. Put the most important terms at the end. - No commands. A prompt such as "Summarize this" or "Translate to German" does not work. The model may even echo it.
- Language match. Write the prompt in the same language as the audio.
- Hallucinated terms. A listed term can appear in the output even when nobody said it, especially in noisy or near-silent audio. Keep the list to terms that really occur.
- Continuing a long recording. When you transcribe chunks (see the chapter on long audio), pass the end of the previous chunk's text as the prompt for the next one. This keeps names and punctuation consistent across the boundary.
def transcribe_chunks(client, chunk_paths):
previous_text = ""
pieces = []
for path in chunk_paths:
with open(path, "rb") as f:
result = client.audio.transcriptions.create(
model="whisper-1",
file=f,
prompt=previous_text[-500:],
)
pieces.append(result.text)
previous_text = result.text
return " ".join(pieces)The slice [-500:] keeps the prompt well within the token limit and keeps the most recent words.
Choosing a response format
response_format decides what comes back. The default is json, which holds only a text field.
| Format | Contents | Use it when |
|---|---|---|
| `json` | `{"text": "..."}` | You only need the words |
| `text` | A plain string | You want to write it straight to a file |
| `srt` | SubRip subtitles with times | Video captions |
| `vtt` | WebVTT subtitles with times | Web video players |
| `verbose_json` | Text, language, duration, segments, optional words | You need timing or quality signals |
Two details cause confusion. First, with text, srt, and vtt, the SDK returns a plain string, not an object. Calling .text on it raises an AttributeError. Second, timestamp options only work with verbose_json. If you pass timestamp_granularities with another format, the API returns an error.
with open("talk.mp3", "rb") as audio_file:
srt_text = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file,
response_format="srt",
)
with open("talk.srt", "w", encoding="utf-8") as out:
out.write(srt_text)Timestamps
Setting response_format="verbose_json" returns the text split into segments. A segment is a phrase or sentence-sized span with a start and end time in seconds. Adding timestamp_granularities selects the level of detail:
["segment"]gives segment timings. This is also the default forverbose_json.["word"]gives a start and end time for each word.["segment", "word"]gives both.
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", "word"],
)
for segment in result.segments:
print(f"{segment.start:.1f}-{segment.end:.1f}: {segment.text}")
for word in result.words[:10]:
print(f"{word.start:.2f} {word.word}")The verbose_json result also carries language (the detected or supplied language) and duration (the audio length in seconds). Segments may include fields such as avg_logprob and no_speech_prob. A segment with a high no_speech_prob is probably silence or noise, and is a good candidate to filter out before you pass text to a chat model.
Word timestamps add processing time, so request them only when you need them. Word timings are estimates and can drift by a fraction of a second, so do not rely on them for frame-exact video editing.
Timestamps across chunks
Each chunk restarts its clock at zero. If you split a recording into 10-minute pieces, add the chunk's start offset to every timestamp, or the second chunk will appear to begin at time zero:
CHUNK_SECONDS = 10 * 60
def shift_segments(segments, chunk_index):
offset = chunk_index * CHUNK_SECONDS
return [
{"start": s.start + offset, "end": s.end + offset, "text": s.text}
for s in segments
]This only works when every chunk except the last has exactly the same length. Cutting on silence instead of fixed lengths means you must track each chunk's real start time.
Common errors
- Wrong language code.
language="English"fails. Use"en". - Calling
.texton a string. Withsrt,vtt, ortext, the result is already a string. - Timestamps without
verbose_json. The request is rejected. - Prompt as instruction. The model ignores commands and may echo them.
- Oversized prompt. Text beyond the final 224 tokens is dropped, so keep key terms at the end.
- Forcing a language on mixed audio. The off-language parts degrade.
Exercise
You transcribe a support call and need a WebVTT caption file plus the exact start time of each word. A colleague proposes responseformat="vtt" with timestampgranularities=["word"]. Why does this fail, and what do you do instead?
Show answer
Timestamp granularities only work with responseformat="verbosejson", so the vtt request is rejected. Request verbosejson with timestampgranularities=["segment", "word"], then read result.segments and result.words. Build the WebVTT file yourself from the segment start, end, and text values, or make a second call with response_format="vtt" if you need the service's own formatting.
Exercise
A French recording begins with 15 seconds of hold music, and the output comes back in English. Which option do you change, and what is the risk of the fix?
Show answer
Set language="fr" so the model skips detection, which was fooled by the music. The risk is that if the audio is not actually French, or switches languages partway through, the model forces the audio into French and the text degrades. Set the language only when you are sure of it.