Sub topic 6
Tuning Decoding: Language, Beam Size, Temperature, and Prompts
Whisper does not read one fixed answer directly from the audio. It generates text token by token. At each step, decoding chooses the next token that best fits the audio and the tokens already produced. Decoding options control this choice.
This chapter assumes basic Python, pip, command-line use, and a working Whisper installation with FFmpeg. It also assumes you know how to call model.transcribe() for a simple file.
The main decoding controls
| Option | Purpose | Typical use |
|---|---|---|
| `language` | Tells Whisper which spoken language to expect | Use it when the language is known |
| `task` | Selects transcription or English translation | Use `"transcribe"` or `"translate"` |
| `beam_size` | Keeps several candidate token sequences during search | Use with deterministic decoding |
| `temperature` | Controls randomness in token selection | Use low values for stable output |
| `initial_prompt` | Provides vocabulary or style context | Use for names, acronyms, and domain terms |
Language and task
Whisper can detect the language automatically, but detection can fail on short clips, noisy audio, or recordings with multiple languages. If you know the language, pass its language code:
import whisper
model = whisper.load_model("small")
result = model.transcribe(
"interview.mp3",
language="en",
task="transcribe",
fp16=False,
)
print(result["text"])The task value "transcribe" keeps the spoken language. The value "translate" asks Whisper to produce English text, even when the speech is in another supported language. Translation is not the same as transcription followed by a separate translation service. It is a decoding task learned by the model.
Beam search
Beam search is a decoding strategy. Instead of keeping only the single best next token, it keeps several partial answers, called beams. It later selects the complete sequence with the best score.
A larger beam_size can improve consistency when several word sequences appear plausible. It also increases computation time and does not guarantee better accuracy. Beam search is most useful with temperature=0.
import whisper
model = whisper.load_model("small")
result = model.transcribe(
"interview.mp3",
language="en",
task="transcribe",
temperature=0,
beam_size=5,
fp16=False,
)
print(result["text"])Temperature and fallback
Temperature changes how strongly Whisper favors high-probability tokens. A temperature of 0 selects a highly predictable result. Higher values allow more variation and can help when the first decoding attempt is uncertain.
Whisper accepts a tuple of temperatures. It tries the values in order and can retry with a higher temperature when quality checks indicate a poor result:
result = model.transcribe(
"interview.mp3",
language="en",
temperature=(0.0, 0.2, 0.4, 0.6),
beam_size=5,
fp16=False,
)Use low temperatures for clean, formal recordings. Do not treat a higher temperature as a guaranteed correction. It may instead introduce substitutions or invented text.
Using an initial prompt
An initial_prompt supplies context at the beginning of decoding. For example, a technical interview may contain names and terms that are uncommon in general speech:
result = model.transcribe(
"interview.mp3",
language="en",
initial_prompt=(
"The speakers discuss Kubernetes, PostgreSQL, NumPy, "
"and the company name Acme Data."
),
temperature=0,
fp16=False,
)
print(result["text"])The prompt can improve spelling, but a wrong prompt can bias the output. Test it against a reference transcript rather than assuming it helped.
Choosing a decoding strategy
Start with explicit language and temperature=0. Add beam search when deterministic output needs improvement. Add an initial prompt only when the recording contains specialized vocabulary. Compare each change using the same evaluation set.
Exercise
A recording is a clear English lecture, but Whisper sometimes detects Spanish and misspells several library names. Which decoding changes should you try first?
Show answer
Set language="en" and provide a short initial_prompt containing the library names. Keep temperature=0 initially, then compare the result with and without the prompt.