Ollama - `OllamaChatGenerator` returns `finish_reason=None` when output is truncated (`done_reason="length"`)
I maintainer di solito rispondono entro 1 giorno
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 (
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.
- Lingua principale
- Python
- Stelle
- 212
- Fork
- 396
- Merge medio
- 2g 9h
- PR unite (30g)
- 95
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di deepset-ai/haystack-core-integrations
-
integration:qdrant P3
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
deepset-ai/haystack-core-integrations#4228 · 4 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Qdrant: `in` filter with an empty list matches all documents (delete_by_filter deletes everything)Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 35/100
deepset-ai/haystack-core-integrations#4334 ·
I maintainer di solito rispondono entro 1 giorno
-
FAISS: filtered search misses matching documents outside the first top_k * 10 neighboursForse già presa @davidsbatista l’ha presa 2 giorni fa. Aperta
deepset-ai/haystack-core-integrations#4322 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Add Late Interaction Qdrant RetrieverForse già presa @anakin87 l’ha presa 6 giorni fa. Aperta
deepset-ai/haystack-core-integrations#4307 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Retrievers treat `run(top_k=0)` as unset because of `top_k or self.top_k`Forse già presa @bogdankostic l’ha presa 8 giorni fa. ApertaP3
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
deepset-ai/haystack-core-integrations#4273 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di deepset-ai/haystack-core-integrations
Issue simili
-
app: scripts status: backlog type: housekeeping
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
netbox-community/netbox-scripts#239 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
LmeSzinc/AzurLaneAutoScript#6194 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
micronaut-projects/micronaut-core#14585 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
langgenius/graphon#443 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 90/100
I maintainer di solito rispondono entro 1 giorno