Tool calls dropped: json_tools start marker never matches when the tokenizer merges the `<tool_call>` closing `>`
Summary
On the OpenAI-compatible server, tool calls from a model that emits the standard Hermes shape <tool_call>\n{...}\n</tool_call> are returned as assistant content with tool_calls = null, even though tokenizer.has_tool_calling is true and tool_parser_type = json_tools. The model's output is valid (correct JSON, right function/args) — it just never reaches the parser.
Root cause: tool-call detection matches markers as exact token-id sequences, and json_tools's start marker "<tool_call>" encodes to a run ending in a standalone > token that the tokenizer merges with the following byte at generation time.
Environment
- mlx-lm
main(df1d3f3), mlx 0.31.2, Apple M4 Max / macOS 26.3 mlx-community/DeepSeek-V4-Flash-2bit-DQ(any model where<tool_call>/</tool_call>are not single special tokens and the tokenizer merges>)
What happens
POST /v1/chat/completions {tools:[get_weather], messages:[{user:"weather in Tokyo?"}]}
-> finish_reason: stop
tool_calls: None
content: '<tool_call>\n{"name": "get_weather", "arguments": {"city": "Tokyo"}}\n</tool_call>'Root cause (with token IDs)
server.py:_make_state_machine wires normal --tool_call_start_tokens--> tool, and SequenceStateMachine (generate.py) matches those token-id sequences with an Aho-Corasick trie. json_tools.tool_call_start = "<tool_call>", encoded in isolation:
encode("<tool_call>") = (30, 72461, 112042, 32) # < tool _call > (trailing ">" = token 32)But the > only survives as a standalone token at end-of-string. With any following byte it merges:
encode("<tool_call>\n") = (30, 72461, 112042, 1018) # ">\n" is ONE token
encode("<tool_call>{") = (30, 72461, 112042, 31923) # ">{" is ONE tokenThe Hermes convention puts \n right after <tool_call>, so the model emits the merged >\n token and the precomputed marker (…, 32) is never a subsequence of the stream → the state machine stays in normal → the call is emitted as content.
Reproducer (deterministic, tokenizer-only, no GPU)
from pathlib import Path
from mlx_lm.tokenizer_utils import load as load_tokenizer
from mlx_lm.generate import SequenceStateMachine
tok = load_tokenizer(Path(MODEL)) # a model with non-special-token <tool_call> markers
enc = lambda s: tuple(tok.encode(s, add_special_tokens=False))
STOCK, PREFIX, END = enc("<tool_call>"), enc("<tool_call"), enc("</tool_call>")
emitted = enc('<tool_call>\n{"name":"get_weather","arguments":{"city":"Paris"}}\n</tool_call>')
def enters_tool(start):
sm = SequenceStateMachine({"normal":[(start,"tool")], "tool":[(END,"normal")]}, initial="normal")
st, seen = sm.make_state(), set()
for x in emitted:
st, _, s = SequenceStateMachine.match(st, x); seen.add(s)
return "tool" in seen
print(enters_tool(STOCK)) # False <- bug: stock "<tool_call>" marker never matches
print(enters_tool(PREFIX)) # True <- "<tool_call" prefix matchesWhy it's gone unnoticed
Models that use json_tools successfully (Qwen2.5 / Hermes-style) either register <tool_call> so the runtime tokens match, or their tokenizer doesn't merge > with the newline. It bites tokenizers where <tool_call> is an ordinary multi-token string and > merges — increasingly common as new checkpoints get OpenAI-style tool templates added at conversion time.
Proposed fix
Match the stable <tool_call prefix (drop the volatile >) and extract the first brace-balanced JSON object (tolerating the leftover >/newline and a trailing </tool_call>). Output is identical to json.loads(text.strip()) for clean segments; streaming is unaffected (the server accumulates the whole tool segment before parsing). PR: https://github.com/ml-explore/mlx-lm/pull/1336.
Full analysis: writeup. On DeepSeek-V4-Flash-2bit-DQ this took tool-call parsing from 0 → 82% on a tool-calling bench.
Source: ml-explore/mlx-lm