Ollama - `OllamaChatGenerator` returns `finish_reason=None` when output is truncated (`done_reason="length"`)
Maintainers usually reply within 1 day
Assessment
This issue has not been assessed yet.
Description
Describe the bug
When a reply is cut off because num_predict was reached, Ollama returns done_reason="length". OllamaChatGenerator maps done_reason through FINISH_REASON_MAPPING, which only contains "stop" and "tool_calls", so "length" falls through to None. Truncated replies therefore have meta["finish_reason"] == None in the non-streaming path, and the final StreamingChunk.finish_reason / reply meta is also None when streaming.
This makes truncation impossible to detect from the Haystack side. Other chat generator integrations map their truncation reason to "length" (e.g. anthropic and amazon_bedrock map max_tokens, cohere and google_genai map MAX_TOKENS, llama_cpp, litellm and watsonx map length).
Expected behavior
done_reason="length" should produce finish_reason="length" in both paths.
To Reproduce
Offline, with Client.chat mocked:
from unittest.mock import patch
from ollama import ChatResponse, Message
from haystack.dataclasses import ChatMessage
from haystack_integrations.components.generators.ollama import OllamaChatGenerator
def resp(done_reason):
return ChatResponse(model="qwen3:0.6b", created_at="2026-01-01T00:00:00Z", done=True,
done_reason=done_reason, message=Message(role="assistant", content="The capital of France is"),
prompt_eval_count=10, eval_count=5)
gen = OllamaChatGenerator(generation_kwargs={"num_predict": 5})
gen.warm_up()
with patch.object(gen._client, "chat", return_value=resp("length")):
print("truncated ->", gen.run([ChatMessage.from_user("x")])["replies"][0].meta["finish_reason"])
# streaming
chunks = [
ChatResponse(model="m", created_at="t", done=False, message=Message(role="assistant", content="The capital")),
ChatResponse(model="m", created_at="t", done=True, done_reason="length",
message=Message(role="assistant", content=""), prompt_eval_count=10, eval_count=5),
]
seen = []
gen2 = OllamaChatGenerator(streaming_callback=lambda c: seen.append(c.finish_reason))
gen2.warm_up()
with patch.object(gen2._client, "chat", return_value=iter(chunks)):
r = gen2.run([ChatMessage.from_user("x")])["replies"][0]
print("streaming truncated ->", r.meta.get("finish_reason"), "chunk finish_reasons:", seen)
Actual output:
truncated -> None
streaming truncated -> None chunk finish_reasons: [None, None]
Expected output:
truncated -> length
streaming truncated -> length chunk finish_reasons: [None, 'length']
The same happens against a real server with generation_kwargs={"num_predict": 5} and a prompt that needs a longer answer.
Describe your environment (please complete the following information):
- OS: macOS
- Haystack version: 3.5.0
- Integration version: ollama-haystack 7.0.2 (
mainat 9d2a68d), ollama 0.6.3
Additional context
The fix is a one-line addition of "length": "length" to FINISH_REASON_MAPPING, plus a regression test. I'm happy to open a PR.
A possible separate follow-up: Ollama reports done_reason="stop" for replies that contain tool calls, so those currently surface finish_reason="stop" rather than "tool_calls". Changing that would alter existing behaviour, so I've left it out of scope here.
- Dominant language
- Python
- Stars
- 212
- Forks
- 396
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 95
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from deepset-ai/haystack-core-integrations
-
integration:qdrant P3
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
deepset-ai/haystack-core-integrations#4228 · 4 comments ·
Maintainers usually reply within 1 day
-
Qdrant: `in` filter with an empty list matches all documents (delete_by_filter deletes everything)Open
Difficulty 2/5 1-3 hours Newbie friendliness 35/100
deepset-ai/haystack-core-integrations#4334 ·
Maintainers usually reply within 1 day
-
FAISS: filtered search misses matching documents outside the first top_k * 10 neighboursPossibly taken @davidsbatista claimed this 2 days ago. Open
deepset-ai/haystack-core-integrations#4322 · 1 assignee ·
Maintainers usually reply within 1 day
-
Add Late Interaction Qdrant RetrieverPossibly taken @anakin87 claimed this 6 days ago. Open
deepset-ai/haystack-core-integrations#4307 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
-
Retrievers treat `run(top_k=0)` as unset because of `top_k or self.top_k`Possibly taken @bogdankostic claimed this 8 days ago. OpenP3
Difficulty 4/5 3-5 days Newbie friendliness 48/100
deepset-ai/haystack-core-integrations#4273 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
All issues in deepset-ai/haystack-core-integrations
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
micronaut-projects/micronaut-core#14585 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
langgenius/graphon#443 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
[BUG]: Management endpoints /v1/memory/stats and /v1/memory/counts bypass API key authenticationOpen
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
docling-project/docling-serve#788 ·
Maintainers usually reply within 1 day
-
Mend: dependency security vulnerability untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
opensearch-project/cross-cluster-replication#1805 ·
Maintainers usually reply within 1 day