[models] Bad Request Error 400 in Claude - Sonnet 4 - all messages must have non-empty content #686

Closed
opened 2026-02-16 10:20:11 -05:00 by yindo · 5 comments
Owner

Originally created by @MitraSafarinejad on GitHub (Oct 4, 2025).

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

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:

  • My first response was extremely long.
  • The client shows no content in the answer field, although the text is present in agent_thoughts.
  • When I send a follow-up query, the model returns this 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'},

here is my log first and second questions information:

Image

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'}

Originally created by @MitraSafarinejad on GitHub (Oct 4, 2025). ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### 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: - My first response was extremely long. - The client shows no content in the answer field, although the text is present in agent_thoughts. - When I send a follow-up query, the model returns this 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'}, here is my log first and second questions information: <img width="1727" height="804" alt="Image" src="https://github.com/user-attachments/assets/d5612b1d-07bb-4414-a991-cf7ee7bd35c8" /> [sonnet 4 log.txt](https://github.com/user-attachments/files/22694311/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'}
yindo added the bug label 2026-02-16 10:20:11 -05:00
yindo closed this issue 2026-02-16 10:20:11 -05:00
Author
Owner

@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:

  • Before sending a new request to Claude, check your message history and remove or fill any messages with empty content ("" or null). This is especially important after a long response, a stopped generation, or when agent_thoughts is present but answer is empty [3].
  • Starting a new chat/session can sometimes bypass the issue, but it may recur if the underlying message construction isn’t fixed [1].
  • If you can upgrade, recent Dify versions have improved message construction and validation to help prevent this [5] [6].

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  Chat with Dosu Join Discord Share on X

@dosubot[bot] commented on GitHub (Oct 4, 2025): <!-- Answer --> 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]](https://github.com/langgenius/dify/issues/20857) [[2]](https://github.com/langgenius/dify/issues/22974) [[3]](https://github.com/langgenius/dify/issues/20206). 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]](https://github.com/langgenius/dify/issues/25953). Workarounds: - Before sending a new request to Claude, check your message history and remove or fill any messages with empty content ("" or null). This is especially important after a long response, a stopped generation, or when agent_thoughts is present but answer is empty [[3]](https://github.com/langgenius/dify/issues/20206). - Starting a new chat/session can sometimes bypass the issue, but it may recur if the underlying message construction isn’t fixed [[1]](https://github.com/langgenius/dify/issues/20857). - If you can upgrade, recent Dify versions have improved message construction and validation to help prevent this [[5]](https://github.com/langgenius/dify/pull/24663) [[6]](https://github.com/langgenius/dify/pull/23796). 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. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/6478c7f3-e275-4e9f-9c4a-432f7ba7fcdf?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/6478c7f3-e275-4e9f-9c4a-432f7ba7fcdf?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/6478c7f3-e275-4e9f-9c4a-432f7ba7fcdf?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/6478c7f3-e275-4e9f-9c4a-432f7ba7fcdf?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/6478c7f3-e275-4e9f-9c4a-432f7ba7fcdf?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/6478c7f3-e275-4e9f-9c4a-432f7ba7fcdf?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/6478c7f3-e275-4e9f-9c4a-432f7ba7fcdf?feedback_type=other)</sup>&nbsp;&nbsp;[![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/26574)
Author
Owner

@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.

@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.
Author
Owner

@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 ClaudeModelProvider class (usually in claude_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  Join Discord Share on X

@dosubot[bot] commented on GitHub (Oct 4, 2025): <!-- Answer --> 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](https://github.com/langgenius/dify-official-plugins/issues/976#issuecomment-2911201182) and [another similar report](https://github.com/langgenius/dify-official-plugins/issues/1064). If you’re patching your deployment, look for the message formatting logic in the `ClaudeModelProvider` class (usually in `claude_model_provider.py`) and ensure it filters out empty messages before sending requests. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/00a12f55-0dbd-4a63-90cd-76e9976e49a3?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/00a12f55-0dbd-4a63-90cd-76e9976e49a3?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/00a12f55-0dbd-4a63-90cd-76e9976e49a3?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/00a12f55-0dbd-4a63-90cd-76e9976e49a3?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/00a12f55-0dbd-4a63-90cd-76e9976e49a3?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/00a12f55-0dbd-4a63-90cd-76e9976e49a3?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/00a12f55-0dbd-4a63-90cd-76e9976e49a3?feedback_type=other)</sup>&nbsp;&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify-official-plugins/issues/1803)
Author
Owner

@MitraSafarinejad 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.

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

@MitraSafarinejad 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. 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
Author
Owner

@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:

  • You reported a bug in Dify v1.3.1 with Claude - Sonnet 4 where long responses cause subsequent requests to fail with a 400 error due to empty messages in conversation history.
  • The root cause is Claude's API requirement that all messages except the final assistant message must have non-empty content.
  • Dify v1.3.1 does not filter out empty messages before sending requests, triggering the error.
  • Workarounds include filtering out empty messages before API calls or upgrading to newer Dify versions with improved validation.
  • You clarified that in your use case, user/assistant prompts are managed in the workflow/chatflow, and the agent sends instructions as user input automatically.

Next Steps:

  • Please confirm if this issue is still relevant with the latest version of dify-official-plugins; if so, you can keep the discussion open by commenting here.
  • Otherwise, I will automatically close this issue in 5 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Oct 20, 2025): Hi, @MitraSafarinejad. I'm [Dosu](https://dosu.dev), and I'm helping the dify-official-plugins team manage their backlog and am marking this issue as stale. Issue Summary: - You reported a bug in Dify v1.3.1 with Claude - Sonnet 4 where long responses cause subsequent requests to fail with a 400 error due to empty messages in conversation history. - The root cause is Claude's API requirement that all messages except the final assistant message must have non-empty content. - Dify v1.3.1 does not filter out empty messages before sending requests, triggering the error. - Workarounds include filtering out empty messages before API calls or upgrading to newer Dify versions with improved validation. - You clarified that in your use case, user/assistant prompts are managed in the workflow/chatflow, and the agent sends instructions as user input automatically. Next Steps: - Please confirm if this issue is still relevant with the latest version of dify-official-plugins; if so, you can keep the discussion open by commenting here. - Otherwise, I will automatically close this issue in 5 days. Thank you for your understanding and contribution!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#686