{"record":{"id":"785b3421bc791574","repo":"sgl-project/sglang","slug":"the-mlx-tensor-bridge-requires-mlx-0-32-0","errorCode":null,"errorMessage":"The MLX tensor bridge requires MLX >= 0.32.0","messagePattern":"The MLX tensor bridge requires MLX >= 0\\.32\\.0","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/utils/tensor_bridge.py","lineNumber":50,"sourceCode":"\n\ndef _serialized_bridge(function: Callable[..., Any]) -> Callable[..., Any]:\n    \"\"\"Serialize one complete Torch/MLX crossing, including result export.\"\"\"\n\n    @wraps(function)\n    def wrapper(*args: Any, **kwargs: Any) -> Any:\n        with _BRIDGE_LOCK:\n            return function(*args, **kwargs)\n\n    return wrapper\n\n\n@lru_cache(maxsize=1)\ndef _mlx_core():\n    try:\n        import mlx.core as mx\n    except ImportError:\n        raise RuntimeError(\"The MLX tensor bridge requires MLX >= 0.32.0\") from None\n    return mx\n\n\ndef _get_torch_device() -> torch.device:\n    \"\"\"Get the PyTorch device for Metal/MPS.\n\n    Returns:\n        torch.device for MPS if available, else CPU\n    \"\"\"\n    if torch.backends.mps.is_available():\n        return torch.device(\"mps\")\n    return torch.device(\"cpu\")\n\n\ndef _torch_to_mlx(\n    tensor: torch.Tensor,\n    *,\n    copy: bool,","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/utils/tensor_bridge.py#L32-L68","documentation":"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.","triggerScenarios":"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.","commonSituations":"Running macOS-only MLX code paths on Linux, a venv missing the mlx package, or an mlx version whose native extension fails to load.","solutions":["pip install 'mlx>=0.32.0' (or mlx[cpu] variants as appropriate)","Verify with python -c 'import mlx.core as mx; print(mx.__version__)'","On non-Apple platforms, avoid the MLX bridge code paths entirely (use CPU/CUDA paths)"],"exampleFix":"# before: RuntimeError on first mlx_call\n# after\npip install \"mlx>=0.32.0\"\npython -c \"import mlx.core; print(mlx.core.__version__)\"","handlingStrategy":"try-catch","validationCode":"try:\n    import mlx.core as mx  # noqa\n    HAS_MLX = True\nexcept ImportError:\n    HAS_MLX = False\nassert HAS_MLX, \"install mlx>=0.32.0 to use the MLX bridge\"","typeGuard":"HAS_MLX = importlib.util.find_spec(\"mlx\") is not None","tryCatchPattern":"try:\n    out = mlx_call(fn, ts)\nexcept RuntimeError as e:\n    if \"requires MLX\" in str(e):\n        raise SystemExit(\"pip install 'mlx>=0.32.0'\") from e\n    raise","preventionTips":["Gate MLX code paths behind an import check","Pin mlx>=0.32.0 in project requirements on macOS"],"tags":["sglang","mlx","dependency","import-error","macos"],"backgroundTag":"missing-dependency","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}