Auto-discover tool-call markers from tokenizer config fields#1163
Open
michaelstingl wants to merge 1 commit intoml-explore:mainfrom
Open
Auto-discover tool-call markers from tokenizer config fields#1163michaelstingl wants to merge 1 commit intoml-explore:mainfrom
michaelstingl wants to merge 1 commit intoml-explore:mainfrom
Conversation
Gemma 4 publishes structured token fields in tokenizer_config.json (stc_token / etc_token for tool calls) that HuggingFace's AutoTokenizer exposes as attributes. Previously mlx-lm could only reach these markers via hardcoded pattern matching in _infer_tool_parser(). Add _infer_markers_from_config() which reads stc_token / etc_token directly from the tokenizer. Parser-module markers still take precedence when a parser is matched; config-discovered markers fall back to enable state-machine streaming for models that ship the fields but do not yet have a dedicated parser module. Also add think_start / think_end parameters to TokenizerWrapper as infrastructure for a future thinking-marker auto-discovery pass (soc_token / eoc_token semantics are deferred until more models adopt the convention). Ref: https://ai.google.dev/gemma/docs/core/prompt-formatting-gemma4
3 tasks
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
Google's Gemma 4 publishes structured special-token fields in
tokenizer_config.json(stc_token/etc_tokenfor tool calls,soc_token/eoc_tokenfor the thinking channel). HuggingFace'sAutoTokenizerexposes these as attributes on the tokenizer object, but nothing in mlx-lm currently consumes them. Tool-call marker discovery relies entirely on the hardcoded_infer_tool_parser()pattern chain.This PR adds a small, additive layer that reads
stc_token/etc_tokendirectly from the tokenizer and feeds the discovered markers into the existingSequenceStateMachineplumbing inserver.py. The intent is that if other model authors adopt the same convention, they will get marker detection for free without requiring a new entry in_infer_tool_parser().Reference: https://ai.google.dev/gemma/docs/core/prompt-formatting-gemma4
What changes
mlx_lm/tokenizer_utils.py_infer_markers_from_config(tokenizer)— readsstc_token/etc_tokenviagetattr, returns a dict of marker strings (orNone).TokenizerWrapper.__init__gains optionalthink_start/think_endkwargs. When passed, they bypass_infer_thinking(); otherwise the existing fallback runs unchanged.load()calls_infer_markers_from_config()once. When a tool parser module is matched via_infer_tool_parser(), its markers still win (unchanged behaviour). When no parser matches but the tokenizer exposesstc_token/etc_token, those markers are used — the state machine can then segregate tool-call content from normal content even without a structured parser.tests/test_tokenizers.pyTestMarkerDiscoveryclass (6 tests) covering the discovery function, the new wrapper kwargs, and the parser-precedence guarantee.No changes to
server.py,generate.py, or any tool-parser module. This is purely a discovery-layer addition.Why tool markers only (thinking deferred)
The
stc_token/etc_tokenpair is semantically unambiguous: anything between them is a tool call.soc_token/eoc_tokenare trickier — Gemma 4'ssoc_tokenis<|channel>, but the actual thinking-start sequence is the multi-token<|channel>thought(channels can have other labels too)._infer_thinking()already handles that case correctly. Auto-discovering thinking markers fromsoc_tokenwould require model-specific heuristics, which defeats the purpose. Thethink_start/think_endkwargs onTokenizerWrapperare there as infrastructure so a future PR can plug in a cleaner convention once more models adopt one.Backwards compatibility
The new
elifbranch inload()is only reachable when_infer_tool_parser()returnsNoneand the tokenizer exposes the Gemma-4-style fields. For every currently-supported parser model this branch is a no-op; the parser's markers continue to win.test_parser_markers_take_precedence(using Qwen3-4B-4bit) exercises this explicitly.Test plan
pre-commit run --files mlx_lm/tokenizer_utils.py tests/test_tokenizers.py— black + isort cleanpython -m pytest tests/test_tool_parsing.py tests/test_tokenizers.py— 14 passed (8 existing + 6 new)python -m pytest tests/test_server.py tests/test_prompt_cache.py— all passedpython -m pytest tests/test_generate.py— identical pass/fail set asmain(the 4 failing tests intest_generate.py::TestGenerate::test_many_batches/test_batch_continued_generation*fail pre-existing onmainin the same local environment, likely due to the missingtest_data.zipartifact the CI downloads)TestMarkerDiscoverytests use a plain stub tokenizer (noMagicMockpatching) to keep them hermetictest_parser_markers_take_precedence) confirms parser-module markers still take precedence🤖 drafted with Claude Code, reviewed before submitting.