sgl-project/sglang · error · RuntimeError

The MLX tensor bridge requires MLX >= 0.32.0

Error message

The MLX tensor bridge requires MLX >= 0.32.0

What it means

The MLX tensor bridge lazily imports mlx.core inside an lru_cached helper and converts ImportError into a clear RuntimeError pinning the minimum supported version. It fires on the first bridge call (torch_to_mlx, mlx_call, mlx_call_multi, mlx_to_torch) when MLX is not installed or is un-importable.

Source

Thrown at python/sglang/srt/utils/tensor_bridge.py:50


def _serialized_bridge(function: Callable[..., Any]) -> Callable[..., Any]:
    """Serialize one complete Torch/MLX crossing, including result export."""

    @wraps(function)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        with _BRIDGE_LOCK:
            return function(*args, **kwargs)

    return wrapper


@lru_cache(maxsize=1)
def _mlx_core():
    try:
        import mlx.core as mx
    except ImportError:
        raise RuntimeError("The MLX tensor bridge requires MLX >= 0.32.0") from None
    return mx


def _get_torch_device() -> torch.device:
    """Get the PyTorch device for Metal/MPS.

    Returns:
        torch.device for MPS if available, else CPU
    """
    if torch.backends.mps.is_available():
        return torch.device("mps")
    return torch.device("cpu")


def _torch_to_mlx(
    tensor: torch.Tensor,
    *,
    copy: bool,

View on GitHub (pinned to 0132848349)

Solutions

  1. pip install 'mlx>=0.32.0' (or mlx[cpu] variants as appropriate)
  2. Verify with python -c 'import mlx.core as mx; print(mx.__version__)'
  3. On non-Apple platforms, avoid the MLX bridge code paths entirely (use CPU/CUDA paths)

Example fix

# before: RuntimeError on first mlx_call
# after
pip install "mlx>=0.32.0"
python -c "import mlx.core; print(mlx.core.__version__)"
Defensive patterns

Strategy: try-catch

Validate before calling

try:
    import mlx.core as mx  # noqa
    HAS_MLX = True
except ImportError:
    HAS_MLX = False
assert HAS_MLX, "install mlx>=0.32.0 to use the MLX bridge"

Type guard

HAS_MLX = importlib.util.find_spec("mlx") is not None

Try / catch

try:
    out = mlx_call(fn, ts)
except RuntimeError as e:
    if "requires MLX" in str(e):
        raise SystemExit("pip install 'mlx>=0.32.0'") from e
    raise

Prevention

When it happens

Trigger: Calling any tensor_bridge function on a machine where `import mlx.core` fails — MLX not installed, installed but broken, or running on non-Apple hardware where MLX cannot import.

Common situations: Running macOS-only MLX code paths on Linux, a venv missing the mlx package, or an mlx version whose native extension fails to load.

Related errors


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