Ollama client mistakes a truncated response for a finished one: five integration bugs and how to catch them
A service writes a summary of a ticket, the text cuts off mid-sentence, yet the card gets the status "Done" and the log shows HTTP 200. Sergey Proshchaev broke down five Ollama integration bugs: HTTP 200 arrives before generation starts, an error can arrive inside the stream, and the stream can break off without a done line. On the test bench, a simplified client accepted five faulty responses; the client with checks rejected all of them. Mark a job "Done" only after done, an acceptable reason, and a database write.
AI-processed from Habr AI; edited by Hamidun News
Picture a service that writes a short summary of a ticket. The text appears on screen as it is generated, breaks off mid-sentence, yet the card gets the status "Done" and the summary goes out to export. The log shows HTTP 200, and a retried request goes through without trouble. Tech Lead Sergey Proshchaev, who leads Java and Kotlin development in fintech and e-commerce, broke down five Ollama integration bugs that make an application accept an unfinished generation, corrupt text, or create duplicates.
How the test bench works
Requests go to the native /api/generate endpoint with stream: true. The response comes back as NDJSON: one UTF-8 JSON object per line. The response field holds a text fragment; in the final object, done becomes true and done_reason gives the reason generation ended. The author recorded two real responses from Ollama 0.32.15 with the llama3.1:8b Q4_K_M model and compared two client implementations, one on urllib.request and one on http.client. The simplified client concatenates the response fields until the stream ends. The client with checks additionally verifies the protocol and the text, and saves the result under an operation key. Running the bench needs no model; everything is in the repository.
Five bugs
1. HTTP 200 as a sign of success. The status goes out with the headers, before generation begins. If an error happens later, the native API can send a separate object with an error field under the same HTTP 200. A collector that looks only for response skips that line and returns whatever it has accumulated. Check the error field before response and done.
2. A network chunk treated as a JSON line. HTTP chunk boundaries do not match NDJSON line boundaries. On the bench, the first chunk breaks off inside the UTF-8 bytes of the letter "Д", and json.loads() fails with a decoding error. Both bench clients read whole lines up to the newline, so they assemble the text correctly. After switching HTTP libraries, run the same response through with different read boundaries.
3. Saving on EOF. If the server sends all the text and closes the body but never sends the final done object, the simplified client accepts the result and the client with checks rejects it. A similar symptom is described in issue #17118 for /v1/responses in Ollama 0.31.1, but that concerns SSE in the compatible API, not native NDJSON. The reverse also happens: done arrives, but the body is cut off. In CPython 3.14.7, line-by-line reading missed two such failures, and separate checks caught them: calling response.read(1) after done, and comparing the remainder in response.length.
4. Any done: true as permission to write. With num_predict: 1, Ollama ends generation normally with the reason length, and the summary is left with a single word, "Here". For the final export, stop is acceptable, while length means an incomplete result: in a draft interface you can show it with a "truncated" label. It is better to reject an unknown reason. Empty text after stop is also rejected. And stop does not guarantee that the model followed the instruction: asked to return orange, the model produced Juicy.
5. Retrying generation with a fresh database insert. The timeout expired before confirmation, the caller repeated the request, both generations finished normally with different text, and the database ended up with two rows. The author suggests issuing an operation ID before the first request and keeping it on a technical retry. In SQLite, the ON CONFLICT (request_id) DO NOTHING clause protects against this. The same key with a changed request body should produce a conflict.
What to check in your own code
The author summarized the results in a table of twelve scenarios. The simplified client accepted five faulty cases: a stream without done, error after HTTP 200, length, a break after done without a closing chunk, and a body shorter than Content-Length. The client with checks rejected all of them. It also caps line size, body size, and the number of objects, and in a concurrent test with two attempts it leaves one row in the database. The results were cross-checked on CPython 3.14.7 locally and 3.12.3 in CI.
A practical test set for your own client is simple:
- remove the final line and confirm the status does not become "Done";
- insert an error object after HTTP 200 and check what the user sees;
- change the read boundaries and compare the final text;
- cap the generation and check your policy for length;
- repeat the operation with the same key, then with a changed body.
Limitations
This is an experiment on a bench with recorded responses, not a measurement under production load. The author advises choosing timeouts based on your own measurements: on the first capture, loading the model took 10.89 s, and timeout=10 in urlopen() limits only individual blocking operations, not the total response time. The calling code must enforce an overall deadline. With concurrent requests before the save, Ollama can still run twice, and emails and events need a separate delivery guarantee. The stream: false mode simplifies parsing to a single JSON object, but result checks and an operation key are needed there too.
- Dec 2, 2026Qwen3.8-27B at 4-Bit Quantization Weighs 16.6 GB Instead of 55 GB: How to Read the Filename
- Dec 2, 2026T-Decision-1 with 9B Parameters Beats Copernicus-57B on Russian: 71.6 vs. 71.2 Points
- Dec 2, 2026Canonical Releases Ubuntu 26.04 for RTX Spark Laptops with 128 GB of Shared Memory
- Dec 2, 2026AI-Written Rust Driver for NVIDIA Boosts Tesla V100 Memory by 26.8%
Key Questions Answered
How the test bench works?
Requests go to the native /api/generate endpoint with stream: true. The response comes back as NDJSON: one UTF-8 JSON object per line. The response field holds a text fragment; in the final object, done becomes true and done_reason gives the reason generation ended. The author recorded two real responses from Ollama 0.32.15 with the llama3.1:8b Q4_K_M model and compared two client implementations, one on urllib.request and one on http.client.
What are the key details on Five bugs?
1. HTTP 200 as a sign of success. The status goes out with the headers, before generation begins. If an error happens later, the native API can send a separate object with an error field under the same HTTP 200. A collector that looks only for response skips that line and returns whatever it has accumulated. Check the error field before response and done. 2. A network chunk treated as a JSON line. HTTP chunk boundaries do not match NDJSON line boundaries.
What to check in your own code?
The author summarized the results in a table of twelve scenarios. The simplified client accepted five faulty cases: a stream without done, error after HTTP 200, length, a break after done without a closing chunk, and a body shorter than Content-Length. The client with checks rejected all of them. It also caps line size, body size, and the number of objects, and in a concurrent test with two attempts it leaves one row in the database. The results were cross-checked on CPython 3.14.7 locally and 3.12.3 in CI.
Test Frontier LLMs & Autonomous Agents in One Workspace
Run Claude Opus 5.5, GPT-6, Gemini 3.1, and DeepSeek side-by-side in Nomad AI (nomadnet.ai) — unified model routing, deep research, and multimodal workflows under one subscription.
Daily 60-sec AI Voice Brief + Weekly Production AI Agent Playbook
Listen to a 60-second AI news brief every morning and get a weekly hands-on playbook for shipping AI agents to production — tools, MCP servers and patterns that work. Curated by Jemal Hamidun, Head of AI at Alpina Digital.
