fix(openai_compatible): handle None tool.description in function_call format and token counting#322
Conversation
There was a problem hiding this comment.
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.
| 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 "") |
There was a problem hiding this comment.
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
cae0ebd to
36c4a26
Compare
Bug Description
Two crash points in
OAICompatLargeLanguageModelwhentool.descriptionisNone:"description": tool.descriptionpassesNoneinto the function_call payload_get_num_tokens_by_gpt2(tool.description)crashes withTypeErrorRoot Cause
PromptMessageTool.descriptionis typed asstrbut receivesNonein practice when using custom OpenAPI-based tool providers. The tool provider chain (ApiToolBundle.summary→ToolDescription.llm→PromptMessageTool.description) can produceNoneat several points.Fix
Add
or ""fallback to both lines — minimal, safe, consistent with the guard already used in theazure_openaiplugin (line 472):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
summarybut nodescriptionRelated
azure_openaiplugin's own_num_tokens_for_tools