Repository navigation
Fix find_topics crash when search_term is a list of strings - #2553
sebastianbreguel wants to merge 2 commits into
Conversation
Closes #2475, closes #2392 find_topics() always wrapped search_term in an extra list before passing it to the embedder, so a list input became a nested list. sentence-transformers then interpreted the inner list as a text-pair, raising IndexError for a single-element list and silently producing wrong embeddings for length-2 lists. Resolve search_term to a flat list of strings (string -> single-element list, list -> as-is), embed all terms, and average the resulting vectors into a single query embedding. Empty lists now raise ValueError explicitly instead of silently returning NaN similarities. Adds a regression test covering single- and multi-element list inputs across fixture models, plus a test for the empty-list ValueError.
|
The red CI here is flaky, and it comes from the Python 3.10 job, not from this change. I ran the same code twice, and the
The other versions show as failed because fail-fast cancelled them. In the second run, 3.14 finished and passed. None of the failing tests touch
The fixture is built in My guess, which I haven't proven: the 3.10 runner computes the embeddings with tiny numeric differences, and that flips HDBSCAN's automatic reduction between 7 and 9 topics. Two things fall out of this, both separate from this PR:
Could you re-run the 3.10 job? I don't have permission to. |
|
@MaartenGr could you review this? |
|
@sebastianbreguel Thank you for the PR. Note that there is a v1.0 version in the works where this is going to be fixed: https://github.com/MaartenGr/BERTopic/releases |
|
Thanks @MaartenGr, I see v1.0 already takes a list of search terms and averages them the same way. Closing this in favor of v1.0. |
Reopens #2479 (closed by mistake), rebased onto current
master.What does this PR do?
Fixes #2475
Fixes #2392
BERTopic.find_topics(search_term)crashes withIndexErrorwhensearch_termis a single-element list, and silently misbehaves with two-element lists.Root cause
find_topicsalways wrappedsearch_termin an extra list before passing it to_extract_embeddings:When the user passed a list, this produced a nested list (e.g.
[["car"]]).sentence-transformers' tokenizer then interpreted the inner list as a text-pair, raisingIndexErrorontext_tuple[1]for length-1 inputs and silently producing wrong embeddings for length-2 inputs (it appears to "work" for length ≥ 3 only by accident).Changes
search_termto a flat list of strings: a string becomes a single-element list (preserving prior behavior); a list is passed through unchanged.ValueErroron empty input instead of silently returningNaNsimilarities fromnp.meanover zero rows.str | List[str] | Noneand document the list semantics in the docstring.Tests
test_find_topics_with_listparametrized over["car"]and["car", "vehicle"]across two fixture models — would have caught this bug.test_find_topics_empty_list_raisescovers the empty-list guard.test_find_topicscontinues to pass (string input is unchanged).All
find_topicstests pass locally andruff check/ruff format --checkare clean.Before submitting