Skip to content

Conversation

@wangln19
Copy link

@wangln19 wangln19 commented Oct 27, 2025

… when function_id format doesn't match the hardcoded assumption. Signed-off-by: wangln19 [email protected]

Purpose

Fix a robust parsing issue in KimiK2ToolParser that causes IndexError when function_id format doesn't match the hardcoded assumption.

Problem:
The current implementation assumes function_id is always in the format functions.xxx:0, using fragile parsing logic: split('.')[1].split(':')[0]. This causes an IndexError crash when the function_id is in the format xxx:0 (without the functions. prefix).

Solution:
Replace the fragile parsing logic with a more robust approach: split(':')[0].split('.')[-1]. This works correctly for both formats:

  • functions.get_weather:0get_weather
  • get_weather:0get_weather
  • a.b.c.func:1func

This change follows the principle of eliminating special cases - the code now handles all format variations uniformly without conditional branches.

Changes:

  • Fixed 3 occurrences in kimi_k2_tool_parser.py (line 98, 247, 257)
  • Updated test assertions in test_kimi_k2_tool_parser.py to remove overly strict format requirements and use the same robust parsing logic

Test Plan

Run existing test suite for KimiK2ToolParser:

pytest tests/tool_use/test_kimi_k2_tool_parser.py -v

Test Result

All existing tests pass with the new parsing logic. The updated parser now gracefully handles both function_id formats:

  • Original format: functions.function_name:index
  • Simplified format: function_name:index

The test suite validates:

  • Basic tool call extraction
  • Multi-tool call scenarios
  • Streaming functionality
  • Invalid JSON handling
  • Invalid function call format handling

Checklist:

  • The purpose of the PR - Fix IndexError crash in KimiK2ToolParser when function_id lacks namespace prefix
  • The test plan - Run existing pytest suite
  • The test results - All tests pass with more robust parsing logic
  • Documentation update - Not needed (internal parser logic improvement)
  • Release notes - Bug fix, no user-facing API changes
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

Copy link
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 addresses a critical IndexError in the KimiK2ToolParser by replacing fragile parsing logic with a more robust approach. The changes include modifications in kimi_k2_tool_parser.py and updates to test assertions in test_kimi_k2_tool_parser.py to accommodate both function ID formats. The review focuses on ensuring the correctness and robustness of the new parsing logic.

# assert tool call id format
assert actual_tool_call.id.startswith("functions.")
# assert tool call id format: should contain function name and numeric index
# Format can be either "functions.func_name:0" or "func_name:0"
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The comment should accurately reflect the format being asserted. It should mention that the format can also include a function name with multiple dot separated prefixes, such as a.b.c.func_name:0.

It's important that comments are accurate and reflect the code's functionality, as they serve as documentation for others (and your future self!).

function_name = function_id.split(".")[1].split(":")[0]
# function_id: functions.get_weather:0 or get_weather:0
# Extract function name: split by ':' first, then take last part after '.'
function_name = function_id.split(":")[0].split(".")[-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This change improves the robustness of the function name extraction. However, it's crucial to ensure that this new logic doesn't inadvertently introduce any regressions or unexpected behavior with other function ID formats that might exist but weren't covered in the initial bug report. Consider adding a comment to highlight the formats that are now supported, and a test case to cover the a.b.c.func:1 format mentioned in the description.

if current_tool_call_matches:
tool_id, tool_args = current_tool_call_matches.groups()
tool_name = tool_id.split(".")[1].split(":")[0]
tool_name = tool_id.split(":")[0].split(".")[-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This change improves the robustness of the tool name extraction. However, it's crucial to ensure that this new logic doesn't inadvertently introduce any regressions or unexpected behavior with other tool ID formats that might exist but weren't covered in the initial bug report. Consider adding a comment to highlight the formats that are now supported, and a test case to cover the a.b.c.func:1 format mentioned in the description.

if current_tool_call_name_matches:
(tool_id_str,) = current_tool_call_name_matches.groups()
tool_name = tool_id_str.split(".")[1].split(":")[0]
tool_name = tool_id_str.split(":")[0].split(".")[-1]
Copy link
Contributor

Choose a reason for hiding this comment

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

high

This change improves the robustness of the tool name extraction. However, it's crucial to ensure that this new logic doesn't inadvertently introduce any regressions or unexpected behavior with other tool ID formats that might exist but weren't covered in the initial bug report. Consider adding a comment to highlight the formats that are now supported, and a test case to cover the a.b.c.func:1 format mentioned in the description.

@chaunceyjiang chaunceyjiang self-assigned this Oct 27, 2025
… when function_id format doesn't match the hardcoded assumption. Signed-off-by: wangln19 <[email protected]>

Signed-off-by: wangln19 <[email protected]>
@chaunceyjiang chaunceyjiang added the ready ONLY add when PR is ready to merge/full CI is needed label Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend ready ONLY add when PR is ready to merge/full CI is needed tool-calling

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants