sgl-project/sglang · error · RuntimeError

torchaudio is required for audio inputs; install torchaudio

Error message

torchaudio is required for audio inputs; install torchaudio

What it means

MiMo audio preprocessing needs torchaudio (and its MelSpectrogram transform) to compute mel spectrograms from audio. Neither is importable in the current environment, so any audio input path fails at dependency check.

Source

Thrown at python/sglang/srt/multimodal/processors/mimo_audio.py:148

            f_max=audio_fmax,
            n_mels=audio_n_mels,
            power=1.0,
            center=True,
        )
        self._mel_spectrogram = None
        self._resamplers: OrderedDict[int, torchaudio.transforms.Resample] = (
            OrderedDict()
        )
        self._resamplers_max = max_resamplers

    @property
    def audio_token_per_second(self) -> float:
        return self.audio_input_id_per_second / self.audio_group_size

    @staticmethod
    def _ensure_audio_dependencies() -> None:
        if torchaudio is None or MelSpectrogram is None:
            raise RuntimeError(
                "torchaudio is required for audio inputs; install torchaudio"
            )

    @property
    def mel_spectrogram(self):
        self._ensure_audio_dependencies()
        if self._mel_spectrogram is None:
            self._mel_spectrogram = MelSpectrogram(**self.mel_spectrogram_kwargs)
        return self._mel_spectrogram

    def compute_audio_token_len(self, mel_len: int) -> int:
        n = mel_len + 3 - self.audio_kernel_size
        n = (n + 2 - self.audio_kernel_size) // self.audio_stride_size + 1
        n = n // self.audio_avg_pooler + int(n % self.audio_avg_pooler != 0)
        return math.ceil(n / self.audio_group_size)

    def preprocess_audio(self, audio):
        """Load audio source → log-mel spectrogram + token length.

View on GitHub (pinned to 0132848349)

Solutions

  1. pip install torchaudio matching your torch version (e.g. torchaudio==<torch version>)
  2. If import fails despite installation, align torchaudio with the installed torch/torchvision/CUDA versions
  3. Use an image/deployment that includes audio dependencies when serving audio-capable models

Example fix

# before: RuntimeError torchaudio is required
# after
pip install torchaudio==2.5.1  # match your torch==2.5.1
Defensive patterns

Strategy: fallback

Validate before calling

try:
    import torchaudio  # noqa
    AVAILABLE = True
except ImportError:
    AVAILABLE = False
if not AVAILABLE: raise SystemExit("install torchaudio")

Try / catch

try: run audio request except RuntimeError as e: if 'torchaudio' in str(e): prompt to install / route to text-only

Prevention

When it happens

Trigger: Calling mel_spectrogram or preprocess_audio on the MiMo audio pipeline when torchaudio was not installed or failed to import (e.g. version mismatch with torch).

Common situations: Installing sglang without the audio extras; a torch/torchaudio version mismatch causing a silent import failure recorded as None; slim Docker images omitting torchaudio.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/cbbb6c44448cc192. Report an issue: GitHub.