mlx-lm package provides a simple interface for loading and serving LLMs.
Installation
Install the MLX language model package:Basic Usage
Themlx-lm package provides a simple interface for text generation with MLX models.
See the Models page for all available MLX models, or browse MLX community models at mlx-community LFM2 models.
Generation Parameters
Control text generation behavior using parameters in thegenerate() function. Key parameters:
temperature(float, default 1.0): Controls randomness (0.0 = deterministic, higher = more random). Typical range: 0.1-2.0top_p(float, default 1.0): Nucleus sampling - limits to tokens with cumulative probability ≤ top_p. Typical range: 0.1-1.0top_k(int, default 50): Limits to top-k most probable tokens. Typical range: 1-100max_tokens(int): Maximum number of tokens to generaterepetition_penalty(float, default 1.0): Penalty for repeating tokens (>1.0 = discourage repetition). Typical range: 1.0-1.5
Streaming Generation
Stream responses withstream_generate():
Serving with mlx-lm
MLX can serve models through an OpenAI-compatible API. Start a server with:Using the Server
Once running, use the OpenAI Python client:Curl request example
Curl request example
Vision Models
LFM2-VL models support both text and image inputs for multimodal inference. Usemlx_vlm to load and generate with vision models:
Single Image Example
Single Image Example
Multiple Images Example
Multiple Images Example
LFM3 and d1 Models
The LFM3 models and the d1 decision models run with MLX-VLM 0.7.11 or later:
The LFM3 models run with
mlx_vlm.generate:
mlx_vlm.generate turns reasoning off unless you pass --enable-thinking. mlx-lm cannot load LFM3-1.7B yet. Support is pending in ml-explore/mlx-lm#2016.
The d1 models answer questions instead of generating text, and d1-2B and d1-omni-2B refuse mlx_vlm.generate and chat apps. Ask them with python -m mlx_vlm decide, which prints the answers as JSON:
value and probability. The d1 model cards show the Python API.
Speculative Decoding
Some LFM2.5 vision models have DSpark draft models that make generation faster without changing the output. Pass the drafter to MLX-VLM (v0.7.2 or newer) with--draft-model:
--temperature 0. See Speculative Decoding for which models have drafters.