lobehub/lobe-chat














The issue has been closed
[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.