Skip to content

fix: handle missing 'type' field in tool parameter properties for OpenAITokenCounter#1403

Open
octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
octo-patch:fix/issue-1397-token-counter-missing-type-field
Open

fix: handle missing 'type' field in tool parameter properties for OpenAITokenCounter#1403
octo-patch wants to merge 1 commit intoagentscope-ai:mainfrom
octo-patch:fix/issue-1397-token-counter-missing-type-field

Conversation

@octo-patch
Copy link
Copy Markdown

Fixes #1397

Problem

When PlanNotebook is enabled, some plan-related tools (e.g., view_historical_plans) define parameter properties that do not include a type field. The _calculate_tokens_for_tools function in _openai_token_counter.py attempts to access properties[key]["type"] directly, causing a KeyError when the field is absent.

Solution

Replace the direct key access properties[key]["type"] with properties[key].get("type", "") so that missing type fields are handled gracefully with an empty string fallback, which is safe to encode and contributes 0 extra tokens.

Testing

Manually verified: constructing a tool schema where a property omits the type field (as in the view_historical_plans plan tool) no longer raises a KeyError when token counting is invoked.

…en counter (fixes agentscope-ai#1397)

When PlanNotebook is enabled, some plan-related tools may have parameter
properties without a 'type' field, causing a KeyError in
_calculate_tokens_for_tools. Use .get('type', '') instead of direct
key access to handle this gracefully.
@cla-assistant
Copy link
Copy Markdown

cla-assistant bot commented Apr 3, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

[Bug]:启用PlanNotebook后,使用OpenAITokenCounter时,对于plan相关工具的统计报错

1 participant