_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
- Install
mlx-audiowithout optional extras:uv venv --python 3.12 .venv source .venv/bin/activate uv pip install "mlx-audio @ git+https://github.com/Blaizzy/mlx-audio@03a4d99e6cac5c13523c1027761ab33abfdcd30d" pytest - Run
test_config_and_loader_registration:Or run in Python:python -m pytest mlx_audio/vad/tests/test_nemotron_diarization.py -k test_config_and_loader_registration -v -o addopts=''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
mlx_audio/sts/models/__init__.pyeagerly imports.moshi, which importssentencepiece.sentencepieceis an optional dependency declared under[sts].- When
get_model_categorychecks candidate categories, it probescategory = "sts"viamodule_path = "mlx_audio.sts.models.nemotron_diarization". importlib.util.find_spec(module_path)loads parent packagemlx_audio.sts.models, triggering__init__.pyand raisingModuleNotFoundError: No module named 'sentencepiece'.- In
mlx_audio/utils.py: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 raisemissing_nameis"sentencepiece", which does not matchmodule_path("mlx_audio.sts.models.nemotron_diarization")._has_model_modulere-raises the error instead of treating the unresolvable module path as absent (False).
Suggested Fix
Either:
- Make
_has_model_modulereturnFalsewhen missing dependencies prevent loading candidate model packages:def _has_model_module(module_path: str) -> bool: try: return importlib.util.find_spec(module_path) is not None except (ModuleNotFoundError, ImportError): return False - Or use lazy / dynamic imports inside
mlx_audio/sts/models/__init__.pyso that optional extras are only imported when that specific model is requested.
Source: Blaizzy/mlx-audio