MaartenGr / MaartenGr/BERTopic

BadRequestError: Error code: 400

Open
#2,469 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

Dear Maarten,

How are you? Hope this message finds you all well.

Recently, I encountered a BadRequestError with Error code 400 from OpenAI when I run BERTopic with the representation_model being set to ChatGPT5.2. And the full error information was pasted as the following:

BadRequestError: Error code: 400 - {'error': {'message': "Unsupported parameter: 'stop' is not supported with this model.", 'type': 'invalid_request_error', 'param': 'stop', 'code': 'unsupported_parameter'}}

I did some research myself, and I figured out that the newer ChatGPT models, from some newer ChatGPT 4.x, no longer support "stop".

I solved this problem by copying and pasting your source code into my local Jupyter notebook, then I commented out the following two lines from bertopic.representation._openai:

if self.generator_kwargs.get("model"):
            self.model = generator_kwargs.get("model")
            del self.generator_kwargs["model"]
        if self.generator_kwargs.get("prompt"):
            del self.generator_kwargs["prompt"]
        #if not self.generator_kwargs.get("stop"):
        #    self.generator_kwargs["stop"] = "\n"

This fixed my problem immediately, however, I don't think this is the best solution. Because, some other users may still stick to those old models which still support "stop". I proposed the following code, you may want to consider to add into your source code:

First, we define a new method

def _safe_chat_completion(self, kwargs):
    """
    Try a chat completion call.
    If the model rejects the `stop` parameter, retry without it.
    """
    try:
        return client.chat.completions.create(**kwargs)

    except openai.BadRequestError as e:
        error_msg = str(e)

        # Explicitly detect the stop-parameter error
        if "Unsupported parameter: 'stop'" in error_msg:
            # Remove stop and retry once
            kwargs = dict(kwargs)      # shallow copy
            kwargs.pop("stop", None)

            return client.chat.completions.create(**kwargs)

        # Any other error should still raise
        raise

Remove the following block in extract_topics

if self.exponential_backoff:
    response = chat_completions_with_backoff(self.client, **kwargs)
else:
    response = self.client.chat.completions.create(**kwargs)

And call the new defined function:

if self.exponential_backoff:
    response = chat_completions_with_backoff(
        self.client,
        **kwargs
    )
else:
    response = self._safe_chat_completion(kwargs)

I tested it, it worked. Hope this might be helpful to you.

Reproduction
if self.generator_kwargs.get("model"):
            self.model = generator_kwargs.get("model")
            del self.generator_kwargs["model"]
        if self.generator_kwargs.get("prompt"):
            del self.generator_kwargs["prompt"]
        #if not self.generator_kwargs.get("stop"):
        #    self.generator_kwargs["stop"] = "\n"

BERTopic Version

0.17.4

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 bertopic.representation._openai and inspect extract_topics, especially how generator_kwargs and the stop parameter reach the chat completion call. Reproduce the 400 error with a newer ChatGPT model, then verify that compatible models still work and that unrelated API errors are not retried or suppressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.