MaartenGr / MaartenGr/BERTopic

AttributeError: 'NoneType' object has no attribute 'strip' when using AzureOpenAI client

Open
#2,353 5 comments 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

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.