Handle None content in OpenAI representation (#2353)#2525
Open
lntutor wants to merge 1 commit into
Open
Conversation
The OpenAI/AzureOpenAI representation guarded the response with
`hasattr(response.choices[0].message, "content")` before calling
`.content.strip()`. The `content` field is always present, so `hasattr`
is always True, but its value can be `None` (e.g. when the response is
blocked by the content filter and `finish_reason='content_filter'`).
This raised `AttributeError: 'NoneType' object has no attribute 'strip'`.
Read `content` first and check it against `None` explicitly, falling back
to the existing "No label returned" placeholder. Normal (non-None)
responses keep their current behavior.
Add unit tests that drive `extract_topics` with a mocked OpenAI client
returning `content=None` (asserting the fallback label) and normal
content (asserting stripping and prefix removal). The tests use
`pytest.importorskip("openai")`, and `openai` is added to the test extra
so they run in CI.
Fixes MaartenGr#2353
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01G8b9dEaJtpvydzz4ttM7JH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes #2353
Root cause
In
bertopic/representation/_openai.py, the generated label was guarded with:contentis always present as a field on the message, sohasattr(...)is alwaysTrue. When OpenAI/AzureOpenAI blocks a response via the content filter, it returnsfinish_reason='content_filter'withcontent=None. The guard passes andNone.strip()raises:as reported in the issue traceback (at the
.content.strip()line).Fix
Read
contentfirst and check it explicitly againstNone:This reuses the existing
"No label returned"placeholder and keeps behavior identical for normal (non-None) responses. There is a single chat-completions code path in this file, so no other call site needs changing.Test
Adds
tests/test_representation/test_openai.pywith two lightweight unit tests that callextract_topicsdirectly against a mocked OpenAI client (no model training):content=Nonereturns{0: [("No label returned", 1)]}and does not raise (this fails on the previous implementation with theAttributeErrorfrom the issue)."topic: Pets\n"is stripped and thetopic:prefix removed, yielding{0: [("Pets", 1)]}.The module imports
openaiat load time, so the tests usepytest.importorskip("openai"), andopenai>=1.0.0is added to thetestextra so they run under CI'suv sync --extra test.Before submitting