From 36c4a26b5e71fcb175e7b860cc619a92d6568d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20L=C3=A1zaro?= Date: Wed, 13 May 2026 12:49:13 +0200 Subject: [PATCH] fix(openai_compatible): handle None tool.description in function_call format, token counting, agent strategy, and tool parameter serialization When API tools have no summary/description in their OpenAPI spec, tool.description and tool.description.llm can be None, causing TypeError on string concatenation. Fixed locations: - openai_compatible/llm.py: function_call dict and _num_tokens_from_tools - agent/strategy.py: _convert_tool_to_prompt_message_tool - entities/tool.py: ToolProviderApiToolBundle.to_prompt_message_tool --- src/dify_plugin/entities/tool.py | 2 +- src/dify_plugin/interfaces/agent/strategy.py | 2 +- src/dify_plugin/interfaces/model/openai_compatible/llm.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/dify_plugin/entities/tool.py b/src/dify_plugin/entities/tool.py index 5fda0019..3bf34a71 100644 --- a/src/dify_plugin/entities/tool.py +++ b/src/dify_plugin/entities/tool.py @@ -359,7 +359,7 @@ def to_prompt_message(self) -> PromptMessageTool: for name, parameter in self.tool_parameters.items(): tool.parameters[name] = { "type": parameter.type.value, - "description": parameter.description, + "description": parameter.description or "", } if parameter.required: diff --git a/src/dify_plugin/interfaces/agent/strategy.py b/src/dify_plugin/interfaces/agent/strategy.py index 9d20bdef..eed44d29 100644 --- a/src/dify_plugin/interfaces/agent/strategy.py +++ b/src/dify_plugin/interfaces/agent/strategy.py @@ -302,7 +302,7 @@ def _convert_tool_to_prompt_message_tool( """Convert tool to prompt message tool""" message_tool = PromptMessageTool( name=tool.identity.name, - description=tool.description.llm if tool.description else "", + description=(tool.description.llm or "") if tool.description else "", parameters={ "type": "object", "properties": {}, diff --git a/src/dify_plugin/interfaces/model/openai_compatible/llm.py b/src/dify_plugin/interfaces/model/openai_compatible/llm.py index c5a1f46f..6b7a1d9a 100644 --- a/src/dify_plugin/interfaces/model/openai_compatible/llm.py +++ b/src/dify_plugin/interfaces/model/openai_compatible/llm.py @@ -598,7 +598,7 @@ def _generate( data["functions"] = [ { "name": tool.name, - "description": tool.description, + "description": tool.description or "", "parameters": tool.parameters, } for tool in tools @@ -1127,7 +1127,7 @@ def _num_tokens_for_tools(self, tools: list[PromptMessageTool]) -> int: num_tokens += self._get_num_tokens_by_gpt2(tool.name) num_tokens += self._get_num_tokens_by_gpt2("description") if hasattr(tool, "description"): - num_tokens += self._get_num_tokens_by_gpt2(tool.description) + num_tokens += self._get_num_tokens_by_gpt2(tool.description or "") if hasattr(tool, "parameters"): parameters = tool.parameters num_tokens += self._get_num_tokens_by_gpt2("parameters")