haystack icon indicating copy to clipboard operation
haystack copied to clipboard

bug: HuggingFaceLocalGenerator returns N×M replies instead of N when stop_words has multiple entries

Open sachinn854 opened this issue 6 months ago • 9 comments

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

System:

  • OS: any
  • Haystack version: latest main
  • Component: HuggingFaceLocalGenerator

sachinn854 avatar May 26 '26 13:05 sachinn854

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.

sachinn854 avatar May 26 '26 13:05 sachinn854

hi sachin, did you opened the pr? I'd like to work on this

rautaditya2606 avatar May 26 '26 18:05 rautaditya2606

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?

sachinn854 avatar May 26 '26 18:05 sachinn854

it's okay , if you are going to work on this issue , but please let the maintainer verify it first

sachinn854 avatar May 26 '26 18:05 sachinn854

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.

rautaditya2606 avatar May 26 '26 18:05 rautaditya2606

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.

sachinn854 avatar May 26 '26 18:05 sachinn854

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.

rautaditya2606 avatar May 26 '26 18:05 rautaditya2606

@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.

sachinn854 avatar May 27 '26 11:05 sachinn854

We'll get back to you shortly with a PR review. Thank you for your efforts! 👍

julian-risch avatar May 29 '26 08:05 julian-risch