Skip to content

BadRequestError: Error code: 400#2469

Description

@powerhorse1986

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions