Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions vllm/reasoning/hy_v3_reasoning_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class HYV3ReasoningParser(BaseThinkingReasoningParser):
"""

def __init__(self, tokenizer: TokenizerLike, *args, **kwargs):
init_kwargs = getattr(tokenizer, "init_kwargs", None) or {}
self.suffix: str = init_kwargs.get("token_suffix") or ""
super().__init__(tokenizer, *args, **kwargs)

# First, If there is reasoning_effort in chat_kwargs,
Expand All @@ -52,12 +54,12 @@ def __init__(self, tokenizer: TokenizerLike, *args, **kwargs):
@property
def start_token(self) -> str:
"""The token that starts reasoning content."""
return "<think>"
return f"<think{self.suffix}>"

@property
def end_token(self) -> str:
"""The token that ends reasoning content."""
return "</think>"
return f"</think{self.suffix}>"

def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
if self._identity_parser is not None:
Expand Down
29 changes: 20 additions & 9 deletions vllm/tool_parsers/hy_v3_tool_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ def _get_schema_options(arg_schema: dict) -> list[dict]:
Note: single ``type`` has the highest priority.
"""
if "type" in arg_schema:
type_val = arg_schema["type"]
# JSON Schema allows "type" to be an array to represent union types,
# e.g. "type": ["string", "object"].
# Expand it into an anyOf-equivalent format:
# [{"type": "string"}, {"type": "object"}]
# so that _get_types / _parse_value can handle it uniformly later.
if isinstance(type_val, list):
return [{"type": t} for t in type_val]
return [arg_schema]
if "anyOf" in arg_schema:
return arg_schema["anyOf"]
Expand Down Expand Up @@ -261,19 +269,22 @@ def __init__(self, tokenizer: TokenizerLike, tools: list[Tool] | None = None):
self._current_arg_is_string: bool = False # is current arg pure string?
self._streamed_json_len: int = 0 # bytes of JSON already sent

self.tool_calls_start_token: str = "<tool_calls>"
self.tool_calls_end_token: str = "</tool_calls>"
init_kwargs = getattr(tokenizer, "init_kwargs", None) or {}
self.suffix: str = init_kwargs.get("token_suffix") or ""

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.

⚪ Severity: LOW

The token_suffix value from the tokenizer config is interpolated into regex patterns (lines 289-303) without re.escape(). A malicious model's tokenizer_config.json could supply regex metacharacters to alter pattern matching of tool calls. Other parsers in this codebase (e.g., deepseekv32_tool_parser.py) correctly use re.escape().
Helpful? Add 👍 / 👎

💡 Fix Suggestion

Suggestion: Apply re.escape() to the token strings when interpolating them into regex patterns on lines 289-303. The token values (which contain the unescaped suffix) should be escaped before being used in regex compilation. For example, change the regex constructions to use re.escape(self.tool_call_start_token) instead of directly interpolating self.tool_call_start_token. Specifically:

self.tool_call_regex = re.compile(
    re.escape(self.tool_call_start_token) + r"(.*?)"
    + re.escape(self.tool_sep_token) + r"(.*?)"
    + re.escape(self.tool_call_end_token),
    re.DOTALL,
)

self.tool_call_portion_regex = re.compile(
    re.escape(self.tool_call_start_token) + r"(.*?)"
    + re.escape(self.tool_sep_token) + r"(.*)",
    re.DOTALL,
)

self.func_args_regex = re.compile(
    re.escape(self.arg_key_start_token) + r"(.*?)"
    + re.escape(self.arg_key_end_token) + r"\s*"
    + re.escape(self.arg_value_start_token) + r"(.*?)"
    + re.escape(self.arg_value_end_token),
    re.DOTALL,
)

This follows the same pattern used in deepseekv32_tool_parser.py and ensures that any regex metacharacters in the suffix from the tokenizer config are treated as literals.


self.tool_call_start_token: str = "<tool_call>"
self.tool_call_end_token: str = "</tool_call>"
self.tool_calls_start_token: str = f"<tool_calls{self.suffix}>"
self.tool_calls_end_token: str = f"</tool_calls{self.suffix}>"

self.tool_sep_token: str = "<tool_sep>"
self.tool_call_start_token: str = f"<tool_call{self.suffix}>"
self.tool_call_end_token: str = f"</tool_call{self.suffix}>"

self.arg_key_start_token: str = "<arg_key>"
self.arg_key_end_token: str = "</arg_key>"
self.tool_sep_token: str = f"<tool_sep{self.suffix}>"

self.arg_value_start_token: str = "<arg_value>"
self.arg_value_end_token: str = "</arg_value>"
self.arg_key_start_token: str = f"<arg_key{self.suffix}>"
self.arg_key_end_token: str = f"</arg_key{self.suffix}>"

self.arg_value_start_token: str = f"<arg_value{self.suffix}>"
self.arg_value_end_token: str = f"</arg_value{self.suffix}>"

self.tool_call_regex = re.compile(
rf"{self.tool_call_start_token}(.*?){self.tool_sep_token}"
Expand Down
Loading