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

Aperta
#4,302 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@davidsbatista ci sta già lavorando.

Dal 19/11/2026.

  • #4303 di @MaiAditya — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

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.

Lingua principale
Python
Stelle
212
Fork
396
Merge medio
2g 9h
PR unite (30g)
95

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di deepset-ai/haystack-core-integrations

Tutte le issue di deepset-ai/haystack-core-integrations

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.