From bfacf4ace790e11968a2be8a4db855fa04cebbb0 Mon Sep 17 00:00:00 2001 From: yihong0618 Date: Sun, 1 Dec 2024 19:17:41 +0800 Subject: [PATCH] fix: claude can not handle empty string Signed-off-by: yihong0618 --- api/core/model_runtime/model_providers/anthropic/llm/llm.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/api/core/model_runtime/model_providers/anthropic/llm/llm.py b/api/core/model_runtime/model_providers/anthropic/llm/llm.py index b24324708b0067..3faf5abbe87f58 100644 --- a/api/core/model_runtime/model_providers/anthropic/llm/llm.py +++ b/api/core/model_runtime/model_providers/anthropic/llm/llm.py @@ -483,6 +483,10 @@ def _convert_prompt_messages(self, prompt_messages: Sequence[PromptMessage]) -> if isinstance(message, UserPromptMessage): message = cast(UserPromptMessage, message) if isinstance(message.content, str): + # handle empty user prompt see #10013 #10520 + # responses, ignore user prompts containing only whitespace, the Claude API can't handle it. + if not message.content.strip(): + continue message_dict = {"role": "user", "content": message.content} prompt_message_dicts.append(message_dict) else: