Skip to content
Open
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
2 changes: 2 additions & 0 deletions mlx_lm/tokenizer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,8 @@ def _infer_tool_parser(tokenizer):
return "qwen3_coder"
elif "<|tool_calls_section_begin|>" in chat_template:
return "kimi_k2"
elif '<function name="' in chat_template and '<param name="' in chat_template:
return "minicpm5"
elif "[TOOL_CALLS]" in chat_template:
return "mistral"
elif "<tool_call>" in chat_template and "tool_call.name" in chat_template:
Expand Down
74 changes: 74 additions & 0 deletions mlx_lm/tool_parsers/minicpm5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Copyright © 2026 Apple Inc.

"""
Tool call parser for OpenBMB MiniCPM5.

The chat template asks the model for
``<function name="fn"><param name="p">value</param></function>`` with no
outer wrapper. Values containing ``<``, ``&`` or newlines are wrapped in a
CDATA block, and parallel calls are consecutive ``<function>`` blocks.
"""

from typing import Any, Optional

import regex as re

from .minimax_m2 import (
_convert_param_value_with_types,
_extract_name,
_get_param_types_from_config,
)

tool_call_start = "<function name="
tool_call_end = "</function>"

# The state machine strips both markers, so the server hands the parser
# ``"fn"><param ...>...``; the leading ``<function name=`` is optional here
# so the same parser also accepts the full text.
_function_regex = re.compile(
r"(?:<function\s+)?(?:name\s*=\s*)?"
r"(?P<name>\"[^\"]*\"|'[^']*'|[^\s\"'<>]+)\s*>"
r"(?P<body>.*?)(?:</function>|$)",
re.DOTALL,
)
_param_regex = re.compile(
r"<param\s+name\s*=\s*(?P<name>\"[^\"]*\"|'[^']*'|[^\s\"'<>]+)\s*>"
r"(?P<value>.*?)</param>",
re.DOTALL,
)
_cdata_regex = re.compile(r"^\s*<!\[CDATA\[(.*?)\]\]>\s*$", re.DOTALL)


def _param_value(raw: str) -> str:
if (match := _cdata_regex.match(raw)) is not None:
return match.group(1)
return raw.strip()


def parse_tool_call(model_output: str, tools: Optional[Any] = None):
function_matches = list(_function_regex.finditer(model_output))
if not function_matches:
raise ValueError("No function provided.")

param_config_for = {}
for tool in tools or []:
if function := tool.get("function", False):
if params := function.get("parameters", False):
param_config_for[function["name"]] = params.get("properties", {})

calls = []
for function_match in function_matches:
function_name = _extract_name(function_match.group("name"))
param_config = param_config_for.get(function_name, {})
arguments = {}
for param_match in _param_regex.finditer(function_match.group("body")):
param_name = _extract_name(param_match.group("name"))
arguments[param_name] = _convert_param_value_with_types(
_param_value(param_match.group("value")),
_get_param_types_from_config(param_name, param_config),
)
calls.append(dict(name=function_name, arguments=arguments))

if len(calls) == 1:
return calls[0]
return calls
121 changes: 121 additions & 0 deletions tests/test_tool_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
kimi_k2,
kimi_k3,
longcat,
minicpm5,
minimax_m2,
mistral,
pythonic,
Expand Down Expand Up @@ -39,6 +40,10 @@ def test_parsers(self):
'<invoke name="multiply">\n<parameter name="a">12234585</parameter>\n<parameter name="b">48838483920</parameter>\n</invoke>',
minimax_m2,
),
(
'<function name="multiply"><param name="a">12234585</param><param name="b">48838483920</param></function>',
minicpm5,
),
(
"<function=multiply>\n<parameter=a>\n12234585\n</parameter>\n<parameter=b>\n48838483920\n</parameter>\n</function>",
qwen3_coder,
Expand Down Expand Up @@ -109,6 +114,10 @@ def test_parsers(self):
'<invoke name="get_current_temperature">\n<parameter name="location">London</parameter>\n</invoke>',
minimax_m2,
),
(
'<function name="get_current_temperature"><param name="location">London</param></function>',
minicpm5,
),
(
"<function=get_current_temperature>\n<parameter=location>\nLondon\n</parameter>\n</function>",
qwen3_coder,
Expand Down Expand Up @@ -577,6 +586,118 @@ def test_kimi_k3(self):
'<|open|>call index="1"<|sep|><|close|>call<|sep|>', None
)

def test_minicpm5(self):
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"},
"days": {"type": "integer"},
"metric": {"type": "boolean"},
},
},
},
},
{
"type": "function",
"function": {
"name": "write_file",
"parameters": {
"type": "object",
"properties": {
"path": {"type": "string"},
"content": {"type": "string"},
},
},
},
},
]

# What the server passes: both markers stripped by the state machine.
tool_call = minicpm5.parse_tool_call(
'"get_weather"><param name="city">Paris</param>'
'<param name="days">3</param><param name="metric">true</param>',
tools,
)
self.assertEqual(
tool_call,
{
"name": "get_weather",
"arguments": {"city": "Paris", "days": 3, "metric": True},
},
)

# Single quotes and whitespace around the tags.
tool_call = minicpm5.parse_tool_call(
"<function name='get_weather'>\n<param name='city'> Paris </param>\n</function>",
tools,
)
self.assertEqual(
tool_call, {"name": "get_weather", "arguments": {"city": "Paris"}}
)

# CDATA keeps the value verbatim, including newlines and markup.
tool_call = minicpm5.parse_tool_call(
'<function name="write_file"><param name="path">a.txt</param>'
'<param name="content"><![CDATA[line 1\n<b>&</b>\n]]></param></function>',
tools,
)
self.assertEqual(
tool_call,
{
"name": "write_file",
"arguments": {"path": "a.txt", "content": "line 1\n<b>&</b>\n"},
},
)

# No schema: values stay strings.
tool_call = minicpm5.parse_tool_call(
'<function name="get_weather"><param name="days">3</param></function>'
)
self.assertEqual(tool_call, {"name": "get_weather", "arguments": {"days": "3"}})

# Parallel calls are consecutive blocks.
tool_calls = minicpm5.parse_tool_call(
'<function name="get_weather"><param name="city">Tokyo</param></function>\n'
'<function name="write_file"><param name="path">b.txt</param></function>',
tools,
)
self.assertEqual(
tool_calls,
[
{"name": "get_weather", "arguments": {"city": "Tokyo"}},
{"name": "write_file", "arguments": {"path": "b.txt"}},
],
)

# Truncated by max_tokens: no closing tag, the complete params survive.
tool_call = minicpm5.parse_tool_call(
'"get_weather"><param name="city">Paris</param><param name="days">3',
tools,
)
self.assertEqual(
tool_call, {"name": "get_weather", "arguments": {"city": "Paris"}}
)

# No parameters, an empty value, and dotted/hyphenated names.
self.assertEqual(
minicpm5.parse_tool_call('<function name="get_weather"></function>'),
{"name": "get_weather", "arguments": {}},
)
self.assertEqual(
minicpm5.parse_tool_call(
'<function name="fs.read-file"><param name="path"></param></function>'
),
{"name": "fs.read-file", "arguments": {"path": ""}},
)

with self.assertRaises(ValueError):
minicpm5.parse_tool_call("no call here", tools)

def test_minimax_m2(self):
test_case = (
'<invoke name="search">\n'
Expand Down