serialization
Use this sub-skill when the task is about persisting an already fitted BERTopic model, reloading it locally, or publishing/loading it through the Hugging Face Hub.
Route here for
BERTopic.save(...)andBERTopic.load(...)workflows.- Choosing among
serialization="safetensors",serialization="pytorch", andserialization="pickle". - Deciding whether to include
save_ctfidf=Truefor topic-term inspection after reload. - Deciding how to use
save_embedding_model: a lightweight model pointer, omitting the backend, or reinjecting a backend at load time. - Local save/load from a pickle file or from a lightweight model directory.
- Hub upload with
BERTopic.push_to_hf_hub(...)and Hub loading withBERTopic.load("namespace/repo"). - Serialization-side failures from
BERTopic.merge_models(...), such as missingtorchorsafetensors; route the model-combination decision itself to topic-modeling.
Route elsewhere
- Training, fitting, transforming, partial fitting, topic reduction, topic merging, or outlier workflows: use the topic-modeling sub-skill.
- Embedding backend selection, custom backend implementation, API-client setup, or precomputed embedding construction: use the embeddings-backends sub-skill.
- c-TF-IDF or vectorizer tuning before saving: use the vectorizers-ctfidf sub-skill.
- Representation models, custom labels, or multi-aspect labels: use the representations-labeling sub-skill.
- Plotting, visualization, hierarchy dashboards, or figure export: use the analysis-visualization sub-skill.
Operating references
- Start with
references/api-reference.mdfor save/load/push signatures, format behavior, local file layout, and load-path resolution. - Use
references/workflows.mdfor local exact round trips, portable lightweight saves, embedding-model restoration, Hub upload/load, and local-vs-Hub decisions. - Use
references/troubleshooting.mdfor import failures, optional dependency errors, invalid paths, credential issues, embedding-backend gaps, and version/security warnings. - Run
scripts/smoke_serialization.pyfor a tiny local-only smoke check of pickle plus a lightweight format when the BERTopic runtime dependencies are available.
Minimal decision flow
- If the file will only be loaded in the same trusted Python and dependency environment,
pickleis the most complete format because it stores the full BERTopic object, including dimensionality-reduction and clustering sub-models. - If the model should be small, safer to share, production-oriented, or Hub-friendly, prefer
safetensorswithsave_ctfidf=Truewhen topic-term inspection after reload matters. - If
safetensorsis unavailable buttorchis already an accepted dependency, usepytorch; it creates the same lightweight directory style with.binweights. - If future
transform(...)calls should work without passingembeddings=, save a SentenceTransformer-compatible model id withsave_embedding_model="sentence-transformers/..."or pass a freshly constructed backend toBERTopic.load(..., embedding_model=...). - If the backend is custom or uses external API credentials, do not expect lightweight serialization to store it; reconstruct that backend separately and inject it at load time.
- If uploading to the Hub, use
push_to_hf_hub(...)only after confirminghuggingface_hubis installed and the process is authenticated or has a token. - If loading locally, pass an existing file or directory. If a missing path contains
/, BERTopic treats it as a Hub repo id.
Verification anchors
- Local no-network save/reload: fit or reuse a tiny synthetic model with precomputed embeddings, save as pickle and at least one lightweight format, reload from local file/directory, and assert topic counts plus representative topic metadata survive.
- Local-vs-Hub guidance: confirm local round trips do not require Hugging Face credentials, while
push_to_hf_hub(...)andBERTopic.load("namespace/repo")require Hub-oriented dependencies and network/auth conditions. - Embedding restoration edge case: load a lightweight model once without an embedding backend and once with an explicit backend or precomputed embeddings, then document the difference in future
transform(...)behavior.
