Skip to content

[Bugfix][Tool Parser] Fix Hunyuan A13B nested JSON tool streaming - #38103

Closed
vineetatiwari27 wants to merge 2 commits into
vllm-project:mainfrom
vineetatiwari27:fix/hunyuan-a13b-nested-json-streaming
Closed

vineetatiwari27 wants to merge 2 commits into
vllm-project:mainfrom
vineetatiwari27:fix/hunyuan-a13b-nested-json-streaming

Conversation

@vineetatiwari27

@vineetatiwari27 vineetatiwari27 commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix streaming tool parsing for Hunyuan A13B when function_call.arguments contains deeply nested JSON objects.

Why this PR

  • Existing tool-parser fixes are for other parsers/structures (e.g. nested list handling in llama4_pythonic, and brace truncation in Hermes streaming).
  • This change is specifically for HunyuanA13BToolParser and its streaming reconstruction of function_call.arguments braces at arbitrary nesting depth.

Tests

  • pytest tests/tool_parsers/test_hunyuan_a13b_tool_parser.py -v

AI assistance

AI assistance was used to implement and validate this parser change.

Signed-off-by: Vineeta Tiwari <vineeta.tiwari2@ibm.com>

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

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.

Code Review

This pull request refactors the hunyuan_a13b_tool_parser to correctly handle nested JSON objects within streaming tool call arguments. The previous regex-based approach was replaced with a new brace-depth scanning mechanism, which includes the introduction of _scan_braced_json_object and _extract_tool_arguments_text methods. This change resolves a known limitation, allowing a previously xfailed test case for nested JSON parsing to now pass. However, a high-severity performance concern has been raised regarding the new implementation, as it re-scans the current_text on each token, leading to a quadratic time complexity (O(N^2)). It is recommended to refactor this to a stateful streaming parser to achieve linear time complexity (O(N)).

"sent_arguments"
]
if not self.streaming_state["sent_tools"][current_idx][
args_text = self._extract_tool_arguments_text(current_text, current_idx)

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.

high

The new approach using _extract_tool_arguments_text and _scan_braced_json_object correctly handles nested JSON, which is a great improvement. However, this implementation introduces a performance concern. On each call (for each new token), these functions re-scan a large portion of the growing current_text. This leads to an overall quadratic time complexity (O(N^2)) for parsing the arguments stream, where N is the length of the arguments. In a performance-critical library like vLLM, this can be a significant issue for tool calls with large arguments.

Consider refactoring this to use a stateful streaming parser. The parser's state (current position, brace depth, etc.) could be stored in self.streaming_state and updated incrementally with each new token, avoiding the need to re-scan previously processed text. This would bring the complexity down to linear time (O(N)).

Signed-off-by: Vineeta Tiwari <vineeta.tiwari2@ibm.com>
Made-with: Cursor
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity within 90 days. It will be automatically closed if no further activity occurs within 30 days. Leave a comment if you feel this pull request should remain open. Thank you!

@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed due to inactivity. Please feel free to reopen if you intend to continue working on it. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working stale Over 90 days of inactivity tool-calling

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant