[Refactor] Extract shared coerce_to_schema_type utility from Minimax M2 tool parser (#43006)

Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
Flora Feng
2026-05-18 17:55:39 -04:00
committed by GitHub
parent 0191354827
commit 57fef4e0bf
3 changed files with 247 additions and 77 deletions
+2 -77
View File
@@ -25,6 +25,7 @@ from vllm.tool_parsers.abstract_tool_parser import (
Tool,
ToolParser,
)
from vllm.tool_parsers.utils import coerce_to_schema_type
logger = init_logger(__name__)
@@ -146,80 +147,6 @@ class MinimaxM2ToolParser(ToolParser):
return list(types)
def _convert_param_value_with_types(
self, value: str, param_types: list[str]
) -> Any:
"""
Convert parameter value to the correct type based on a list of possible types.
Tries each type in order until one succeeds.
Args:
value: The string value to convert
param_types: List of possible type strings
Returns:
The converted value
"""
# Normalize types
normalized_types = [t.lower() for t in param_types]
# Try each type in order of preference (most specific first, string as fallback)
# Priority: null > integer > number > boolean > object > array > string
type_priority = [
"null",
"integer",
"int",
"number",
"float",
"boolean",
"bool",
"object",
"array",
"string",
"str",
"text",
]
for param_type in type_priority:
if param_type not in normalized_types:
continue
if param_type == "null":
if value.lower() == "null":
return None
continue
elif param_type in ["string", "str", "text"]:
return value
elif param_type in ["integer", "int"]:
try:
return int(value)
except (ValueError, TypeError):
continue
elif param_type in ["number", "float"]:
try:
val = float(value)
return val if val != int(val) else int(val)
except (ValueError, TypeError):
continue
elif param_type in ["boolean", "bool"]:
lower_val = value.lower().strip()
if lower_val in ["true", "1", "yes", "on"]:
return True
elif lower_val in ["false", "0", "no", "off"]:
return False
continue
elif param_type in ["object", "array"]:
try:
return json.loads(value)
except json.JSONDecodeError:
continue
# Fallback: try JSON parse, then return as string
try:
return json.loads(value)
except json.JSONDecodeError:
return value
def _get_param_types_from_config(
self, param_name: str, param_config: dict
) -> list[str]:
@@ -280,9 +207,7 @@ class MinimaxM2ToolParser(ToolParser):
param_type = self._get_param_types_from_config(param_name, param_config)
# Convert value
param_dict[param_name] = self._convert_param_value_with_types(
param_value, param_type
)
param_dict[param_name] = coerce_to_schema_type(param_value, param_type)
return ToolCall(
type="function",
+97
View File
@@ -450,6 +450,103 @@ def make_valid_python(text: str) -> tuple[str, str] | None:
return candidate, added_text
_TYPE_ALIASES: dict[str, str] = {
"str": "string",
"text": "string",
"varchar": "string",
"char": "string",
"enum": "string",
"int": "integer",
"int32": "integer",
"int64": "integer",
"uint": "integer",
"uint32": "integer",
"uint64": "integer",
"long": "integer",
"short": "integer",
"unsigned": "integer",
"float": "number",
"float32": "number",
"float64": "number",
"double": "number",
"bool": "boolean",
"dict": "object",
"arr": "array",
"list": "array",
"sequence": "array",
}
def coerce_to_schema_type(value: str, schema_type: str | list[str]) -> Any:
"""Best-effort coercion of a raw string value to a JSON Schema type.
Tries each type in priority order (null > integer > number > boolean >
object > array > string) and returns the first successful coercion.
Falls back to the original string when no coercion succeeds.
Args:
value: The raw string value from the model output.
schema_type: One or more JSON Schema type strings
(e.g. ``"string"`` or ``["string", "null"]``).
"""
if isinstance(schema_type, str):
schema_type = [schema_type]
normalized_types = {
_TYPE_ALIASES.get(key, key) for t in schema_type for key in [t.strip().lower()]
}
# Priority: null > integer > number > boolean > object > array > string
type_priority = [
"null",
"integer",
"number",
"boolean",
"object",
"array",
"string",
]
for candidate_type in type_priority:
if candidate_type not in normalized_types:
continue
if candidate_type == "null":
if value.lower() == "null":
return None
continue
if candidate_type == "string":
return value
if candidate_type == "integer":
try:
return int(value)
except (ValueError, TypeError):
continue
if candidate_type == "number":
try:
val = float(value)
return val if val != int(val) else int(val)
except (ValueError, TypeError):
continue
if candidate_type == "boolean":
lower_val = value.lower().strip()
if lower_val in ("true", "1"):
return True
if lower_val in ("false", "0"):
return False
continue
if candidate_type in ("object", "array"):
try:
return json.loads(value)
except (json.JSONDecodeError, ValueError, TypeError):
continue
try:
return json.loads(value)
except (json.JSONDecodeError, ValueError):
return value
def compute_tool_delta(
previously_sent_args: str,
new_call: ToolCall,