fix(tool-parser): preserve "none"/"nil" strings as valid enum values in minimax_m2 (#39599)
Signed-off-by: Yiyang Liu <yiyangliu@microsoft.com> Signed-off-by: Yiyang Liu <37043548+ianliuy@users.noreply.github.com> Signed-off-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: sfeng33 <4florafeng@gmail.com> Co-authored-by: Roger Wang <hey@rogerw.io>
This commit is contained in:
co-authored by
Copilot
sfeng33
Roger Wang
parent
ca7e4546da
commit
63cc8a55a9
@@ -548,3 +548,133 @@ class TestAnyOfNullableParam:
|
|||||||
parsed = json.loads(tc[0]["arguments"])
|
parsed = json.loads(tc[0]["arguments"])
|
||||||
assert parsed["config"] == {"theme": "dark", "fontSize": 14}
|
assert parsed["config"] == {"theme": "dark", "fontSize": 14}
|
||||||
assert isinstance(parsed["config"], dict)
|
assert isinstance(parsed["config"], dict)
|
||||||
|
|
||||||
|
|
||||||
|
class TestNoneStringPreservation:
|
||||||
|
"""Regression tests for #39567: 'none' as a string must not become None."""
|
||||||
|
|
||||||
|
def test_none_string_preserved_in_enum(self):
|
||||||
|
"""'none' in an enum must stay as the string 'none', not Python None."""
|
||||||
|
tools = [
|
||||||
|
ChatCompletionToolsParam(
|
||||||
|
function=FunctionDefinition(
|
||||||
|
name="set_theme",
|
||||||
|
parameters={
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"theme": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["dark", "light", "none"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
parser = MinimaxM2ToolParser(FakeTokenizer(), tools=tools)
|
||||||
|
|
||||||
|
results = _feed(
|
||||||
|
parser,
|
||||||
|
[
|
||||||
|
'<minimax:tool_call><invoke name="set_theme">'
|
||||||
|
'<parameter name="theme">none</parameter>'
|
||||||
|
"</invoke></minimax:tool_call>",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
tc = _collect_tool_calls(results)
|
||||||
|
assert len(tc) == 1
|
||||||
|
parsed = json.loads(tc[0]["arguments"])
|
||||||
|
assert parsed["theme"] == "none"
|
||||||
|
assert parsed["theme"] is not None
|
||||||
|
|
||||||
|
def test_none_string_preserved_plain_string(self):
|
||||||
|
"""'none' as a plain string param must stay as 'none'."""
|
||||||
|
tools = [
|
||||||
|
ChatCompletionToolsParam(
|
||||||
|
function=FunctionDefinition(
|
||||||
|
name="echo",
|
||||||
|
parameters={
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"message": {"type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
parser = MinimaxM2ToolParser(FakeTokenizer(), tools=tools)
|
||||||
|
|
||||||
|
results = _feed(
|
||||||
|
parser,
|
||||||
|
[
|
||||||
|
'<minimax:tool_call><invoke name="echo">'
|
||||||
|
'<parameter name="message">none</parameter>'
|
||||||
|
"</invoke></minimax:tool_call>",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
tc = _collect_tool_calls(results)
|
||||||
|
assert len(tc) == 1
|
||||||
|
parsed = json.loads(tc[0]["arguments"])
|
||||||
|
assert parsed["message"] == "none"
|
||||||
|
|
||||||
|
def test_null_still_converts_to_none(self):
|
||||||
|
"""'null' in a nullable param must still become Python None."""
|
||||||
|
tools = [
|
||||||
|
ChatCompletionToolsParam(
|
||||||
|
function=FunctionDefinition(
|
||||||
|
name="update_profile",
|
||||||
|
parameters={
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"nickname": {
|
||||||
|
"anyOf": [{"type": "string"}, {"type": "null"}],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
parser = MinimaxM2ToolParser(FakeTokenizer(), tools=tools)
|
||||||
|
|
||||||
|
results = _feed(
|
||||||
|
parser,
|
||||||
|
[
|
||||||
|
'<minimax:tool_call><invoke name="update_profile">'
|
||||||
|
'<parameter name="nickname">null</parameter>'
|
||||||
|
"</invoke></minimax:tool_call>",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
tc = _collect_tool_calls(results)
|
||||||
|
assert len(tc) == 1
|
||||||
|
parsed = json.loads(tc[0]["arguments"])
|
||||||
|
assert parsed["nickname"] is None
|
||||||
|
|
||||||
|
def test_nil_string_preserved(self):
|
||||||
|
"""'nil' must stay as the string 'nil', not become None."""
|
||||||
|
tools = [
|
||||||
|
ChatCompletionToolsParam(
|
||||||
|
function=FunctionDefinition(
|
||||||
|
name="echo",
|
||||||
|
parameters={
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"value": {"type": "string"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
parser = MinimaxM2ToolParser(FakeTokenizer(), tools=tools)
|
||||||
|
|
||||||
|
results = _feed(
|
||||||
|
parser,
|
||||||
|
[
|
||||||
|
'<minimax:tool_call><invoke name="echo">'
|
||||||
|
'<parameter name="value">nil</parameter>'
|
||||||
|
"</invoke></minimax:tool_call>",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
tc = _collect_tool_calls(results)
|
||||||
|
assert len(tc) == 1
|
||||||
|
parsed = json.loads(tc[0]["arguments"])
|
||||||
|
assert parsed["value"] == "nil"
|
||||||
|
|||||||
@@ -160,16 +160,13 @@ class MinimaxM2ToolParser(ToolParser):
|
|||||||
Returns:
|
Returns:
|
||||||
The converted value
|
The converted value
|
||||||
"""
|
"""
|
||||||
# Check if the VALUE itself indicates null (not just if null is allowed)
|
|
||||||
if value.lower() in ("null", "none", "nil"):
|
|
||||||
return None
|
|
||||||
|
|
||||||
# Normalize types
|
# Normalize types
|
||||||
normalized_types = [t.lower() for t in param_types]
|
normalized_types = [t.lower() for t in param_types]
|
||||||
|
|
||||||
# Try each type in order of preference (most specific first, string as fallback)
|
# Try each type in order of preference (most specific first, string as fallback)
|
||||||
# Priority: integer > number > boolean > object > array > string
|
# Priority: null > integer > number > boolean > object > array > string
|
||||||
type_priority = [
|
type_priority = [
|
||||||
|
"null",
|
||||||
"integer",
|
"integer",
|
||||||
"int",
|
"int",
|
||||||
"number",
|
"number",
|
||||||
@@ -187,7 +184,11 @@ class MinimaxM2ToolParser(ToolParser):
|
|||||||
if param_type not in normalized_types:
|
if param_type not in normalized_types:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if param_type in ["string", "str", "text"]:
|
if param_type == "null":
|
||||||
|
if value.lower() == "null":
|
||||||
|
return None
|
||||||
|
continue
|
||||||
|
elif param_type in ["string", "str", "text"]:
|
||||||
return value
|
return value
|
||||||
elif param_type in ["integer", "int"]:
|
elif param_type in ["integer", "int"]:
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user