MaartenGr / MaartenGr/BERTopic
AttributeError: 'NoneType' object has no attribute 'strip' when using AzureOpenAI client
Nobody has claimed this yet.
- 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
BERTopic Error with Content Filtering in AzureOpenAI
Description
When using BERTopic with AzureOpenAI, I'm encountering an error when content filtering is triggered. The error occurs in _openai.py where it tries to strip a None value, failing to properly handle the case where OpenAI returns a content-filtered response.
Error
AttributeError: 'NoneType' object has no attribute 'strip'
Traceback
<bertopic._bertopic.BERTopic object at 0x8cf54a68230>
File "/azureml-envs/azureml_5493e10031d137133cf6acf55ecbab07/lib/python3.12/site-packages/bertopic/representation/_openai.py", line 234, in extract_topics
label = response.choices[0].message.content.strip().replace("topic: ", "")
└ [Choice(finish_reason='content_filter', index=0, logprobs=None, message=ChatCompletionMessage(content=None, refusal=None, rol...
└ ChatCompletion(id='chatcmpl-BTxsrB8nuD2wpjvlGY4BEKxeTXf1K', choices=[Choice(finish_reason='content_filter', index=0, logprobs...
AttributeError: 'NoneType' object has no attribute 'strip'
Suggested Fix
Update the condition to also check if content is not None:
if response and hasattr(response.choices[0].message, "content") and response.choices[0].message.content is not None:
label = response.choices[0].message.content.strip().replace("topic: ", "")
else:
label = "No label returned"
Reproduction
BERTopic Version
0.17.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in representation/_openai.py at the failing line in extract_topics, where response.choices[0].message.content is stripped. Inspect the content-filtered response shown in the traceback and handle a missing content value; done means this response no longer raises AttributeError and produces the stated fallback label.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 40/100