bug: HuggingFaceLocalGenerator returns N×M replies instead of N when stop_words has multiple entries
Describe the bug
HuggingFaceLocalGenerator uses a list comprehension with two for clauses to remove stop words
from replies. This creates a cross-product — with N replies and M stop words, the output contains
N×M replies instead of N. Half of the extra replies still contain the stop word.
Error message
No error is thrown. The output silently contains more replies than expected.
To Reproduce
from haystack.components.generators import HuggingFaceLocalGenerator
# Minimal reproduction without a real model
replies = ["Paris is the capital. STOP", "France is in Europe. STOP"]
stop_words = ["STOP", "END"]
# This is exactly what line 263 does:
buggy = [r.replace(sw, "").rstrip() for r in replies for sw in stop_words]
print(buggy)
# ['Paris is the capital.', 'Paris is the capital. STOP',
# 'France is in Europe.', 'France is in Europe. STOP']
# 4 replies instead of 2 — and 2 still contain the stop word!
Expected behavior
N replies in → N replies out, with all stop words removed from each:
["Paris is the capital.", "France is in Europe."]
Root Cause
haystack/components/generators/hugging_face_local.py line 263:
# Buggy — cross-product
replies = [reply.replace(stop_word, "").rstrip() for reply in replies for stop_word in self.stop_words]
The sibling class HuggingFaceChatLocalGenerator (chat/hugging_face_local.py line 654) already
does this correctly:
# Correct — sequential
for stop_word in stop_words:
replies = [reply.replace(stop_word, "").rstrip() for reply in replies]
Fix
Apply the same pattern used in HuggingFaceChatLocalGenerator:
if self.stop_words:
for stop_word in self.stop_words:
replies = [reply.replace(stop_word, "").rstrip() for reply in replies]
Additional context
Only affects non-streaming mode with 2+ stop words. Streaming path uses HFTokenStreamingHandler
which handles stop words separately and is not affected.
FAQ Check
- [x] Have you had a look at our new FAQ page?
System:
- OS: any
- Haystack version: latest main
- Component:
HuggingFaceLocalGenerator
I'd like to work on this — if this looks like a valid bug to you, I'm happy to open a PR with the fix.
hi sachin, did you opened the pr? I'd like to work on this
hey @rautaditya2606 , first lets maintainer verify this Issue , i already have told , that i am going to work for this issue , if maintainer will verify it, why did you raise PR?
it's okay , if you are going to work on this issue , but please let the maintainer verify it first
hey @rautaditya2606 , first lets maintainer verify this Issue , i already have told , that i am going to work for this issue , if maintainer will verify it, why did you raise PR?
hi sachin, apologies, I've closed the pr.
It's okay bro, no problem. You can work on this issue , I have no problem with that. But please wait for the maintainer to verify it first.
It's okay bro, no problem. You can work on this issue , I have no problem with that. But please wait for the maintainer to verify it first.
thanks sachin, really appreciate it. Will wait for maintainer verification.
@julian-risch I’m currently waiting for the maintainer’s review on this issue. Also, I noticed there are already two duplicate PRs open: #11413 and #11414.
We'll get back to you shortly with a PR review. Thank you for your efforts! 👍