Fix tool calls dropped when closed by EOS without an end marker - #1373
Closed
rayanhanader wants to merge 1 commit into
Closed
Fix tool calls dropped when closed by EOS without an end marker#1373rayanhanader wants to merge 1 commit into
rayanhanader wants to merge 1 commit into
Conversation
This was referenced Jun 8, 2026
mabaeyens
added a commit
to mabaeyens/mlx-lm
that referenced
this pull request
Jul 18, 2026
…marker Mistral's tool_call_end_tokens is empty (one-sided [TOOL_CALLS] marker), so EOS is the only exit from the tool state, and it transitions to None, not back to tool. The post-loop flush guard (prev_state == tool) then never fires, silently dropping the buffered tool call and returning an empty message with finish_reason: stop. Matches ml-explore#1373 (upstream PR for ml-explore#1307). Cherry-picked here ahead of upstream merge so mira-core has a durable, version-pinned source instead of a hand-patch to the installed package.
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.
What
When a model with no tool-call end marker (the Mistral family, where
tool_call_end_tokensis empty) emits a tool call,mlx_lm.serverreturns an empty assistant message withfinish_reason: "stop", even though the model generated a valid[TOOL_CALLS] ...sequence andcompletion_tokensis non-zero.Why
The state machine transitions
normal -> toolon the[TOOL_CALLS]token and buffers everything after it intotool_text. With no end marker the tool call is closed by the EOS token, which transitionstool -> None, so the last token of the generation reports its state asNone.The flush after the accumulation loop in
handle_completionis guarded byprev_state == "tool", which isNoneat that point, so the buffered tool call is never appended.made_tool_callstaysFalse,finish_reasonstays"stop", and the message comes back empty. The parser is never reached, which is why there is noFailed to parse tool callwarning either.Fix
Flush whenever
tool_textis non-empty. It can only be non-empty when a tool call was opened and not yet flushed, so this is also safe for models that do have an end marker, since theirtool_textis reset to""on thetool -> normaltransition.Test
Added
test_tool_call_without_end_marker_is_not_droppedintests/test_server.py. It mocks generation with a scripted token stream that ends in the EOS toNonetransition and asserts the tool call is returned withfinish_reason: "tool_calls". It fails onmainand passes with the fix. The fulltest_server.pysuite passes andpre-commit(black, isort) is clean.Notes
I could not run the 24B model from the report on a 16 GB machine, so I reproduced the empty-message behavior on
Ministral-8B-Instruct-2410-4bit(same emptytool_call_end_tokens). To confirm the report's model is the same case, I checked the tokenizer and chat template ofmlx-community/mistralai_Devstral-Small-2-24B-Instruct-2512-MLX-MXFP4:tool_call_end_tokensis empty, the template renders tool calls as[TOOL_CALLS]<name>[ARGS]<json_args>which the currentmistral.pyparser already handles, and there is no tool-call-id length constraint. So for that model this is purely the flush bug.Fixes #1307.