Skip to content

fix(openai_compatible): handle None tool.description in function_call format and token counting#322

Open
v4mpir0ck wants to merge 1 commit into
langgenius:mainfrom
v4mpir0ck:fix/openai-compatible-none-tool-description
Open

fix(openai_compatible): handle None tool.description in function_call format and token counting#322
v4mpir0ck wants to merge 1 commit into
langgenius:mainfrom
v4mpir0ck:fix/openai-compatible-none-tool-description

Conversation

@v4mpir0ck
Copy link
Copy Markdown

Bug Description

Two crash points in OAICompatLargeLanguageModel when tool.description is None:

  1. Line 601"description": tool.description passes None into the function_call payload
  2. Line 1130_get_num_tokens_by_gpt2(tool.description) crashes with TypeError
TypeError: can only concatenate str (not "NoneType") to str

Root Cause

PromptMessageTool.description is typed as str but receives None in practice when using custom OpenAPI-based tool providers. The tool provider chain (ApiToolBundle.summaryToolDescription.llmPromptMessageTool.description) can produce None at several points.

Fix

Add or "" fallback to both lines — minimal, safe, consistent with the guard already used in the azure_openai plugin (line 472):

# Line 601
"description": tool.description or "",

# Line 1130  
num_tokens += self._get_num_tokens_by_gpt2(tool.description or "")

Impact

This bug blocks all agent invocations that use custom API tools with missing descriptions on any plugin that inherits from OAICompatLargeLanguageModel (OpenAI, Azure OpenAI, Ollama, etc).

How to Reproduce

  1. Create an OpenAPI tool provider with endpoints that have summary but no description
  2. Create an Agent using those tools
  3. Invoke the agent → crash before any tool call

Related

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the OpenAI-compatible LLM interface to ensure tool descriptions default to an empty string when null during function generation and token calculation. The review feedback identifies a redundant attribute check in the token calculation logic and suggests simplifying the code by removing the unnecessary conditional statement.

Comment on lines 1129 to +1130
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 "")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since description is a required field in the PromptMessageTool Pydantic model, the hasattr(tool, "description") check is redundant. An instance of PromptMessageTool will always have this attribute.

You can simplify this by removing the conditional check. This would also make the code more consistent with the _generate method (line 601), which accesses tool.description directly.

            num_tokens += self._get_num_tokens_by_gpt2(tool.description or "")

… 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
@v4mpir0ck v4mpir0ck force-pushed the fix/openai-compatible-none-tool-description branch from cae0ebd to 36c4a26 Compare May 13, 2026 11:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant