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: 2 additions & 2 deletions mlx_lm/tool_parsers/json_tools.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright © 2025 Apple Inc.

import json
import json_repair

tool_call_start = "<tool_call>"

tool_call_end = "</tool_call>"


def parse_tool_call(text, tools=None):
return json.loads(text.strip())
return json_repair.loads(text.strip())
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"protobuf",
"pyyaml",
"jinja2",
"json-repair>=0.58.7",
],
packages=[
"mlx_lm",
Expand Down
68 changes: 68 additions & 0 deletions tests/test_tool_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,74 @@ def test_parsers(self):
}
self.assertEqual(tool_call, expected)

def test_json_tools_repairs_malformed_json(self):
tools_multiply = [
{
"type": "function",
"function": {
"name": "multiply",
"description": "Multiply two numbers.",
"parameters": {
"type": "object",
"required": ["a", "b"],
"properties": {
"a": {"type": "number", "description": "a is a number"},
"b": {"type": "number", "description": "b is a number"},
},
},
},
}
]
expected_multiply = {
"name": "multiply",
"arguments": {"a": 12234585, "b": 48838483920},
}
malformed_multiply = [
(
"trailing_comma",
'{"name": "multiply", "arguments": {"a": 12234585, "b": 48838483920,},}',
),
(
"single_quoted",
"{'name': 'multiply', 'arguments': {'a': 12234585, 'b': 48838483920}}",
),
]
for label, text in malformed_multiply:
with self.subTest(case=label):
self.assertEqual(
json_tools.parse_tool_call(text, tools_multiply),
expected_multiply,
)

tools_temp = [
{
"type": "function",
"function": {
"name": "get_current_temperature",
"description": "Get the current temperature.",
"parameters": {
"type": "object",
"required": ["location"],
"properties": {
"location": {"type": "str", "description": "The location."},
},
},
},
}
]
expected_temp = {
"name": "get_current_temperature",
"arguments": {"location": "London"},
}
text = (
'{"name": "get_current_temperature", '
'"arguments": {"location": "London",},}'
)
self.assertEqual(
json_tools.parse_tool_call(text, tools_temp),
expected_temp,
)

def test_qwen3_coder_single_quoted_params(self):
tools = [
{
Expand Down