fix: support list search terms in find_topics (#2475) - #2539
Open
kiruthick01 wants to merge 1 commit into
Open
Conversation
`find_topics` wrapped `search_term` in a list before passing it to `_extract_embeddings`, so a list argument became a nested list. In sentence-transformers' `Transformer.tokenize` a non-str, non-dict element falls through to the text-pair branch, which reads `text_tuple[0]` and `text_tuple[1]`. A one-element list therefore raised an IndexError, while a longer list was silently encoded as a sentence pair, ignoring every term after the second one. Normalize `search_term` to a list of strings and average the resulting word embeddings into a single query embedding. Passing a string is unchanged: averaging a single embedding returns that embedding. Empty lists and non-string entries now raise an informative error. Fixes MaartenGr#2475 Fixes MaartenGr#2392
Author
|
The All three come from the session-scoped
That reads to me like version-dependent nondeterminism in the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
find_topicswrapssearch_termin a list before passing it to_extract_embeddings, so a list argument becomes a nested list. In sentence-transformers'Transformer.tokenize, an element that is neither astrnor adictfalls through to the text-pair branch:This has two consequences:
IndexError: list index out of range(the reported bug).This PR normalizes
search_termto a list of strings and averages the resulting word embeddings into a single query embedding. Passing a string is unchanged, since averaging a single embedding returns that embedding. Empty lists and non-string entries now raise an informativeValueError/TypeErrorinstead of failing deep inside the tokenizer.Fixes #2475
Fixes #2392
A note on the approach
In #2392 you noted that
search_termis typed asstrand notlist[str], so I want to be upfront that supporting lists is a deliberate choice here rather than a settled one. The alternative — keeping thestr-only contract and raising a clearTypeError— is a smaller change and I am happy to switch to it if you prefer; the silent-truncation path above is the part that I think should not stay as-is either way. Please treat the specific approach as open to discussion.Tests
Three tests added to
tests/test_representation/test_representations.py:(a, b)sentence pair is not equivalent to(b, a);ruff checkandruff format --checkboth pass. I have not been able to run the full suite locally, so the new tests get their first real execution in CI.Before submitting
find_topicswhen called with a list of 1 string #2392 are open bug reports; the specific fix was not pre-agreed, hence the note above.