huggingface/transformers · error · ValueError

mel_scale should be one of "htk", "slaney" or "kaldi".

Error message

mel_scale should be one of "htk", "slaney" or "kaldi".

What it means

Thrown by `hertz_to_mel` when the `mel_scale` argument is not one of the three supported scales: "htk", "slaney", or "kaldi". The function converts frequencies in Hz to mel units and the conversion formula differs per scale, so an unknown string has no defined behavior and is rejected before any math runs.

Source

Thrown at src/transformers/audio_utils.py:463

    return make_list_of_audio(audio)


def hertz_to_mel(freq: float | np.ndarray, mel_scale: str = "htk") -> float | np.ndarray:
    """
    Convert frequency from hertz to mels.

    Args:
        freq (`float` or `np.ndarray`):
            The frequency, or multiple frequencies, in hertz (Hz).
        mel_scale (`str`, *optional*, defaults to `"htk"`):
            The mel frequency scale to use, `"htk"`, `"kaldi"` or `"slaney"`.

    Returns:
        `float` or `np.ndarray`: The frequencies on the mel scale.
    """

    if mel_scale not in ["slaney", "htk", "kaldi"]:
        raise ValueError('mel_scale should be one of "htk", "slaney" or "kaldi".')

    if mel_scale == "htk":
        return 2595.0 * np.log10(1.0 + (freq / 700.0))
    elif mel_scale == "kaldi":
        return 1127.0 * np.log(1.0 + (freq / 700.0))

    min_log_hertz = 1000.0
    min_log_mel = 15.0
    logstep = 27.0 / np.log(6.4)
    mels = 3.0 * freq / 200.0

    if isinstance(freq, np.ndarray):
        log_region = freq >= min_log_hertz
        mels[log_region] = min_log_mel + np.log(freq[log_region] / min_log_hertz) * logstep
    elif freq >= min_log_hertz:
        mels = min_log_mel + np.log(freq / min_log_hertz) * logstep

    return mels

View on GitHub (pinned to a597f97485)

Solutions

  1. Set mel_scale to exactly one of "htk", "slaney", or "kaldi" (lowercase)
  2. If the value comes from a config/JSON, check for typos or casing issues in that file
  3. If you intended torchaudio/librosa-style 'slaney' behavior, use mel_scale="slaney"

Example fix

// before
mels = hertz_to_mel(440.0, mel_scale="HTK")  # ValueError

// after
mels = hertz_to_mel(440.0, mel_scale="htk")
Defensive patterns

Strategy: validation

Validate before calling

VALID_MEL_SCALES = {"htk", "slaney", "kaldi"}
assert mel_scale in VALID_MEL_SCALES, f"mel_scale must be one of {sorted(VALID_MEL_SCALES)}, got {mel_scale!r}"

Type guard

def is_valid_mel_scale(s: str) -> bool:
    return isinstance(s, str) and s in {"htk", "slaney", "kaldi"}

Try / catch

try:
    mels = hertz_to_mel(freq, mel_scale=mel_scale)
except ValueError as e:
    if "mel_scale" in str(e):
        mel_scale = "htk"  # or log and re-raise
        mels = hertz_to_mel(freq, mel_scale=mel_scale)
    else:
        raise

Prevention

When it happens

Trigger: Calling `hertz_to_mel(freq, mel_scale=...)` with a typo ("htkk", "Slaney" with capital letter), an unsupported scale ("librosa", "mfcc"), or a non-string value. Indirectly hit through `mel_filter_bank(..., mel_scale=...)` which forwards the argument.

Common situations: Copy-pasting mel_scale from another library's config (torchaudio, librosa use different vocabularies); case-sensitivity mistakes; custom feature-extractor configs overriding `mel_scale` with a wrong value.

Related errors


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/d223093b02b6fb3b. Report an issue: GitHub.