lobehub/lobe-chat

[Bug] Anthropic API returns error when the first message is from assistant. #1601

Happ1ness-dev posted onGitHub

💻 Operating System

Android

📦 Environment

Other

🌐 Browser

Chrome

🐛 Bug Description

Seems like Anthropic API isn't really happy when you're sending first message under the assistant role.

I accidentally found out about it when I tried to limit the message history sent to the model.

In the error, it says:

{
  "error": {
    "status": 400,
    "headers": {
      "cf-cache-status": "DYNAMIC",
      "cf-ray": [redacted],
      "connection": "keep-alive",
      "content-length": "120",
      "content-type": "application/json",
      "date": [redacted],
      "request-id": [redacted],
      "server": "cloudflare",
      "via": "1.1 google",
      "x-cloud-trace-context": [redacted],
      "x-should-retry": "false"
    },
    "error": {
      "type": "error",
      "error": {
        "type": "invalid_request_error",
        "message": "messages: first message must use the \"user\" role"
      }
    }
  },
  "endpoint": "https://api.anthropic.com",
  "provider": "anthropic"
}

This doesn't seem to apply to system role though.

🚦 Expected Behavior

Try to not send assistant message as first in the list. System role seems to be fine though.

📷 Recurrence Steps

Option 1:

  • Send a message from the user.
  • Wait for the response from the bot.
  • Delete the user message, so the history is starting with assistant's message.
  • Try chatting with assistant now.

Option 2:

  • Send a couple of messages.
  • Adjust history cut-off so the first message in history is from the assistant.
  • Try chatting with assistant now.

📝 Additional Information

The issue isn't happening with OpenAI API, so I think the only part that needs changing is Anthropic API implementation.


👀 @Happ1ness-dev

Thank you for raising an issue. We will investigate into the matter and get back to you as soon as possible. Please make sure you have given us as much context as possible.
非常感谢您提交 issue。我们会尽快调查此事,并尽快回复您。 请确保您已经提供了尽可能多的背景信息。

posted by lobehubbot 12 months ago

messages: first message must use the "user" role

Message indicates that the requested message starts with "assistant" role rather than "user" role, which is not permitted in Claude.

In fact, requests to Claude must start with "user" role and exactly follows with "assistant" and "user" pairs. Any other request is "bad" (400).

Ex. OK:

"messages": [
  { "role": "user", ...}
]
"messages": [
  { "role": "user", ...},
  { "role": "assistant", ...},
  { "role": "user", ...}
]

Bad:

"messages": [
  { "role": "assistant", ...}
]
"messages": [
  { "role": "user", ...},
  { "role": "assistant", ...},
  { "role": "assistant", ...},
  { "role": "user", ...}
]
posted by BrandonStudio 12 months ago

@BrandonStudio I can't seem to replicate the error with "user, assistant, assistant, user" messages sequence. Are you sure that 2 consecutive messages from assistant produce an error?

posted by Happ1ness-dev 12 months ago

不懂,我今天突然也有同样的问题,anthropic api测试没问题。

{ "error": { "status": 400, "headers": { "connection": "keep-alive", "content-length": "184", "content-type": "application/json", "date": "Wed, 27 Mar 2024 12:08:58 GMT", "request-id": "req_0154YLAibXPbELPQvh5G2J8C", "via": "1.1 google", "x-cloud-trace-context": "691f3559ace780d47541f113d73afd8a", "x-should-retry": "false" }, "error": { "type": "error", "error": { "type": "invalid_request_error", "message": "messages: roles must alternate between "user" and "assistant", but found multiple "assistant" roles in a row" } } }, "endpoint": "https://api.anthropic.com", "provider": "anthropic" }

posted by JZ86 12 months ago

不懂,我今天突然也有同样的问题,anthropic api测试没问题。

Is your history limit capped? That's probably why. Try uncapping it.

Yeah, unfortunately, it makes history limiting a lot less usable for claude, and the costs can get out of hand pretty quickly.

posted by Happ1ness-dev 12 months ago

I thinks there is a fixture for this: auto add an empty message pair for the miss role.

posted by arvinxx 12 months ago

@arvinxx I have actually yet to get the problem. As far as I know, if the request is exactly user, assistant, user, assitant, ..., there should not be any problem. As there is no additional information, I can only guess that maybe the request is not with the right pattern if previously a re-generation was triggered.

posted by BrandonStudio 12 months ago

As there is no additional information

As far as I understand, Anthropic API just need your messages history to start with user role. That's all.

posted by Happ1ness-dev 12 months ago

auto add an empty message pair for the miss role.

I don't think it has to be a pair. Just add an empty user message at the beginning if we're missing it.

Alternatively, we could delete (not send) assistant messages from the beginning of the history until it's starting with user message.

posted by Happ1ness-dev 12 months ago

I don't think it has to be a pair. Just add an empty user message at the beginning if we're missing it.

yeah, I mean this.

posted by arvinxx 12 months ago

Hope this is fixed. It makes no sense and makes interoperability with the rest of the LLMs difficult.

posted by soylomass 11 months ago

Hope this is fixed

Not sure about that. I'm still waiting for the right time to update and not break the UI (happens randomly in latest versions, afaik).

posted by Happ1ness-dev 11 months ago

✅ @Happ1ness-dev

This issue is closed, If you have any questions, you can comment and reply.
此问题已经关闭。如果您有任何问题,可以留言并回复。

posted by lobehubbot 10 months ago

:tada: This issue has been resolved in version 0.156.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

posted by lobehubbot 10 months ago

Fund this Issue

$0.00
Funded

Pull requests