MaartenGr / MaartenGr/BERTopic

OpenAI policy false positive trigger an error

Open
#2,342 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
7.8k
Forks
920
Avg merge
22h 24m
Merged PRs (30d)
5

Description

Have you searched existing issues? 🔎
  • I have searched and found no existing issues
Desribe the bug

When a policy is triggered by OpenAI, bertopic crash...
A solution could be to detect the 'code': 'content_filter' pair and create a "flagged" topic ?

logs:

2025-04-16 15:51:41,573 - BERTopic - Dimensionality - Fitting the dimensionality reduction algorithm
2025-04-16 15:51:58,486 - BERTopic - Dimensionality - Completed ✓
2025-04-16 15:51:58,486 - BERTopic - Cluster - Start clustering the reduced embeddings
2025-04-16 15:51:58,687 - BERTopic - Cluster - Completed ✓
2025-04-16 15:51:58,703 - BERTopic - Representation - Fine-tuning topics using representation models.
100%|██████████| 286/286 [08:10<00:00, 1.71s/it]
3%|▎ | 8/286 [00:12<07:07, 1.54s/it]

BadRequestError Traceback (most recent call last)
Cell In[27], line 18
3 topic_model = BERTopic(
4
5 # Pipeline models
(...)
14 verbose=True
15 )
17 # Train model
---> 18 topics, probs = topic_model.fit_transform(docs, embeddings)
20 # Reduce outliers with pre-calculate embeddings instead
21 new_topics = topic_model.reduce_outliers(docs, topics, probabilities=probs, strategy="embeddings", embeddings=embeddings)

File c:\Users\damien.bukudjian\AppData\Local\miniconda3\envs\orionenv\Lib\site-packages\bertopic_bertopic.py:515, in BERTopic.fit_transform(self, documents, embeddings, images, y)
511 self._save_representative_docs(custom_documents)
513 else:
514 # Extract topics by calculating c-TF-IDF, reduce topics if needed, and get representations.
--> 515 self._extract_topics(
516 documents, embeddings=embeddings, verbose=self.verbose, fine_tune_representation=not self.nr_topics
517 )
518 if self.nr_topics:
519 documents = self._reduce_topics(documents)

File c:\Users\damien.bukudjian\AppData\Local\miniconda3\envs\orionenv\Lib\site-packages\bertopic_bertopic.py:4031, in BERTopic._extract_topics(self, documents, embeddings, mappings, verbose, fine_tune_representation)
...
(...)
1066 retries_taken=retries_taken,
1067 )

BadRequestError: Error code: 400 - {'error': {'message': "The response was filtered due to the prompt triggering Azure OpenAI's content management policy. Please modify your prompt and retry. To learn more about our content filtering policies please read our documentation: https://go.microsoft.com/fwlink/?linkid=2198766", 'type': None, 'param': 'prompt', 'code': 'content_filter', 'status': 400, 'innererror': {'code': 'ResponsibleAIPolicyViolation', 'content_filter_result': {'hate': {'filtered': False, 'severity': 'safe'}, 'jailbreak': {'filtered': True, 'detected': True}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}}}}}

Reproduction

No response

BERTopic Version

0.17.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at BERTopic.fit_transform in _bertopic.py:515 and follow _extract_topics at _bertopic.py:4031 to the OpenAI representation call shown in the traceback. Reproduce the Azure OpenAI content_filter BadRequestError if possible, then define and test handling that prevents the fitting process from crashing while preserving an appropriate result for the affected topic.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.