[Tool Parser] Fixed exception handling for backslash escapes
https://github.com/vllm-project/vllm/pull/46708
Purpose
make_valid_python (in vllm/tool_parsers/utils.py) is used to add a missing closing character to close an incomplete Python tool call expression during streaming. It checks only the single character immediately preceding it to determine whether a closing quote terminates a string.
if index > 0 and text[index - 1] == "\\":
pass # treat quote as escaped
else:
bracket_stack.pop()
This is incorrect behavior in the case of consecutive backslashes. For example, if a real closing quote follows an escaped backslash, closing quote, the single-character check incorrectly treats the \ quote as escaped, causing the string to remain open and producing an invalid or unparseable result.
This PR counts the number of consecutive backslashes preceding the quote and considers the quote to be escaped only when that number is odd.
This is not a duplicate
I searched for open PRs and issues before submitting.
-
gh pr list --repo vllm-project/vllm --state open --search "make_valid_python"Enable tool calls after text chunks #17311 → This applies only to an unrelated 2025 draft (, “Enable tool calls after text chunks”) applies.
-
gh search issues --repo vllm-project/vllm 'make_valid_python backslash escape'→ No search results found.
This backslash-escape counting bug has not been resolved in any currently open PRs or issues.
Tests
Regression tests covering all input cases—normal, even number of backslashes, and odd number of backslashes—have been added inside tests/tool_parsers/test_utils.py ( ).TestMakeValidPythonEscapes
-
The patched version passes all
make_valid_pythonNonepasses all four cases. The prefix functionreturns the case with an even number of backslashes, so it catches the regression error in the new test.
-
python -m ruff check vllm/tool_parsers/utils.py tests/tool_parsers/test_utils.py→ Passed all checks. -
python -m ruff format --diff <same files>→ Already formatted.