{"error":"the input length exceeds the context length"} — Ollama's
/api/embed answers this with HTTP 400 when the text is longer than the model's
context window and the request says "truncate":false. Split the text into
smaller chunks and send them as an array; raising num_ctx to 512 was not
enough for the long text in the test.
Without the truncate field the same request succeeds, and that is the bigger
trap: Ollama silently cuts the text to the context size and embeds only part of it. So if
your vectors seem to miss part of long documents, this may be why, even though you never
saw the error.
The error
PS> curl.exe -s -i http://127.0.0.1:15190/api/embed -d '@long-false.json' (truncate false)
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Date: Fri, 06 Nov 2026 07:19:50 GMT
Content-Length: 55
{"error":"the input length exceeds the context length"}
Why it happens
An embedding model reads at most a fixed number of tokens. For all-minilm,
ollama show reports a context length of 512 and a default
num_ctx parameter of 256. The test text, one sentence repeated 60 times (4,799
characters), is far longer than that. With "truncate":false the server
refuses it; with the field missing or true it keeps only 256 tokens. The successful answers
give it away: both report "prompt_eval_count":256.
PS> curl.exe -s http://127.0.0.1:15190/api/embed -d '@long-default.json' (no truncate field)
{"model":"all-minilm","embeddings":[[-0.0013283596,-0.01993357,0.014072818,0.019788582,-0.00028737905,0.0496281,-0.01977 ... (cut) ... 87608226,-0.12967187,-0.02219416]],"total_duration":477079600,"load_duration":5084400,"prompt_eval_count":256}
Raising the window helped only for the shorter text. With
"options":{"num_ctx":512} a 25-sentence text (1,999 characters, 402 tokens)
was accepted, but the full 60-sentence text still failed with the same error at 512.
The fix
PS> curl.exe -s http://127.0.0.1:15190/api/embed -d '@chunks.json' (five chunks of 12 sentences, truncate false)
{"model":"all-minilm","embeddings":[[0.001512101,-0.020383036,0.007426407,0.023070399,0.010554669,0.05640311,-0.00927711 ... (cut) ... 75825,-0.12127529,-0.017551184]],"total_duration":919162900,"load_duration":801576300,"prompt_eval_count":970}
vectors returned: 5
The same 60 sentences, cut into five chunks of 12 and sent as a JSON array in
input, came back as five vectors with truncate still
false, 970 tokens in all and nothing lost. That is the shape a RAG pipeline
wants anyway: one vector per passage, small enough to match a question. Keep
"truncate":false in production so an oversized chunk fails loudly instead of
being cut, and size chunks against the model's num_ctx. If a
slightly bigger window is all you need, pass num_ctx in
options, as the 25-sentence test did with 512.
How it was reproduced
Ollama 0.40.14 on Windows 11, against a separate test server started with
ollama serve on 127.0.0.1:15190 with its own models folder and
all-minilm pulled into it. The request bodies were written to JSON files by a
PowerShell script and sent with curl.exe: the long text with
truncate false, missing and true, with num_ctx 512, a 25-sentence
text, and the five-chunk array. The sample sentence uses fictional names. Long vectors are
cut in the output above, as marked.
Frequently asked
- What does 'the input length exceeds the context length' mean in Ollama?
- The text sent to /api/embed has more tokens than the model's context window, and the request set truncate to false. Split the text into smaller chunks; raising num_ctx to 512 helped a 402-token text but not a longer one.
- Does Ollama truncate long embedding input?
- Yes, by default. Without a truncate field, or with truncate true, the server keeps only num_ctx tokens and embeds those. In the test both answers reported prompt_eval_count 256 for a much longer text.
- How do I embed a long document with Ollama?
- Split it into passages that fit the model's num_ctx and send them as an array in the input field. Each passage gets its own vector, which is also what retrieval needs.
More decoded errors in the Fixes category. Chunking for a real RAG pipeline in .NET is built step by step in Chunks, Embeddings and pgvector.