Framework guide · rapid-mlx 0.12.14 · ← Back to README

OpenAI SDK — the three-line switch

rapid-mlx serves a drop-in OpenAI-compatible API on localhost. If your script already uses the OpenAI Python or JS SDK, moving it onto a local model is a constructor change — base_url and api_key — and nothing else. This is the single most common way rapid-mlx is called in the wild.

Wire: /v1/chat/completions, /v1/completions, /v1/models, /v1/embeddings — see the API surface · Setup: pass base_url + any non-empty api_key · Works unchanged: streaming, tool calling, JSON mode / structured output, vision inputs on VLM aliases.

Serve a model

$ rapid-mlx serve qwen3.5-9b-4bit
⚡ serving on http://localhost:8000/v1

Python

from openai import OpenAI

client = OpenAI(
    base_url="http://localhost:8000/v1",  # ← the switch
    api_key="not-needed",                  # any non-empty string
)

r = client.chat.completions.create(
    model="default",  # resolves to whatever the server booted
    messages=[{"role": "user", "content": "Explain MLX in one line."}],
)
print(r.choices[0].message.content)

Zero-code: env vars

The SDK reads OPENAI_BASE_URL and OPENAI_API_KEY from the environment, so an existing tool can be pointed local without touching its source at all:

$ export OPENAI_BASE_URL=http://localhost:8000/v1
$ export OPENAI_API_KEY=not-needed
$ python your_existing_script.py

JavaScript / TypeScript

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "http://localhost:8000/v1",
  apiKey: "not-needed",
});

const r = await client.chat.completions.create({
  model: "default",
  messages: [{ role: "user", content: "Explain MLX in one line." }],
});
console.log(r.choices[0].message.content);

What carries over

Gotchas

See also