tomviner/llm-claude

Getting API Error 400: Invalid request

Closed this issue · 3 comments

The plugin used to work fine for me, but I now get errors whenever I try to pass a prompt. Here is what I get using the llm python API to pass a prompt to Claude:

Traceback (most recent call last):
  File "myscript.py", line 40, in gen_longsum
    initial_response = response.text()
                      ^^^^^^^^^^^^^^^
  File "\venv\Lib\site-packages\llm\models.py", line 112, in text
    self._force()
  File "\venv\Lib\site-packages\llm\models.py", line 106, in _force
    list(self)
  File "\venv\Lib\site-packages\llm\models.py", line 91, in __iter__
    for chunk in self.model.execute(
  File "\venv\Lib\site-packages\llm_claude\__init__.py", line 54, in execute
    completion = anthropic.completions.create(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\venv\Lib\site-packages\anthropic\_utils\_utils.py", line 250, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "\venv\Lib\site-packages\anthropic\resources\completions.py", line 223, in create
    return self._post(
           ^^^^^^^^^^^
  File "\venv\Lib\site-packages\anthropic\_base_client.py", line 949, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\venv\Lib\site-packages\anthropic\_base_client.py", line 748, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "\venv\Lib\site-packages\anthropic\_base_client.py", line 785, in _request
    raise self._make_status_error_from_response(request, err.response) from None
anthropic.BadRequestError: <html><title>Error 400 (Bad Request)!!1</title></html>

Note that I get the same error if I use llm in the command line like so:

llm -m claude "Compare and contrast the leadership styles of Abraham Lincoln and Boris Johnson."

I get this output:

Error: <html><title>Error 400 (Bad Request)!!1</title></html>

Anthropic's official documentation describes Error 400 as:

Invalid request: there was an issue with the format or content of your request.

Was there a breaking change in the past month that required updating the plugin to pass prompts in the right format, or is there somehow an issue on my end?

I'm on holiday for the next two weeks, but happy to review pull requests. Would you like to try updating the anthropic library please.

I did some troubleshooting and it turns out that my API key hadn't been properly stored. It works now. My bad!