skip to content

Against Ollama 0.35's /v1/chat/completions, an agent sending tool_choice 'required' and n=3 gets one choice and prose replies — why, and what else differs silently?

level: seniorimportance: should knowfreq 11%

answer

  1. dropped, not rejected
  2. the unchecked boxes on the compat page
  3. sampling values filled in
  4. no field for context size
  5. stateless /v1/responses

basics

~10 s

Ollama 0.35's /v1/chat/completions does not support tool_choice, n, logit_bias or user and drops them without an error. It also sends temperature and top_p of 1.0 when omitted and has no context-size field.

solid answer

~50 s

In Ollama 0.35.0 the compatibility handler decodes the body into its own request type, and fields it does not declare — `tool_choice`, `n`, `logit_bias`, `user` — are discarded without a 400. So the agent gets one choice, and the model decides for itself whether to call a tool. Other gaps are just as quiet: when `temperature` or `top_p` is omitted the layer sends `1.0` for each, overriding the model's own defaults; the OpenAI request has no field for the context size, so a long history is trimmed from the oldest messages to fit the context the server allocated; and `/v1/responses` is stateless, with no `previous_response_id`. Image URLs, by contrast, fail loudly — base64 only. Fix it by validating tool use in the app, setting sampling explicitly, raising context with a Modelfile `PARAMETER num_ctx` or `OLLAMA_CONTEXT_LENGTH`, or moving to native `/api/chat`.

go deeper

for a junior

Remember that Ollama's /v1 routes honour only a listed subset of OpenAI fields, and that unsupported ones such as tool_choice and n are dropped without an error.

for a middle

Explain each silent gap: ignored fields, sampling filled in as 1.0, no context-size field, oldest messages trimmed, stateless /v1/responses, plus the few gaps that do error.

for a senior

Go from symptom to cause, then fix in the right layer: tool validation in the app, explicit sampling, a Modelfile or OLLAMA_CONTEXT_LENGTH for context, or a move to native /api/chat.

for a principal

Decide whether the team keeps the OpenAI SDK with a behavioural contract test per backend, or writes a native Ollama client and accepts the portability cost.

## Why nothing errors Ollama's `/v1/chat/completions` is a **translation layer**: it parses the OpenAI-shaped body into its own request type, converts that into a native chat request, and converts the answer back. In 0.35.0 the parse ignores any field the type does not declare. The compatibility page lists the request fields with checkboxes, and four are unchecked: **`tool_choice`, `logit_bias`, `user` and `n`**. Because they are dropped rather than rejected, a client written for the hosted API gets a `200` and a normal-looking response; only the behaviour changes. For the agent in the question that explains both symptoms: - **`n=3` gives one choice.** The local route generates a single completion, so `choices` has one element. - **`tool_choice: "required"` gives prose.** Without the field, the model is free to answer in text or to call a tool, exactly as if no preference had been sent. Tools themselves are supported, so a capable model often still calls them, which is why the failure looks intermittent rather than total. ## The other quiet differences | Behaviour | What a hosted-API client expects | Ollama 0.35.0 `/v1` | |---|---|---| | `tool_choice`, `logit_bias`, `user`, `n` | honoured | ignored, no error | | `temperature` / `top_p` omitted | the service's own default | filled in as `1.0` each, overriding the model's defaults | | context window | fixed by the hosted model | whatever the server allocated; no request field changes it | | history longer than that context | the caller's problem | oldest non-system messages trimmed to fit | | `previous_response_id` on `/v1/responses` | server-side thread | unsupported; resend the history | | image given as an `https://` URL | fetched | rejected; base64 data URLs only | | logprobs | available | marked unsupported on the compatibility page | Three rows deserve a closer look. 1. **Sampling defaults.** A model's Modelfile or metadata usually sets a temperature tuned for it, and native requests inherit those values unless `options` overrides them. The 0.35.0 compatibility layer, however, writes `temperature: 1.0` and `top_p: 1.0` into every request that omits them, and request values win over the model's. The same prompt can therefore be noticeably more random through `/v1` than through `ollama run`. Sending both explicitly removes the difference. 2. **Context size.** In the docs' words, the OpenAI API "does not have a way of setting the context size". The documented fix is a Modelfile with `PARAMETER num_ctx <size>` built with `ollama create`, then calling the new model name; a server-wide `OLLAMA_CONTEXT_LENGTH` also works. How large the default is and what raising it costs in memory belong to the offload material; the API point is only that `/v1` cannot ask. When a history does not fit, the chat path removes messages from the front, keeping system messages, until the prompt fits, so an agent quietly forgets its earliest turns. 3. **Statelessness.** `/v1/responses` (added in v0.13.3) supports `input`, `instructions`, tools and streaming, but not `previous_response_id` or `conversation`. Code that relies on the server remembering a thread must resend it. ## What does fail loudly Not every gap is silent. An image passed as an `http://` or `https://` URL returns an error asking for base64 data. A request carrying `tools` to a model whose metadata lacks tool support returns `400` with "does not support tools". An uninstalled model name returns `404`. These show up in logs and tests; the silent ones show up only as odd behaviour. ## Fixing the agent - **Do not rely on forcing tools.** Check whether the response contains `tool_calls`; if a tool was mandatory and none came back, re-prompt or fail explicitly. For a fixed output shape, the native `format` field with a JSON schema is the stronger lever. - **Loop instead of `n`.** Send several requests when several candidates are genuinely needed. - **Pin sampling.** Send `temperature` and `top_p` on every call. - **Size the context deliberately** with a Modelfile-built model or `OLLAMA_CONTEXT_LENGTH`, and track prompt length on the client. - **Move to `/api/chat`** when you need per-request `options` (including `num_ctx`), `think` levels or a `format` schema passed through unchanged; the native route exposes them directly. The general rule: against a local `/v1` server, the compatibility checklist is the contract. Assume anything unchecked is dropped, and write tests that assert on behaviour, not only on a `200`, because a status check will never notice.

  • How would a test suite catch these gaps before the agent ships?
    Assert on behaviour, not status: send `tool_choice` 'required' and check that `tool_calls` is present, send `n=2` and check the length of `choices`. Against Ollama's `/v1` those assertions fail while every call returns 200, which is exactly what a status-only test misses.

A travel plug adapter: the plug fits the socket and power flows, but the appliance's extra pins connect to nothing, so the features they control — here tool_choice and n — quietly do nothing instead of tripping a fuse.

  • Ollama returns 400 for any OpenAI field it does not support.
  • Setting n=3 on Ollama's /v1 route returns three choices.
  • Omitting temperature on /v1 keeps the Modelfile's temperature.
  • The context size can be raised per request on /v1/chat/completions.
  • Ollama's /v1/responses keeps the thread when given previous_response_id.

Assumes Ollama 0.35.0; the compatibility field list and the 1.0 sampling fill-in are read from that release's docs and source.