BERTopic is a topic modeling technique that leverages state-of-the-art transformer models (like BERT) and a class-based TF-IDF procedure to create dense clusters, resulting in easily interpretable topics while retaining important words in their descriptions. It is currently at version 0.17.4 and actively maintained with regular updates and feature enhancements.
pip install bertopicVerified import paths — ran on the pinned version, not inferred.
This quickstart example demonstrates how to initialize BERTopic, fit it to a dataset (the 20 newsgroups dataset is commonly used), and retrieve information about the discovered topics. The `fit_transform` method processes the documents, returning topic assignments and probabilities.
Pin the versions of `bertopic` and its core dependencies (`umap-learn`, `hdbscan`, `sentence-transformers`) in your `requirements.txt` or `pyproject.toml` to match the environment where the model was trained.
For reproducible results, initialize UMAP with a `random_state` and pass it to BERTopic: `from umap import UMAP; umap_model = UMAP(random_state=42); topic_model = BERTopic(umap_model=umap_model)`.
Calculate embeddings once using `sentence-transformers` (or your preferred embedding model) and then pass them as the `embeddings` argument to `topic_model.fit_transform(docs, embeddings=precomputed_embeddings)`.
For multilingual documents, initialize BERTopic with `topic_model = BERTopic(language="multilingual")`. This will load a multilingual model ('paraphrase-multilingual-MiniLM-L12-v2').Upgrade your Python environment to Python 3.9 or higher. Python 3.10 and 3.11 are explicitly supported, with 3.13 support added in recent patches.