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
3 changes: 2 additions & 1 deletion ACKNOWLEDGMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Z.ai & THUKEG's `GLM5 (GLM MoE DSA)`
- Jonathan Springer: Added architecture-specific tool parser for IBM Granite models
14 changes: 14 additions & 0 deletions mlx_lm/tool_parsers/granite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json

tool_call_start = "<tool_call>"

tool_call_end = "</tool_call>"


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
21 changes: 21 additions & 0 deletions tests/test_tool_parsing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import json
import unittest

from mlx_lm.tool_parsers import (
function_gemma,
gemma4,
glm47,
granite,
json_tools,
kimi_k2,
longcat,
Expand Down Expand Up @@ -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<|"|>}}'
Expand Down