“Bad Request Error, Error code: 400 - [anthropic] {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: all messages must have non-empty content except for the optional final assistant message'}}” #6415

Closed
opened 2026-02-21 18:15:38 -05:00 by yindo · 19 comments
Owner

Originally created by @MitraSafarinejad on GitHub (Oct 29, 2024).

Self Checks

  • 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 (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

0.9.1-fix1

Cloud or Self Hosted

Self Hosted (Docker)

Steps to reproduce

Hello, I’m currently using the Anthropic claude 3.5 Sonnet model in agent mode and encountered this error message:

“Bad Request Error, Error code: 400 - [anthropic] {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: all messages must have non-empty content except for the optional final assistant message'}}”
Interestingly, if I open a new chat, the error disappears temporarily.

Could you please help me understand what might be causing this error and how I could resolve it?

image

✔️ Expected Behavior

The model should respond to user inputs in agent mode without error, processing each message smoothly.

Actual Behavior

The model returns a “Bad Request” error for requests, specifying that all messages must have non-empty content, even though the messages do contain content

Originally created by @MitraSafarinejad on GitHub (Oct 29, 2024). ### Self Checks - [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 (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [X] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [X] Please do not modify this template :) and fill in all the required fields. ### Dify version 0.9.1-fix1 ### Cloud or Self Hosted Self Hosted (Docker) ### Steps to reproduce Hello, I’m currently using the Anthropic claude 3.5 Sonnet model in agent mode and encountered this error message: “Bad Request Error, Error code: 400 - [anthropic] {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: all messages must have non-empty content except for the optional final assistant message'}}” Interestingly, if I open a new chat, the error disappears temporarily. Could you please help me understand what might be causing this error and how I could resolve it? ![image](https://github.com/user-attachments/assets/d9f676d4-74f9-4ef0-8f75-0cf3c01da91c) ### ✔️ Expected Behavior The model should respond to user inputs in agent mode without error, processing each message smoothly. ### ❌ Actual Behavior The model returns a “Bad Request” error for requests, specifying that all messages must have non-empty content, even though the messages do contain content
yindo added the 🐞 bug label 2026-02-21 18:15:38 -05:00
yindo closed this issue 2026-02-21 18:15:38 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Oct 29, 2024):

The "Bad Request Error, Error code: 400" with the message "all messages must have non-empty content except for the optional final assistant message" is likely caused by sending a prompt message with empty content to the Anthropic Claude 3.5 Sonnet model. This error can occur if any of the UserPromptMessage or SystemPromptMessage instances in the prompt_messages list have empty content.

To resolve this issue, ensure that all messages in the prompt_messages list have non-empty content before invoking the model. You can add a check to filter out or handle empty messages before they are processed and sent to the model. Here's a code snippet that demonstrates how you might ensure non-empty content:

# Ensure all messages have non-empty content
filtered_prompt_messages = [
    message for message in prompt_messages if message.content.strip() != ""
]

# If the last message is an AssistantPromptMessage, it can be empty
if isinstance(filtered_prompt_messages[-1], AssistantPromptMessage):
    filtered_prompt_messages[-1].content = filtered_prompt_messages[-1].content or ""

# Use filtered_prompt_messages in the model invocation
response = client.messages.create(
    model=model,
    messages=filtered_prompt_messages,
    stream=stream,
    extra_headers=extra_headers,
    **model_parameters,
    **extra_model_kwargs,
)

This code ensures that only messages with non-empty content are sent to the model, which should prevent the "Bad Request Error" from occurring [1].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Oct 29, 2024): <!-- Answer --> The "Bad Request Error, Error code: 400" with the message "all messages must have non-empty content except for the optional final assistant message" is likely caused by sending a prompt message with empty content to the Anthropic Claude 3.5 Sonnet model. This error can occur if any of the `UserPromptMessage` or `SystemPromptMessage` instances in the `prompt_messages` list have empty content. To resolve this issue, ensure that all messages in the `prompt_messages` list have non-empty content before invoking the model. You can add a check to filter out or handle empty messages before they are processed and sent to the model. Here's a code snippet that demonstrates how you might ensure non-empty content: ```python # Ensure all messages have non-empty content filtered_prompt_messages = [ message for message in prompt_messages if message.content.strip() != "" ] # If the last message is an AssistantPromptMessage, it can be empty if isinstance(filtered_prompt_messages[-1], AssistantPromptMessage): filtered_prompt_messages[-1].content = filtered_prompt_messages[-1].content or "" # Use filtered_prompt_messages in the model invocation response = client.messages.create( model=model, messages=filtered_prompt_messages, stream=stream, extra_headers=extra_headers, **model_parameters, **extra_model_kwargs, ) ``` This code ensures that only messages with non-empty content are sent to the model, which should prevent the "Bad Request Error" from occurring <sup>[[1]](https://github.com/langgenius/dify/blob/main/api/core/model_runtime/model_providers/anthropic/llm/llm.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@MitraSafarinejad commented on GitHub (Oct 29, 2024):

I appreciate the suggestion; however, I am using the Sonnet model in Chatbot Basic or Agent Mode, which doesn't involve handling UserPromptMessage or SystemPromptMessage parameters directly. This error still occurs under these conditions, and it temporarily resolves if I start a new chat.

Could there be another cause or solution applicable to Agent Mode specifically? Thank you very much for any further guidance you can provide!

@MitraSafarinejad commented on GitHub (Oct 29, 2024): I appreciate the suggestion; however, I am using the Sonnet model in Chatbot Basic or Agent Mode, which doesn't involve handling UserPromptMessage or SystemPromptMessage parameters directly. This error still occurs under these conditions, and it temporarily resolves if I start a new chat. Could there be another cause or solution applicable to Agent Mode specifically? Thank you very much for any further guidance you can provide!
Author
Owner

@dosubot[bot] commented on GitHub (Oct 29, 2024):

In Agent Mode, the error you're encountering might be related to how the Dify platform handles message history and resets it when starting a new chat. When you start a new chat, the message history is cleared, which might temporarily resolve the issue if the error is related to the content of previous messages.

The handleRestart function in the Dify codebase is responsible for resetting the conversation by clearing the message history and starting fresh. This function resets the conversationId and taskId, stops any ongoing chat processes, and updates the chat list to either an empty list or a new list containing the opening statement if configured [1].

If the error persists, ensure that all messages being processed in Agent Mode have non-empty content. Since you mentioned that you're not directly handling message parameters, it might be worth checking if there's any configuration or setup in your environment that could inadvertently lead to empty messages being sent.

Additionally, consider reviewing any logging or debugging information available through the Dify platform, as it can provide insights into the message flow and help identify any issues with message content [2][3][4].

To continue talking to Dosu, mention @dosu.

@dosubot[bot] commented on GitHub (Oct 29, 2024): <!-- Answer --> In Agent Mode, the error you're encountering might be related to how the Dify platform handles message history and resets it when starting a new chat. When you start a new chat, the message history is cleared, which might temporarily resolve the issue if the error is related to the content of previous messages. The `handleRestart` function in the Dify codebase is responsible for resetting the conversation by clearing the message history and starting fresh. This function resets the `conversationId` and `taskId`, stops any ongoing chat processes, and updates the chat list to either an empty list or a new list containing the opening statement if configured <sup>[[1]](https://github.com/langgenius/dify/blob/main/web/app/components/base/chat/chat/hooks.ts)</sup>. If the error persists, ensure that all messages being processed in Agent Mode have non-empty content. Since you mentioned that you're not directly handling message parameters, it might be worth checking if there's any configuration or setup in your environment that could inadvertently lead to empty messages being sent. Additionally, consider reviewing any logging or debugging information available through the Dify platform, as it can provide insights into the message flow and help identify any issues with message content <sup>[[2]](https://github.com/langgenius/dify-docs/blob/main/en/guides/monitoring/integrate-external-ops-tools/integrate-langfuse.md)</sup><sup>[[3]](https://github.com/langgenius/dify-docs/blob/main/en/guides/application-orchestrate/agent.md)</sup><sup>[[4]](https://github.com/langgenius/dify/blob/main/api/core/callback_handler/agent_tool_callback_handler.py)</sup>. <!-- Dosu Comment Footer --> *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
Author
Owner

@crazywoola commented on GitHub (Oct 29, 2024):

This is a known issue, we haven't fixed it yet. I have already talk to the team regarding your previous issue https://github.com/langgenius/dify/issues/9366 I will close previous issue, you can keep an eye on this one for updates.

@crazywoola commented on GitHub (Oct 29, 2024): This is a known issue, we haven't fixed it yet. I have already talk to the team regarding your previous issue https://github.com/langgenius/dify/issues/9366 I will close previous issue, you can keep an eye on this one for updates.
Author
Owner

@MitraSafarinejad commented on GitHub (Oct 29, 2024):

Thank you for your message. I just wanted to clarify that this issue is different from #9366. The error in #9366 was related to the number of tools called by the Anthropic models, but this issue is not about tools at all.

I think it might be better to reopen issue #9366 since the two issues are not related. Please let me know if you need any further details to help address this. Thank you!

@MitraSafarinejad commented on GitHub (Oct 29, 2024): Thank you for your message. I just wanted to clarify that this issue is different from #9366. The error in #9366 was related to the number of tools called by the Anthropic models, but this issue is not about tools at all. I think it might be better to reopen issue #9366 since the two issues are not related. Please let me know if you need any further details to help address this. Thank you!
Author
Owner

@tnfsmith commented on GitHub (Oct 29, 2024):

This also happens on AzureOpenAI

@tnfsmith commented on GitHub (Oct 29, 2024): This also happens on AzureOpenAI
Author
Owner

@dosubot[bot] commented on GitHub (Nov 29, 2024):

Hi, @MitraSafarinejad. I'm Dosu, and I'm helping the Dify team manage their backlog. I'm marking this issue as stale.

Issue Summary:

  • You reported a "Bad Request Error" (error code 400) with the Anthropic Claude 3.5 Sonnet model in agent mode.
  • I suggested ensuring all messages have content, but you clarified that the error occurs in Agent Mode, which doesn't involve direct message handling.
  • Crazywoola acknowledged it as a known issue, but you noted it is distinct from a previous issue.
  • Tnfsmith mentioned a similar issue occurring on AzureOpenAI.

Next Steps:

  • Please let us know if this issue is still relevant to the latest version of the Dify repository by commenting here.
  • If there is no further activity, this issue will be automatically closed in 15 days.

Thank you for your understanding and contribution!

@dosubot[bot] commented on GitHub (Nov 29, 2024): Hi, @MitraSafarinejad. I'm [Dosu](https://dosu.dev), and I'm helping the Dify team manage their backlog. I'm marking this issue as stale. **Issue Summary:** - You reported a "Bad Request Error" (error code 400) with the Anthropic Claude 3.5 Sonnet model in agent mode. - I suggested ensuring all messages have content, but you clarified that the error occurs in Agent Mode, which doesn't involve direct message handling. - Crazywoola acknowledged it as a known issue, but you noted it is distinct from a previous issue. - Tnfsmith mentioned a similar issue occurring on AzureOpenAI. **Next Steps:** - Please let us know if this issue is still relevant to the latest version of the Dify repository by commenting here. - If there is no further activity, this issue will be automatically closed in 15 days. Thank you for your understanding and contribution!
Author
Owner

@pru007 commented on GitHub (Dec 1, 2024):

I am using langgraph, I am facing the same issue there as well. Just wanted to check if this error is specific to Anthropic models.

@pru007 commented on GitHub (Dec 1, 2024): I am using langgraph, I am facing the same issue there as well. Just wanted to check if this error is specific to Anthropic models.
Author
Owner

@CosplayCookoff commented on GitHub (Dec 5, 2024):

Why is this closed? How is it fixed?? I have this 400 error atleast 5 times per day and it means I have to start a new interation of the api. I am so very frustrated!

@CosplayCookoff commented on GitHub (Dec 5, 2024): Why is this closed? How is it fixed?? I have this 400 error atleast 5 times per day and it means I have to start a new interation of the api. I am so very frustrated!
Author
Owner

@laipz8200 commented on GitHub (Dec 5, 2024):

Why is this closed? How is it fixed?? I have this 400 error atleast 5 times per day and it means I have to start a new interation of the api. I am so very frustrated!

Because we've fixed it in the main branch, it will work in the next version.

@laipz8200 commented on GitHub (Dec 5, 2024): > Why is this closed? How is it fixed?? I have this 400 error atleast 5 times per day and it means I have to start a new interation of the api. I am so very frustrated! Because we've fixed it in the main branch, it will work in the next version.
Author
Owner

@mrecek commented on GitHub (Mar 8, 2025):

I'm not sure this is fixed. I just updated to the latest version (2025.3.1) but I am receiving the "text content blocks must be non-empty" from my ESP32 S3 Box 3 when using the Claud API. Some commands work, but about half the time I get this error message. I'll also get this error when chatting via text prompts. For example, I'll type "turn off all the lights", it turns them off without errors, then I type "now turn them on" then I get this error message:

Sorry, I had a problem talking to Anthropic: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: text content blocks must be non-empty'}}

@mrecek commented on GitHub (Mar 8, 2025): I'm not sure this is fixed. I just updated to the latest version (2025.3.1) but I am receiving the "text content blocks must be non-empty" from my ESP32 S3 Box 3 when using the Claud API. Some commands work, but about half the time I get this error message. I'll also get this error when chatting via text prompts. For example, I'll type "turn off all the lights", it turns them off without errors, then I type "now turn them on" then I get this error message: Sorry, I had a problem talking to Anthropic: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: text content blocks must be non-empty'}}
Author
Owner

@MitraSafarinejad commented on GitHub (Jun 1, 2025):

Hello,

I'm currently using Dify version 1.3.1 and have encountered the same 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'}}

This occurs when utilizing the Anthropic Claude 4 Opus model in agent mode.
Additionally, I've noticed that when I open the logs, the "Thinking..." timer starts
Although this issue was marked as fixed in #11238, it seems to still be present in the latest version.

Image

Could you please provide guidance on how to resolve this?

@MitraSafarinejad commented on GitHub (Jun 1, 2025): Hello, I'm currently using Dify version 1.3.1 and have encountered the same 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'}} This occurs when utilizing the Anthropic Claude 4 Opus model in agent mode. Additionally, I've noticed that when I open the logs, the "Thinking..." timer starts Although this issue was marked as fixed in [#11238](https://github.com/langgenius/dify/pull/11238), it seems to still be present in the latest version. ![Image](https://github.com/user-attachments/assets/c4b853dc-cd9d-4ef1-9008-d1e47ad8f399) Could you please provide guidance on how to resolve this?
Author
Owner

@laipz8200 commented on GitHub (Jun 3, 2025):

Hi @MitraSafarinejad. Since this issue is already closed, you should open a new one for your problem. Claude doesn't support any empty messages in the prompt, you can check your inputs for the LLM in the tracing panel.

@laipz8200 commented on GitHub (Jun 3, 2025): Hi @MitraSafarinejad. Since this issue is already closed, you should open a new one for your problem. Claude doesn't support any empty messages in the prompt, you can check your inputs for the LLM in the tracing panel.
Author
Owner

@garyy7811 commented on GitHub (Jul 13, 2025):

latest version still having same problem :
⎿ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.13: all messages must have
non-empty content except for the optional final assistant message"}}

@garyy7811 commented on GitHub (Jul 13, 2025): latest version still having same problem : ⎿ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.13: all messages must have non-empty content except for the optional final assistant message"}}
Author
Owner

@Sarfaraz021 commented on GitHub (Jul 28, 2025):

this issue still persist even with claude sonnet 4

Image
@Sarfaraz021 commented on GitHub (Jul 28, 2025): this issue still persist even with claude sonnet 4 <img width="742" height="725" alt="Image" src="https://github.com/user-attachments/assets/2e6d2d40-cb90-49b5-8c35-b2f5313abaa5" />
Author
Owner

@tjallingvds commented on GitHub (Aug 20, 2025):

Still have the same issue team

@tjallingvds commented on GitHub (Aug 20, 2025): Still have the same issue team
Author
Owner

@lankabhedi commented on GitHub (Sep 22, 2025):

Still having this issue.

⎿ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.241: all messages must have non-empty content except for the optional final assistant message"},"request_id":"req_011CTQK8MZcoq2ZURLNWL85j"}

@lankabhedi commented on GitHub (Sep 22, 2025): Still having this issue. ` ⎿ API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"messages.241: all messages must have non-empty content except for the optional final assistant message"},"request_id":"req_011CTQK8MZcoq2ZURLNWL85j"} `
Author
Owner

@Mono-123 commented on GitHub (Oct 25, 2025):

Claude Sonnet 4-5 also encountered this issue while using the online platform's agent, and once an error occurs, the subsequent conversation cannot proceed. How should this be handled?

@Mono-123 commented on GitHub (Oct 25, 2025): Claude Sonnet 4-5 also encountered this issue while using the online platform's agent, and once an error occurs, the subsequent conversation cannot proceed. How should this be handled?
Author
Owner

@shimosaka commented on GitHub (Oct 26, 2025):

I am also encountering the same problem. My Dify version is 1.9.2 (the latest).

This is occurring with LLMs other than Claude as well, so it may not be a Claude-specific issue. For me, it happens in an app that generates images when file upload in chat is enabled. It seems that after an image is generated (or displayed), the handling of sys.files breaks, and it becomes empty.

Disabling file upload resolves the issue in my case.

@shimosaka commented on GitHub (Oct 26, 2025): I am also encountering the same problem. My Dify version is 1.9.2 (the latest). This is occurring with LLMs other than Claude as well, so it may not be a Claude-specific issue. For me, it happens in an app that generates images when file upload in chat is enabled. It seems that after an image is generated (or displayed), the handling of sys.files breaks, and it becomes empty. Disabling file upload resolves the issue in my case.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#6415