[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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user