Chat completions
POST /v1/chat/completions — the OpenAI chat API on local models, with streaming, reasoning, tools and structured output, plus the fields Midium adds.
For developers
POST /v1/chat/completions is OpenAI's chat completions API, served by the models on this Mac. Point any OpenAI SDK or client at http://127.0.0.1:9100/v1, send your Midium key, and use a Midium model's name as model. It streams, calls tools, returns structured JSON and gives you the model's reasoning separately from its answer.
Request#
curl http://127.0.0.1:9100/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL",
"messages": [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Why is the sky blue? One sentence."}
],
"reasoning_effort": "low"
}'
Fields that aren't in the OpenAI SDK's types (top_k, chat_template_kwargs, strict_max_tokens, …) go in extra_body in Python, or straight into the request object in JavaScript.
Parameters#
| Name | Type | Description |
|---|---|---|
model | string | Required. A model's name or nickname. Midium: matched without regard to case or extra spaces. A nickname such as Qwen3.8 27B (8-bit) picks that exact build; a plain name picks the default build. |
messages | array | Required. The conversation: system, user, assistant and tool messages. User content can be a string or a list of parts, including images (see Images & vision). |
stream | boolean | Send the reply as server-sent events. Default false. |
temperature | number | Sampling temperature. Midium: when you leave it out, the model's recommended value is used (one for thinking turns, one for plain turns), not 1.0. |
top_p | number | Nucleus sampling. Midium: filled from the model's recommendation when omitted. |
top_k | integer | Midium: keep only the k most likely tokens (vLLM-style field). Filled from the model's recommendation when omitted. |
max_tokens | integer | Cap on the reply's length. Midium: a cap below 8192 is raised to 8192, because small caps cut reasoning and tool-calling models off mid-answer. Omitted: the model's default, else 32,768. Always limited to the context the prompt leaves free. |
strict_max_tokens | boolean | Midium: true honors max_tokens exactly, with no 8192 floor. |
stop | string or array | Stop sequences. |
presence_penalty | number | Default 0. |
frequency_penalty | number | Default 0. |
reasoning_effort | string | How hard a reasoning model thinks. Midium: none turns thinking off; low, medium, high (and minimal, xhigh, max) turn it on and map to the model's own levels where it has them. |
chat_template_kwargs | object | Midium: passed to the model's chat template, for example {"enable_thinking": false}. Wins over reasoning_effort. |
tools | array | Function tools the model may call, plus Midium's {"type": "web_search"}. See Tool calling. |
tool_choice | string or object | auto, none, required, or a specific function. See Tool calling. |
response_format | object | {"type": "json_object"} or {"type": "json_schema", ...}. See Structured output. |
n | integer | Accepted, but one choice is always returned. |
user | string | Accepted and ignored. |
speculative_decoding | boolean | Midium: false skips the model's speculative-decoding helper for this request, if it has one turned on. |
ignore_eos | boolean | Midium: for benchmarks. Don't stop at the end-of-text token, so the reply is exactly the cap long. Needs max_tokens (or max_completion_tokens), which then is honored exactly. |
max_completion_tokens | integer | Midium: read only together with ignore_eos, where it takes the place of max_tokens. Otherwise ignored. |
midium_metrics | boolean | Midium: with stream: true, the last chunk carries midium_metrics with the engine's measured decode_tps, prompt_tokens and completion_tokens (what Settings → API uses). |
Response#
{
"id": "chatcmpl-6f1c2a4e-…",
"object": "chat.completion",
"created": 1763030400,
"model": "YOUR_MODEL",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Air scatters short blue wavelengths of sunlight far more than red ones.",
"reasoning": "The user wants one sentence on Rayleigh scattering…",
"reasoning_content": "The user wants one sentence on Rayleigh scattering…"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 31,
"completion_tokens": 58,
"total_tokens": 89
}
}
modelechoes the name you sent.finish_reasonisstop,length(the reply hit its cap) ortool_calls.reasoningandreasoning_contenthold the model's thinking, the same text twice (most OpenAI-compatible clients readreasoning_content). They're absent when the model didn't think.usage.prompt_tokens_details.cached_tokensappears when part of the prompt was served from the model's cache.
Streaming#
With "stream": true the reply comes as chat.completion.chunk events:
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","reasoning_content":"The user wants"},"finish_reason":null}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Air scatters"},"finish_reason":null}]}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":31,"completion_tokens":58,"total_tokens":89}}
data: {"id":"chatcmpl-…","object":"chat.completion.chunk","choices":[],"usage":{"prompt_tokens":31,"completion_tokens":58,"total_tokens":89}}
data: [DONE]
- Thinking streams as
delta.reasoning_content, the answer asdelta.content. - Tool calls stream as
delta.tool_calls: the call'sidand name first, then its arguments. - Usage is always sent — on the finishing chunk and on a final chunk with empty
choices. You don't needstream_options. Some chunks along the way also carry a runningusage.completion_tokens. - If generation fails partway, a chunk with an
errorobject and emptychoicesis sent, then[DONE].
Thinking#
Reasoning models (Qwen 3.x, gpt-oss, and others) think before they answer. The thinking is never mixed into content.
- A request that says nothing about thinking gets the model's own default: Qwen 3.x thinks, Gemma 4 doesn't.
reasoning_effort: "none"orchat_template_kwargs: {"enable_thinking": false}turns it off. Any otherreasoning_effortturns it on.GET /v1/modelsmarks models that can think withcapabilities.reasoning: true.
Notes#
- Loading. A model that isn't in memory loads on the first request, so that request takes longer. Keep a model loaded from Settings → Models.
- Only chat models. A request naming an embedding, reranker, image or video model fails with
400 model_not_chatand says which endpoint to use instead. - Audio models. An installed transcription model can be called here with an
input_audiopart, without streaming or tools. Audio transcription is the better fit. - Agents. Hermes Agent, OpenClaw, OpenBot and other OpenAI-compatible agents use this endpoint. See Connectors.
Errors#
| Status | code | When |
|---|---|---|
404 | model_not_found | No model by that name. The message lists the models you can use. |
400 | model_not_chat | The model can't chat (embedding, reranker, image or video model). |
400 | invalid_tool_choice | tool_choice names a tool that isn't in tools. |
400 | invalid_tool_call | The model's tool-call arguments weren't valid JSON. |
400 | unsupported_streaming, unsupported_tools, invalid_audio | Streaming, tools or a bad audio part sent to a transcription model. |
422 | — | The body doesn't match the schema (a missing model, a wrong type). |
Errors use OpenAI's envelope: {"error": {"message", "type", "code"}}. See Errors & limits.
Something here wrong or out of date? Tell us.
