#972·mlx-audio

_has_model_module raises ModuleNotFoundError when probing categories with uninstalled optional dependencies (e.g. sentencepiece in sts)

Author: 0x4nur4gCreated Sep 24, 2026Updated Sep 24, 2026

Description

When mlx-audio is installed without optional extras (e.g. standard pip install mlx-audio without [sts] or [all]), calling get_model_category crashes with ModuleNotFoundError: No module named 'sentencepiece'. This causes test_config_and_loader_registration in mlx_audio/vad/tests/test_nemotron_diarization.py to fail.

Steps to Reproduce

  1. Install mlx-audio without optional extras:
    bash
    uv venv --python 3.12 .venv
    source .venv/bin/activate
    uv pip install "mlx-audio @ git+https://github.com/Blaizzy/mlx-audio@03a4d99e6cac5c13523c1027761ab33abfdcd30d" pytest
  2. Run test_config_and_loader_registration:
    bash
    python -m pytest mlx_audio/vad/tests/test_nemotron_diarization.py -k test_config_and_loader_registration -v -o addopts=''
    Or run in Python:
    python
    from mlx_audio.utils import get_model_category
    get_model_category("nemotron_diarization", [])

Error Output

    def test_config_and_loader_registration(tmp_path):
        from mlx_audio.utils import get_model_category
        from mlx_audio.vad import load
    
        cfg = tiny_config()
        assert ModelConfig.from_dict(asdict(cfg)) == cfg
>       assert get_model_category("nemotron_diarization", []) == "vad"

mlx_audio/vad/tests/test_nemotron_diarization.py:141: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
mlx_audio/utils.py:781: in get_model_category
    if _has_model_module(module_path):
mlx_audio/utils.py:726: in _has_model_module
    return importlib.util.find_spec(module_path) is not None
mlx_audio/sts/models/__init__.py:20: in <module>
    from .moshi import MoshiConfig, MoshiSTSModel
mlx_audio/sts/models/moshi/__init__.py:1: in <module>
    from .moshi import MoshiConfig, MoshiSTSModel
mlx_audio/sts/models/moshi/moshi.py:7: in <module>
    import sentencepiece
E   ModuleNotFoundError: No module named 'sentencepiece'

Root Cause

  1. mlx_audio/sts/models/__init__.py eagerly imports .moshi, which imports sentencepiece. sentencepiece is an optional dependency declared under [sts].
  2. When get_model_category checks candidate categories, it probes category = "sts" via module_path = "mlx_audio.sts.models.nemotron_diarization".
  3. importlib.util.find_spec(module_path) loads parent package mlx_audio.sts.models, triggering __init__.py and raising ModuleNotFoundError: No module named 'sentencepiece'.
  4. In mlx_audio/utils.py:
    python
    def _has_model_module(module_path: str) -> bool:
        try:
            return importlib.util.find_spec(module_path) is not None
        except ModuleNotFoundError as exc:
            missing_name = exc.name or ""
            if module_path == missing_name or module_path.startswith(f"{missing_name}."):
                return False
            raise
    missing_name is "sentencepiece", which does not match module_path ("mlx_audio.sts.models.nemotron_diarization"). _has_model_module re-raises the error instead of treating the unresolvable module path as absent (False).

Suggested Fix

Either:

  1. Make _has_model_module return False when missing dependencies prevent loading candidate model packages:
    python
    def _has_model_module(module_path: str) -> bool:
        try:
            return importlib.util.find_spec(module_path) is not None
        except (ModuleNotFoundError, ImportError):
            return False
  2. Or use lazy / dynamic imports inside mlx_audio/sts/models/__init__.py so that optional extras are only imported when that specific model is requested.