An OpenAI-SDK app calls an Ollama 0.35 model baked with SYSTEM, temperature 0.2 and num_ctx 16384 via /v1/chat/completions — which settings apply?
answer
- request beats model beats server
- /v1 has no context-size field
- client system message comes first
- omitted temperature is not left alone
- 1.0 filled in on /v1
basics
~20 sOnly num_ctx is safe: /v1 cannot send a context size, so the Modelfile's 16384 applies. SYSTEM applies only when the client sends no system message first, and /v1 fills an omitted temperature with 1.0, overriding the baked 0.2.
solid answer
~40 sOllama resolves options in layers: the server default (`OLLAMA_CONTEXT_LENGTH`, otherwise a VRAM-based context), then the model's `PARAMETER` values, then whatever the request sends. Through `/v1/chat/completions` that gives three different outcomes. `num_ctx 16384` applies, because the OpenAI request has no context-size field — which is exactly why the docs tell you to bake it into a Modelfile. `SYSTEM` is prepended only when the request's first message is not a system message; an SDK app that sends its own system prompt replaces the baked one, nothing is merged. And `temperature 0.2` is lost: when the client omits `temperature`, the compatibility layer fills in `1.0` (and `1.0` for `top_p`), and request values beat model values. Either send `temperature` from the client or call native `/api/chat`, which leaves unset options to the model.
code
python · 13 linesfrom openai import OpenAI
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
resp = client.chat.completions.create(
model="support-bot", # built with num_ctx 16384 and SYSTEM
temperature=0.2, # send it: an omitted temperature becomes 1.0
messages=[
# no system message here, so the Modelfile's SYSTEM is used
{"role": "user", "content": "How do I reopen a ticket?"},
],
)
print(resp.choices[0].message.content)go deeper
Recall the order: server default, then the model's PARAMETER values, then the request, and that a client's own system message replaces the Modelfile's SYSTEM.
Explain why num_ctx belongs in the Modelfile for /v1 clients, and why an omitted temperature on /v1 still overrides the baked one.
Diagnose an app that behaves differently from ollama run by listing what its client sends, and fix ownership of the system prompt and sampling values in one place.
Choose where each setting lives for a team: model-level for what clients cannot send, client-level for what the compatibility route always sends, and one owner for the system prompt.
## Three layers of settings Every Ollama request runs with an **options** set built in a fixed order, each layer overriding the one before: 1. **Server defaults.** The context size comes from `OLLAMA_CONTEXT_LENGTH` when it is set; otherwise Ollama 0.35 picks it from total VRAM (4k under 24 GiB, 32k from 24 GiB, 256k from 48 GiB). Sampling options have built-in defaults. 2. **The model's Modelfile values.** Every `PARAMETER` baked in by `ollama create` replaces the server default for that key. 3. **The request.** Any option the request carries replaces the model's value for that request only. The system prompt and seeded turns follow their own rules on the chat routes: - The model's **MESSAGE** turns are placed in front of the request's messages. - The model's **SYSTEM** prompt is added only when the request's first message is **not** a system message. If the client sends one, the client's system prompt is used and the baked one is skipped — Ollama does not concatenate them. That ordering explains the whole scenario once you add what the OpenAI-compatible route puts into the request. ## What `/v1/chat/completions` puts into the request The OpenAI API has no field for a model's context size, so `/v1/chat/completions` can never send `num_ctx`. The compatibility docs answer that gap directly: create a Modelfile with `PARAMETER num_ctx <size>` and call the new model name. Sampling is different. The compatibility layer converts OpenAI fields into Ollama options, and for two of them it does not leave an omitted field empty: - `temperature` omitted → the request carries `temperature` = `1.0`. - `top_p` omitted → the request carries `top_p` = `1.0`. Since request options beat model options, a Modelfile's `temperature` and `top_p` never apply to `/v1/chat/completions` calls; the client's value applies when it sends one, and `1.0` applies when it does not. Fields the client does send — `stop`, `max_tokens` (mapped to `num_predict`), `seed` — override the model's values the same way. What those sampling options do to the output is a decoding subject; the point here is which value wins. ## The scenario, setting by setting | Modelfile setting | Through `/v1/chat/completions` | Through native `/api/chat` with no `options` | |---|---|---| | `PARAMETER num_ctx 16384` | applies — /v1 cannot send a context size | applies | | `SYSTEM ...` | applies only if the app sends no leading system message | applies only if the request sends no leading system message | | `PARAMETER temperature 0.2` | overridden: the app's value, or `1.0` when omitted | applies | | `PARAMETER stop ...` | applies unless the app sends `stop` | applies | The symptom of the temperature row is subtle: the assistant is noticeably more varied than the model tested with `ollama run`, and nobody changed the Modelfile. The symptom of the system row is a persona that never appears, because the SDK app's own boilerplate system prompt took its place. Both are silent: no warning is logged, the request succeeds, and the only evidence is the output. ## What to do about it - **Keep `num_ctx` in the Modelfile.** For `/v1` clients it is the documented way to set the context; set it deliberately, because a larger context costs memory. - **Send sampling values from the client** when the app uses `/v1`, or move the app to `/api/chat`, where unset options fall through to the Modelfile. - **Pick one owner for the system prompt.** Either the app sends it and the Modelfile has none, or the app sends no system message and the Modelfile's applies. Two sources guarantee confusion. - **Verify against the running model:** `ollama ps` shows the context the model was loaded with, and `ollama show --modelfile` shows what was baked in. ## Why it is asked It is the classic surprise of the first app that reuses an OpenAI client against a local model. A strong answer states the precedence — request over model over server — and then the two details that make it bite: `/v1` cannot carry a context size, so the Modelfile owns it, while `/v1` always carries a temperature, so the Modelfile does not.
- Which wins in Ollama 0.35: `OLLAMA_CONTEXT_LENGTH=8192` on the server or `PARAMETER num_ctx 16384` in the model?The Modelfile value. The server variable only replaces the VRAM-based default; a model's `num_ctx` parameter overrides that default, and a request's own `num_ctx` in native `options` would override the model in turn. `ollama ps` shows the context the model was actually loaded with.
- How does a native `/api/chat` client keep the model's temperature but raise the context for one call?Send only the option it wants to change: `"options": {"num_ctx": 32768}`. Native routes leave unset options to the model, so the baked `temperature` still applies while that request's context is replaced. A different context size can force the model to reload, so mixing sizes across calls has a cost.
- Baked Modelfile parameters always win over the request
- Omitted temperature on /v1 falls back to the Modelfile's value
- Ollama merges the client's system prompt with the Modelfile's SYSTEM
- /v1/chat/completions can set num_ctx through a request field
- OLLAMA_CONTEXT_LENGTH overrides a model's num_ctx parameter
Assumes Ollama 0.35.0: the VRAM-based default context and the compatibility layer's 1.0 fill-in for omitted temperature and top_p are as that release's source defines them.