Skip to content

Fix delete_topics when custom labels are set - #2540

Open
CaptainAni187 wants to merge 1 commit into
MaartenGr:masterfrom
CaptainAni187:fix/delete-topics-custom-labels-list
Open

Fix delete_topics when custom labels are set#2540
CaptainAni187 wants to merge 1 commit into
MaartenGr:masterfrom
CaptainAni187:fix/delete-topics-custom-labels-list

Conversation

@CaptainAni187

Copy link
Copy Markdown

Fixes #2529

Problem

custom_labels_ is stored as a list ordered by topic. set_topic_labels converts a dict argument into a list before assigning it, so the attribute is never a mapping:

https://github.com/MaartenGr/BERTopic/blob/master/bertopic/_bertopic.py#L2028-L2040

delete_topics however iterated it as one:

for old_topic, label in self.custom_labels_.items()

so calling delete_topics() on a model with custom labels raised AttributeError: 'list' object has no attribute 'items'.

A second instance of the same assumption sits in the branch that initialises the -1 topic. self.custom_labels_[-1] = "" overwrites the last topic's label instead of adding one for the newly created outlier topic. That one is silent, and currently masked because the crash above always happens first — fixing only the reported error would have exposed it.

Fix

  • Record the topics custom_labels_ is ordered by before the topic mapping is rebuilt, then remap the list positionally and re-emit it in new-topic order.
  • Prepend the outlier label with insert(0, ""), since -1 sorts first, and keep the tracked topic list in step.
  • Only remap when the label count matches the topic count, mirroring the existing guard in get_topic_info.

The rest of the surrounding code is unchanged — the other attributes really are dicts, so their comprehensions still apply.

Tests

Adds test_delete_with_custom_labels to tests/test_reduction/test_delete.py, parametrised over the same six fixtures as the existing test_delete. Each topic is labelled after itself, so misalignment is detectable: the test asserts that every surviving topic keeps its own label across renumbering, that a newly created -1 receives an empty label rather than stealing another topic's, and that the list length tracks the topic count.

  • With the fix: 12 passed
  • With the fix reverted (test kept): the 6 new cases fail

ruff check and ruff format are clean at the pinned v0.14.0.

`custom_labels_` is stored as a list ordered by topic, but `delete_topics`
treated it as a mapping, so deleting a topic on a model with custom labels
raised `AttributeError: 'list' object has no attribute 'items'`.

Remap the list positionally against the topics it was built from. Also
prepend the label for a newly created outlier topic rather than assigning
to index -1, which silently overwrote the last topic's label.

Fixes MaartenGr#2529
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Topic deletion fails if topics have custom lables

1 participant