---
id: 18986
lang: en
title: "Ollama client mistakes a truncated response for a finished one: five integration bugs and how to catch them"
published_at: "2026-12-02 16:29:21"
source_name: "Habr AI"
source_url: "https://habr.com/ru/companies/otus/articles/1101922/?utm_campaign=1101922&utm_source=habrahabr&utm_medium=rss"
canonical_url: "https://news.hamidun.com/en/news/18986"
categories: ["llm"]
publisher: "AI News by Jemal Hamidun (news.hamidun.com)"
---

# Ollama client mistakes a truncated response for a finished one: five integration bugs and how to catch them

> **Краткая суть (BLUF):** 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.  - length with done: true means truncated text: a limit of 1 token left just "Here" - Retrying with the same operation key gives 1 row in the database instead of two - A chunk can split UTF-8 in the middle of a letter: read whole lines split on \n

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.

---
- **Первоисточник:** [Habr AI](https://habr.com/ru/companies/otus/articles/1101922/?utm_campaign=1101922&utm_source=habrahabr&utm_medium=rss)
- **Каноническая ссылка:** https://news.hamidun.com/en/news/18986
- **Экосистема автора:** [Бесплатный интенсив по вайбкодингу — предзапись в Нике](https://t.me/vibecodeguidebot?start=agent_intensive_news_md) · [Nomad AI](https://nomadnet.ai) · [Telegram @jemal_hamidun](https://t.me/jemal_hamidun)
