Skip to content

Commit cae0ebd

Browse files
author
Javier Lázaro
committed
fix(openai_compatible): handle None tool.description in function_call format and token counting
PromptMessageTool.description is typed as str but can be None in practice when using custom OpenAPI-based tool providers. This causes two crashes: 1. Line 601: Passing None as 'description' in the function_call payload sent to the LLM API, which may reject it. 2. Line 1130: _get_num_tokens_by_gpt2(tool.description) crashes with TypeError when description is None, since gpt2 tokenizer expects a string. Both are fixed with 'tool.description or ""' — a minimal, safe fallback consistent with the pattern already used in the azure_openai plugin (line 472). Reproduction: Create an OpenAPI tool provider whose endpoints have a summary but no description, then invoke an agent using those tools. Related: langgenius/dify-official-plugins#3095 (same bug in the azure_openai plugin's own _num_tokens_for_tools)
1 parent 418d1fd commit cae0ebd

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

  • src/dify_plugin/interfaces/model/openai_compatible

src/dify_plugin/interfaces/model/openai_compatible/llm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ def _generate(
598598
data["functions"] = [
599599
{
600600
"name": tool.name,
601-
"description": tool.description,
601+
"description": tool.description or "",
602602
"parameters": tool.parameters,
603603
}
604604
for tool in tools
@@ -1127,7 +1127,7 @@ def _num_tokens_for_tools(self, tools: list[PromptMessageTool]) -> int:
11271127
num_tokens += self._get_num_tokens_by_gpt2(tool.name)
11281128
num_tokens += self._get_num_tokens_by_gpt2("description")
11291129
if hasattr(tool, "description"):
1130-
num_tokens += self._get_num_tokens_by_gpt2(tool.description)
1130+
num_tokens += self._get_num_tokens_by_gpt2(tool.description or "")
11311131
if hasattr(tool, "parameters"):
11321132
parameters = tool.parameters
11331133
num_tokens += self._get_num_tokens_by_gpt2("parameters")

0 commit comments

Comments
 (0)