mirror of
https://github.com/langgenius/dify-official-plugins.git
synced 2026-07-21 17:45:23 -04:00
[models] Bad Request Error 400 in Claude - Sonnet 4 - all messages must have non-empty content #686
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @MitraSafarinejad on GitHub (Oct 4, 2025).
Self Checks
Dify version
1.3.1
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
While using Claude - Sonnet 4, I observed that after generating a very long response, the model fails to produce content in subsequent requests. In my case:
"[models] Bad Request Error, Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.1: all messages must have non-empty content except for the optional final assistant message'},
here is my log first and second questions information:
sonnet 4 log.txt
✔️ Expected Behavior
The model should return responses normally and not produce an error
❌ Actual Behavior
get error :
[models] Bad Request Error, Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.1: all messages must have non-empty content except for the optional final assistant message'}
@dosubot[bot] commented on GitHub (Oct 4, 2025):
This error happens because Claude requires every message in the request (except the optional final assistant message) to have non-empty content. In Dify v1.3.1, if a previous response is very long or if the answer field is empty but agent_thoughts is present, the conversation history can end up with an empty message, which triggers the 400 error from Claude’s API. This is a known issue and has been reported by multiple users, especially with Claude models and in agent mode [1] [2] [3].
There’s no config option to change this—it's enforced by Claude. Dify v1.3.1 does not filter or validate out empty messages before sending the request, so the error comes directly from Claude [4].
Workarounds:
If you want to patch your current version, add a validation step before sending messages to Claude: filter out or error on any message (except the last assistant message) that is empty. This logic typically belongs in the message construction or just before the LLM call.
For more technical details and code pointers, see the discussions and PRs linked above.
To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

@crazywoola commented on GitHub (Oct 4, 2025):
I think it's might be you have left the user or assistant empty, and all the prompt are filled in the system prompt.
@dosubot[bot] commented on GitHub (Oct 4, 2025):
This error happens because Claude's API requires every message in the payload to have non-empty content (except for the optional final assistant message), which is stricter than GPT or DeepSeek. After a long response or if generation is stopped, Dify may leave an empty message in the conversation history, causing the 400 error on the next request.
To avoid this, you need to filter out any messages with empty content before sending the payload to Claude. There’s no config option to change this—it's an Anthropic API requirement. The recommended workaround is to add a step in your Claude integration that removes empty messages from the message array right before making the API call. This should only be done for Claude, since other models don’t enforce this rule.
For reference and more details, see this thread and another similar report.
If you’re patching your deployment, look for the message formatting logic in the
ClaudeModelProviderclass (usually inclaude_model_provider.py) and ensure it filters out empty messages before sending requests.To reply, just mention @dosu.
How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

@MitraSafarinejad commented on GitHub (Oct 4, 2025):
Thank you for your response. I’m using the agent form, where I write instructions (prompt for agent). In addition, the user or assistant prompt are not part of the agent form; instead, they exist in the workflow and chatflow.
From what I observe, the agent seems to automatically send its instructions as the user input
@dosubot[bot] commented on GitHub (Oct 20, 2025):
Hi, @MitraSafarinejad. I'm Dosu, and I'm helping the dify-official-plugins team manage their backlog and am marking this issue as stale.
Issue Summary:
Next Steps:
Thank you for your understanding and contribution!