Ollama - `OllamaChatGenerator` returns `finish_reason=None` when output is truncated (`done_reason="length"`)

Open
#4,302 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@davidsbatista is already working on this.

Since Nov 19, 2026.

  • #4303 by @MaiAditya — open

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 (main at 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from deepset-ai/haystack-core-integrations

All issues in deepset-ai/haystack-core-integrations

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.