Conversation
mmr() looped range(top_n - 1) times, removing one candidate from candidates_idx each iteration with no bound relative to that list's actual length. Whenever top_n_words on MaximalMarginalRelevance matches or exceeds a topic's number of extracted candidate words -- a topic with fewer words than the model's configured top_n_words is a normal outcome of topic extraction, not a rare edge case -- the loop exhausts candidates_idx and calls np.argmax on an empty array, raising ValueError with a numpy-version-dependent message (the exact wording in the issue vs. this environment's numpy differs, but both come from the same empty-array argmax). Clamp the loop to min(top_n - 1, len(candidates_idx)) so it never runs past the number of available candidates; when there are enough candidates this is a no-op and behavior is unchanged. Fixes MaartenGr#2266 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
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.
Problem
update_topics()withMaximalMarginalRelevanceraises aValueErrorwhen a topic's number of extracted candidate words is less than or equal totop_n_words— reported asLength of weights not compatible with specified axis, though the exact wording is numpy-version-dependent (I getattempt to get argmax of an empty sequenceon numpy 2.4.6 with the same root cause — see Testing below). This happens "randomly for certain trials," as the reporter noted, precisely because it depends on how many words a given topic actually extracted, which varies per topic/per hyperparameter trial.Root cause
bertopic/representation/_mmr.py'smmr():The loop runs
top_n - 1times, removing one entry fromcandidates_idxper iteration, with no check against how many candidates actually exist. Iftop_n(fromMaximalMarginalRelevance(top_n_words=...)) is>=the number of candidatewordsfor that topic,candidates_idxempties out before the loop ends, and the next iteration'snp.argmax(mmr)(or an equivalent empty-array reduction, depending on numpy version) raises.Fix
When there are enough candidates (the common case),
min(...)is a no-op and behavior is unchanged. When there are fewer, the loop stops once every candidate has been selected, returning all of them instead of crashing — which matchesmmr()'s own contract of returning up totop_nkeywords.Testing
mmr()(isolated from the fullBERTopic/Optuna pipeline in the issue): 3 candidate words,top_n=10→ValueError: attempt to get argmax of an empty sequenceon this environment's numpy (2.4.6). Confirmed the fix resolves it and returns all 3 words.tests/test_representation/test_mmr.pycovering:top_nexceeding available words,top_nexactly equal to available words, a single candidate word, and the existing "more candidates than top_n" case (to confirm no behavior change there).pytestdirectly in this environment —tests/conftest.pyeagerly importsumap, which isn't installed here, so pytest can't collect anything undertests/at all (fails before reaching my file). Ran the equivalent assertions as a plain script instead; please runpytest tests/test_representation/test_mmr.pyin CI/a configured dev environment to confirm under the project's actual test runner.python3 -m py_compilepasses on both changed/added files.Fixes #2266
🤖 Generated with Claude Code