chore: Use json-repair for json_tools tool-call parsing#1071
Open
isaac-cf-wong wants to merge 3 commits intoml-explore:mainfrom
Open
chore: Use json-repair for json_tools tool-call parsing#1071isaac-cf-wong wants to merge 3 commits intoml-explore:mainfrom
json-repair for json_tools tool-call parsing#1071isaac-cf-wong wants to merge 3 commits intoml-explore:mainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
json_toolsparser now usesjson_repair.loads()instead of strict JSON parsing so common LLM mistakes (trailing commas, single-quoted strings, etc.) still produce usable tool calls.Motivation
Chat templates that emit a JSON object inside
<tool_call>often get invalid JSON from the model (I encountered this issue frequently when usingmlx-community/Qwen3.5-27B-Claude-4.6-Opus-Distilled-MLX-4bit).json.loadsfails on those outputs;json-repairis aimed at repairing that kind of text.Changes
mlx_lm/tool_parsers/json_tools.py:parse_tool_callparses the stripped payload with json_repair.loads.setup.py: Declare dependencyjson-repair>=0.58.7(latest version).tests/test_tool_parsing.py: Addtest_json_tools_repairs_malformed_jsoncovering trailing commas and single-quoted JSON for the multiply and temperature fixtures. Existingtest_parserscases still assert valid JSON forjson_tools.Tradeoffs
Parsing is more lenient than before: some malformed text may be repaired into a JSON object instead of raising, which could occasionally yield unexpected
name / arguments. That is usually acceptable for tool-call extraction but is a behavioral change from strictjson.loads.Testing