Skip to content

Add tool call parser for MiniCPM5 - #1899

Open
dasashreeya wants to merge 1 commit into
ml-explore:mainfrom
dasashreeya:feat/minicpm5-tool-parser
Open

dasashreeya wants to merge 1 commit into
ml-explore:mainfrom
dasashreeya:feat/minicpm5-tool-parser

Conversation

@dasashreeya

Copy link
Copy Markdown
Contributor

Fixes #1885

openbmb/MiniCPM5-1B-MLX ships a chat template that asks the model for <function name="fn"><param name="p">value</param></function> with no outer wrapper. _infer_tool_parser in tokenizer_utils.py picks a parser by substring-matching the template and has no branch for that format, so has_tool_calling is False, the server logs "Received tools but model does not support tool calling", and the raw XML comes back in content with finish_reason: "stop".

mlx_lm.server --model openbmb/MiniCPM5-1B-MLX
# any /v1/chat/completions request with `tools`

On main: the warning, tool_calls: null, and<function name="get_weather"><param name="city">Paris</param>…</function> incontent. With this change: finish_reason: "tool_calls" and tool_calls: [{"name": "get_weather", "arguments": "{\"city\": \"Paris\", \"unit\": \"c\"}"}], in both streaming and non-streaming mode. Parallel calls (consecutive <function> blocks) come back as two entries, thinking mode still separates reasoning from the call, and a tool-result round trip renders the assistant tool_calls history correctly through the template.

The parser sets tool_call_start = "<function name=" and tool_call_end = "</function>", unwraps <![CDATA[...]]> values (the template's escape for <, & and newlines), accepts single or double quotes, and types arguments from the tool schema using the helpers already in minimax_m2 (same pattern as laguna reusing glm47). Detection is '<function name="' in chat_template and '<param name="' in chat_template; no other template in the repo's parsers uses those markers.

Tests: minicpm5 added to the two generic cases in test_tool_parsing.py, plus test_minicpm5 covering the marker-stripped text the server passes, quotes and whitespace, CDATA, no schema, parallel blocks, output truncated by max_tokens, no params, empty values, and the no-call error. They fail on main (the module does not exist) and pass here; the full suite passes (312 passed, 1 skipped) and pre-commit is clean.

Per CONTRIBUTING: I directed the investigation, code, and tests using Claude Code and verified the behavior end-to-end against the model on my M4 Pro. I reviewed the diff and take responsibility for every line.

MiniCPM5's chat template asks for
<function name="fn"><param name="p">value</param></function> with no outer
wrapper, which _infer_tool_parser did not recognize, so mlx_lm.server warned
that the model does not support tool calling and returned the raw XML as
content. Add a parser for that format (CDATA values, single or double
quotes, parallel calls as consecutive blocks) and detect it from the
template.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MiniCPM5-1B-MLX tool calling not supported by mlx_lm.server

1 participant