Skip to main content

Function Signature

Parameters

Whisper
required
The Whisper model instance returned by load_model().Must be a multilingual model (not .en variant) that has language tokens.
torch.Tensor
required
A tensor containing the Mel spectrogram(s).Shape:
  • (80, 3000) for single audio segment
  • (n_audio, 80, 3000) for batched segments
  • (n_audio_ctx, n_audio_state) for pre-encoded audio features
The function automatically handles both raw Mel spectrograms and pre-encoded features.
Tokenizer
default:"None"
Optional tokenizer instance. If not provided, automatically creates one based on the model.The tokenizer must have language tokens enabled (multilingual models only).

Returns

Tensor
Tensor of shape (n_audio,) containing the IDs of the most probable language tokens.If input is a single segment (2D tensor), returns a scalar tensor (0D).These are token IDs that appear after the start-of-transcript token.
List[dict]
List of dictionaries (length n_audio) containing the probability distribution over all languages.If input is a single segment, returns a single dict instead of a list.Each dictionary maps language codes to probabilities:
Probabilities sum to 1.0 across all ~100 supported languages.

Example

Notes

How It Works

  1. Encoder: Processes the Mel spectrogram to extract audio features
  2. Single token forward pass: Uses only the start-of-transcript (SOT) token
  3. Language token logits: Extracts logits for all language tokens
  4. Suppression: Sets all non-language tokens to -inf
  5. Argmax: Selects the most probable language token
  6. Softmax: Computes probability distribution across languages
This is performed outside the main decode loop to avoid interfering with KV-caching.

Supported Languages

Whisper multilingual models support ~100 languages. Some common ones:
Full list includes: English, Chinese, Spanish, French, German, Japanese, Korean, Portuguese, Russian, Arabic, Hindi, and many more.

Model Requirements

This function only works with multilingual models: ✅ Compatible:
  • tiny, base, small, medium, large, large-v2, large-v3, turbo
❌ Not compatible:
  • tiny.en, base.en, small.en, medium.en
Attempting to use English-only models raises:

When to Use

Use detect_language() when:
  • You need to know the language before transcription
  • Processing multilingual audio collections
  • Implementing language routing logic
  • Filtering audio by language
  • Verifying language before translation
For transcription, you typically don’t need to call this directly:

Pre-encoded Features

You can pass pre-encoded audio features to skip the encoder:

Confidence Thresholds

Interpret confidence scores:

Batch Processing

Process multiple audio segments efficiently:

Language Code Mapping

Integration with Transcription

Performance

  • Very fast: Only one forward pass through encoder and single decoder token
  • Encoder caching: Reuse encoded features for both detection and decoding
  • Batch-friendly: Process multiple audios in parallel
  • No KV cache: Simpler than full decoding
Typical times on GPU:
  • Single segment: ~10-20ms
  • Batch of 10 segments: ~30-50ms