diff --git a/ACKNOWLEDGMENTS.md b/ACKNOWLEDGMENTS.md index 964053048..bfb7a3217 100644 --- a/ACKNOWLEDGMENTS.md +++ b/ACKNOWLEDGMENTS.md @@ -30,4 +30,5 @@ Added support for the following other features: - Tarjei Mandt: Added support for the following architectures: `Step 3.5 Flash`, MoonshotAI's `Kimi K2.5`, Upstage's `Solar Open`, LG AI Research's `K-Exaone MoE`, Meituan's `LongCat Flash Lite` Helped add support for the following model architectures: -Z.ai & THUKEG's `GLM5 (GLM MoE DSA)` \ No newline at end of file +Z.ai & THUKEG's `GLM5 (GLM MoE DSA)` +- Jonathan Springer: Added architecture-specific tool parser for IBM Granite models diff --git a/mlx_lm/tool_parsers/granite.py b/mlx_lm/tool_parsers/granite.py new file mode 100644 index 000000000..d695b4f63 --- /dev/null +++ b/mlx_lm/tool_parsers/granite.py @@ -0,0 +1,14 @@ +import json + +tool_call_start = "" + +tool_call_end = "" + + +def parse_tool_call(text, tools=None): + text = text.strip() + tool_call, end = json.JSONDecoder().raw_decode(text) + trailing = text[end:].strip() + if trailing and set(trailing) != {"}"}: + raise json.JSONDecodeError("Unexpected trailing data", text, end) + return tool_call diff --git a/tests/test_tool_parsing.py b/tests/test_tool_parsing.py index d1301124c..35433b7b1 100644 --- a/tests/test_tool_parsing.py +++ b/tests/test_tool_parsing.py @@ -1,9 +1,11 @@ +import json import unittest from mlx_lm.tool_parsers import ( function_gemma, gemma4, glm47, + granite, json_tools, kimi_k2, longcat, @@ -209,6 +211,25 @@ def test_qwen3_coder_single_quoted_params(self): self.assertEqual(tool_call["arguments"]["filters"], {"category": "books"}) self.assertEqual(tool_call["arguments"]["tags"], ["fiction", "new"]) + def test_granite(self): + test_cases = [ + '{"name": "get_current_weather", "arguments": {"city": "Boston"}}', + '{"name": "get_current_weather", "arguments": {"city": "Boston"}}}', + '{"name": "get_current_weather", "arguments": {"city": "Boston"}}}}}}', + '{"name": "get_current_weather", "arguments": {"location": {"city": "Boston"}}}}', + ] + + for test_case in test_cases: + with self.subTest(test_case=test_case): + tool_call = granite.parse_tool_call(test_case, None) + self.assertEqual(tool_call["name"], "get_current_weather") + self.assertIn("city", str(tool_call["arguments"])) + + with self.assertRaises(json.JSONDecodeError): + granite.parse_tool_call( + '{"name": "get_current_weather", "arguments": {}} trailing junk', None + ) + def test_gemma4(self): # Nested object test_case = 'call:configure{settings:{enabled:true,name:<|"|>test<|"|>}}'