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
4 changes: 3 additions & 1 deletion mlx_lm/tokenizer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,9 @@ def _infer_tool_parser(chat_template):
return "longcat"
elif "<arg_key>" in chat_template:
return "glm47"
elif "<|tool_list_start|>" in chat_template:
elif (
"<|tool_list_start|>" in chat_template or "<|tool_call_start|>" in chat_template
):
return "pythonic"
elif (
"<tool_call>\\n<function=" in chat_template
Expand Down
25 changes: 25 additions & 0 deletions tests/test_tokenizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
BPEStreamingDetokenizer,
NaiveStreamingDetokenizer,
SPMStreamingDetokenizer,
_infer_tool_parser,
)
from mlx_lm.utils import load_tokenizer

Expand Down Expand Up @@ -109,6 +110,30 @@ def test_thinking(self):
self.assertIsNone(tokenizer.think_start_id)
self.assertIsNone(tokenizer.think_end_id)

def test_infer_tool_parser(self):
# LFM2 (original) uses <|tool_list_start|> ... <|tool_list_end|>.
lfm2_template = (
"{%- if tools %}<|tool_list_start|>{{ tools | tojson }}"
"<|tool_list_end|>{% endif -%}"
)
self.assertEqual(_infer_tool_parser(lfm2_template), "pythonic")

# LFM2.5 renamed the tokens to <|tool_call_start|> ... <|tool_call_end|>
# while keeping the same pythonic call format inside.
# See https://huggingface.co/LiquidAI/LFM2.5-8B-A1B for the template.
lfm25_template = (
"{%- macro render_tool_calls(tool_calls) -%}"
"{{- '<|tool_call_start|>[' + "
"(tool_calls_ns.tool_calls | join(', ')) + "
"']<|tool_call_end|>' -}}"
"{%- endmacro -%}"
)
self.assertEqual(_infer_tool_parser(lfm25_template), "pythonic")

# Sanity: a template with neither token should not match pythonic.
self.assertIsNone(_infer_tool_parser("plain template, no tool tokens"))
self.assertIsNone(_infer_tool_parser(None))


if __name__ == "__main__":
unittest.main()