Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

Added a check to ensure the input `text` is a string before proceeding with parsing (#22809)

crazywoola60c37fe4920f
diff --git a/api/core/llm_generator/llm_generator.py b/api/core/llm_generator/llm_generator.py
index f7fd93be4a2..331ac933c8c 100644
--- a/api/core/llm_generator/llm_generator.py
+++ b/api/core/llm_generator/llm_generator.py
@@ -114,7 +114,8 @@ class LLMGenerator:
),
)
- questions = output_parser.parse(cast(str, response.message.content))
+ text_content = response.message.get_text_content()
+ questions = output_parser.parse(text_content) if text_content else []
except InvokeError:
questions = []
except Exception:
diff --git a/api/core/llm_generator/output_parser/suggested_questions_after_answer.py b/api/core/llm_generator/output_parser/suggested_questions_after_answer.py
index c451bf514cb..98cdc4c8b77 100644
--- a/api/core/llm_generator/output_parser/suggested_questions_after_answer.py
+++ b/api/core/llm_generator/output_parser/suggested_questions_after_answer.py
@@ -15,5 +15,4 @@ class SuggestedQuestionsAfterAnswerOutputParser:
json_obj = json.loads(action_match.group(0).strip())
else:
json_obj = []
-
return json_obj
diff --git a/api/core/model_runtime/entities/message_entities.py b/api/core/model_runtime/entities/message_entities.py
index 9d010ae28d1..83dc7f05258 100644
--- a/api/core/model_runtime/entities/message_entities.py
+++ b/api/core/model_runtime/entities/message_entities.py
@@ -156,6 +156,23 @@ class PromptMessage(ABC, BaseModel):
"""
return not self.content
+ def get_text_content(self) -> str:
+ """
+ Get text content from prompt message.
+
+ :return: Text content as string, empty string if no text content
+ """
+ if isinstance(self.content, str):
+ return self.content
+ elif isinstance(self.content, list):
+ text_parts = []
+ for item in self.content:
+ if isinstance(item, TextPromptMessageContent):
+ text_parts.append(item.data)
+ return "".join(text_parts)
+ else:
+ return ""
+
@field_validator("content", mode="before")
@classmethod
def validate_content(cls, v):
diff --git a/api/core/workflow/nodes/tool/tool_node.py b/api/core/workflow/nodes/tool/tool_node.py
index 86d36f474dc..f437ac841d4 100644
--- a/api/core/workflow/nodes/tool/tool_node.py
+++ b/api/core/workflow/nodes/tool/tool_node.py
@@ -317,7 +317,13 @@ class ToolNode(BaseNode):
elif message.type == ToolInvokeMessage.MessageType.FILE:
assert message.meta is not None
assert isinstance(message.meta, dict)
- assert "file" in message.meta and isinstance(message.meta["file"], File)
+ # Validate that meta contains a 'file' key
+ if "file" not in message.meta:
+ raise ToolNodeError("File message is missing 'file' key in meta")
+
+ # Validate that the file is an instance of File
+ if not isinstance(message.meta["file"], File):
+ raise ToolNodeError(f"Expected File object but got {type(message.meta['file']).__name__}")
files.append(message.meta["file"])
elif message.type == ToolInvokeMessage.MessageType.LOG:
assert isinstance(message.message, ToolInvokeMessage.LogMessage)