skip to content

In Ollama, what changes when an indexer moves from /api/embeddings to /api/embed, and what does /api/embed do with over-long inputs?

level: middleimportance: should knowfreq 8%

answer

  1. superseded, still answering
  2. input takes a string or a list
  3. embeddings: one vector per item
  4. unit-length vectors
  5. truncate defaults to true

basics

~20 s

/api/embed takes input as a string or list and returns one L2-normalized vector per item under embeddings; /api/embeddings took one prompt and returned a raw vector. Over-long inputs are silently cut unless truncate is false.

solid answer

~50 s

In Ollama 0.35.0 `/api/embeddings` is superseded: it takes a single `prompt` and returns one `embedding`, as the model produced it. `/api/embed` takes `input` as a string or a list, so a batch is one request, and returns `embeddings`, a list of vectors in input order, each **L2-normalized** to unit length; `dimensions` keeps the first N values and re-normalizes them. Long inputs are governed by `truncate`, which defaults to `true`: the end of each input is cut to fit the model's context length with no warning. With `"truncate": false` an over-long input fails with an error instead, which is what an indexer that must not lose text wants. `/v1/embeddings` runs through the same handler, so it inherits the normalization and the silent truncation, with no way to opt out. Because the two native routes differ in normalization, don't mix their vectors in one index.

code

bash · 5 lines
bash
curl http://localhost:11434/api/embed -d '{
  "model": "embeddinggemma",
  "input": ["first chunk of the document", "second chunk of the document"],
  "truncate": false
}'

go deeper

for a junior

Recall that /api/embed is the current route: input takes a string or a list, and the response holds one vector per item under embeddings.

for a middle

Explain what changed from /api/embeddings — batching, response shape, unit-length vectors, dimensions — and that truncate defaults to true.

for a senior

Treat silent truncation and mixed normalization as index-quality bugs: send truncate false, re-embed when switching routes, and check prompt_eval_count against expectations.

for a principal

Decide whether embedding goes through Ollama's native route or a portable /v1 client, weighing truncation control and re-embedding cost against backend independence.

## Two routes, one superseded Ollama has served embeddings from two native routes. The older **`POST /api/embeddings`** takes `model` and a single `prompt` and answers `{"embedding": [...]}`. The API reference now marks it "superseded by `/api/embed`": it still answers, but new code should not use it. **`POST /api/embed`** is the current route, and the OpenAI-compatible **`POST /v1/embeddings`** is translated into the same handler. | | `/api/embeddings` | `/api/embed` | |---|---|---| | input field | `prompt` (one string) | `input` (a string or a list of strings) | | response field | `embedding` (one vector) | `embeddings` (list of vectors, input order) | | normalization | none: the model's raw output | L2-normalized to unit length | | over-long input | no control | `truncate` (default `true`) | | fewer dimensions | no | `dimensions` | | token count in response | no | `prompt_eval_count` | Both accept `options` and `keep_alive`. ## What the migration changes in client code 1. **Batching.** Send a list as `input` and get back the same number of vectors, in the same order. An indexer that made one call per chunk can send many chunks per request. 2. **Response parsing.** Read `embeddings[i]`, a list of lists, not `embedding`. Even a single string comes back wrapped in the outer list. 3. **Vector scale.** Every vector from `/api/embed` has length 1. For cosine similarity that changes nothing; for dot-product or Euclidean scoring it does. The old route returned the model's raw vectors, so vectors for the same text from the two routes can differ in magnitude, and an index built through one and queried through the other compares two scales. Re-embed the corpus when you switch, or normalize the stored vectors yourself. Why an index and its queries need one model and one procedure is the retrieval material's point; the Ollama-specific fact is that these two routes are two procedures. 4. **Shorter vectors.** `dimensions` keeps the first N components and re-normalizes them. That only makes sense for models trained so that a prefix of the vector stays meaningful; Ollama does not check this for you. 5. **Failure is per request, not per item.** If any input in a batch fails, for example an over-long chunk sent with `truncate` set to `false`, the whole request returns an error and no vectors at all. The indexer has to find the offending chunk, split it and resend the batch, so batch sizes are a trade between fewer round trips and cheaper retries. ## Truncation: the silent default An embedding model has a fixed context length, and text beyond it cannot be encoded. `/api/embed` handles this with **`truncate`**: - **`true` (the default):** the end of each over-long input is cut so it fits, and the request succeeds. Nothing in the response says which inputs were shortened. - **`false`:** an input longer than the context makes the request fail with an error ("the input length exceeds the context length"). For a retrieval index the default is a quiet data-quality bug. A long chunk sent to a model with a shorter window is embedded from its opening only, and text past the cut can never be found by search. Two habits prevent it: - Send `"truncate": false` from the indexer so over-long chunks surface as errors you can split and retry. - Size chunks below the model's context length in the first place, and compare `prompt_eval_count` with what you expected to send. Whether a model encodes long text well is a property of the model, not of the route; the route only decides whether you are told. ## The compatibility route `/v1/embeddings` accepts `input` as a string or an array of strings (not token arrays), plus `dimensions` and `encoding_format` (float arrays, or `base64` to shrink large responses); `user` is unsupported. Because it ends in the same handler as `/api/embed`, its vectors are normalized and its over-long inputs are truncated. The OpenAI request has no `truncate` field, so a client on `/v1` cannot ask for the error instead. An indexer that must never lose text should call `/api/embed` directly with `truncate` set to `false`, while short query-time calls can stay on `/v1/embeddings`, since both routes return the same normalized vectors.

  • An indexer must never embed a partial chunk — how do you make Ollama tell you?
    Send `"truncate": false` to `/api/embed`. An input longer than the model's context then fails the request with an error instead of being cut, so the indexer can split that chunk and retry. `/v1/embeddings` has no such field and always truncates.
  • Search quality dropped after half the corpus was re-embedded through /api/embed while older vectors came from /api/embeddings — why?
    `/api/embed` returns unit-length vectors; `/api/embeddings` returned the model's raw output. Under dot-product scoring the two halves sit on different scales, so ranking favours whichever half has larger magnitudes. Re-embed everything through one route, or normalize the old vectors.

  • /api/embeddings and /api/embed return identical vectors under different field names.
  • By default /api/embed rejects an input longer than the context.
  • /api/embed needs one request per input string.
  • /v1/embeddings lets a client turn truncation off.
  • dimensions only changes the JSON, never the vector values.

Assumes Ollama 0.35.0; the L2 normalization, the dimensions re-normalization and truncate's default are read from that release's docs and server source.