fix(python-backends): parse tool-call arguments for chat templates and split implicit reasoning blocks#10658
Merged
Merged
Conversation
…d split implicit reasoning blocks
Two bugs broke OpenAI-style tool calling on the MLX backend (and any
Python backend sharing backend/python/common), reproduced end-to-end on
LocalAI v4.5.5 with the metal-mlx backend and
mlx-community/Qwen3.5-2B-MLX-8bit.
messages_to_dicts left each tool call's function.arguments as the raw
OpenAI-wire JSON string. HuggingFace chat templates (e.g. Qwen3.5)
iterate arguments as a mapping (.items()), so any request whose history
contained a prior assistant tool_calls message failed with HTTP 500
"Generation failed: Can only get item pairs from a mapping." — breaking
every agent loop on its second turn. Decode the string back into a dict
so the template sees a mapping.
split_reasoning returned ("", text) whenever the opening think tag was
absent. Models like Qwen3.5 open the assistant turn already inside
thinking, so the generated text carries only the closing </think>; the
whole chain-of-thought leaked into content. When the opener is missing
but the closer is present, treat everything before the closer as
reasoning.
Adds platform-independent unit tests under backend/python/common
(stdlib-only, no MLX/venv required, following parent_watch_test.py).
Assisted-by: Claude Code:claude-opus-4-8
mudler
approved these changes
Jul 3, 2026
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.
Two verified bugs broke OpenAI-style tool calling on the MLX backend — and any other Python backend sharing
backend/python/common. Both were found and fix-verified end-to-end today on a real M4 Mac (16GB) running LocalAI v4.5.5 with the metal-mlx backend and model mlx-community/Qwen3.5-2B-MLX-8bit, driven through Dante Desktop (nib). LocalAI master still carries the same code.Bug 1 — tool-call round-trip crashes: "Can only get item pairs from a mapping"
backend/python/common/python_utils.py→messages_to_dictsdidd["tool_calls"] = json.loads(msg.tool_calls)but left each tool call'sfunction.argumentsas a JSON string (that's the OpenAI wire format). HuggingFace chat templates (e.g. Qwen3.5's) iterateargumentsas a mapping (.items()), so any request whose history contains a prior assistanttool_callsmessage failed with:This broke every agent loop on the second turn. Fix: after decoding
tool_calls, decode eachfunction.argumentsstring back into a dict so the template sees a mapping. Idempotent when it's already a dict; invalid-JSON arguments are left untouched.Bug 2 — reasoning leaks into content when the opening think tag is implicit
backend/python/common/mlx_utils.py→split_reasoningreturned("", text)wheneverthink_startwas absent. But models like Qwen3.5 open the assistant turn already inside thinking — the generated text contains only the closing</think>, never the opener — so the whole chain-of-thought leaked intocontent. Observed:Fix: when
think_startis absent butthink_endis present, treat everything beforethink_endas reasoning and the remainder as content.Verification
Reproduced and fixed end-to-end on LocalAI v4.5.5 / metal-mlx / mlx-community/Qwen3.5-2B-MLX-8bit (M4 Mac 16GB, via Dante Desktop/nib): tool-call emission, the round-trip with a tool result no longer 500s, and reasoning now lands in the reasoning field instead of leaking into content.
Tests
Adds platform-independent unit tests under
backend/python/common(stdlib-only, no MLX/venv required, following the existingparent_watch_test.pypattern) — the canonical helper tests inbackend/python/mlx/test.py::TestSharedHelperscan't run off-Mac because that module importsgrpc/backend_pb2at import time. TDD: both new assertions fail against the unpatched code and pass with the fix.python_utils_test.py: arguments string → mapping; arguments already a dict (idempotent); invalid-JSON arguments left as string; tool call without afunctionkey; invalidtool_callsJSON dropped.mlx_utils_test.py: implicit opener (only</think>present); both tags (unchanged); neither tag; emptythink_endwith no opener match; empty text.Run:
🤖 Generated with Claude Code