Repository navigation
fix(provider): decode Gemma tool-call arguments before chat-template rendering (#249) - #250
Conversation
…rendering Fixes #249. Multi-turn tool calling with Gemma (gemma-4-26b) corrupted function.arguments after the first turn. Rendering a prior assistant `tool_calls` message fed the JSON-string `arguments` into the model's downloaded chat_template.jinja, which took the `is string` branch and emitted a double brace (`call:run_terminal{{"command":"ls -la"}}`). The model imitated that shape on the next turn and the Gemma output parser shredded it, corrupting arguments and compounding turn over turn. Affects every OpenAI-shaped tool-calling SDK. Provider fix: `templateMessageDict()` — the live chat path that feeds `applyChatTemplate` — now decodes the JSON-string arguments into an object via `MLXLMCommon.decodeToolCallArguments`, so the template takes the `is mapping` branch (`command:<|"|>ls -la<|"|>`). The OpenAI wire contract is unchanged; non-JSON / non-object arguments fall back to the raw string. The chat template and the output parser are left untouched. Bumps the mlx-swift-lm submodule to include the shared `decodeToolCallArguments` helper and the MLXLMServer mirror fix (Layr-Labs/mlx-swift-lm#33). Tests: ProviderCoreTests/ToolCallTemplateArgumentsTests asserts templateMessageDict() yields a mapping (double-brace regression guard) and keeps non-JSON args as a string. Verified with a negative control (revert -> test fails with the raw string).
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This change is security-neutral with respect to the threat model, but introduces a small new attack surface worth noting. Trust Boundaries Touched
Threat Assessment
New Attack Surface Not Covered by an Existing Threat
The function Two concerns:
The function definition is not included in the diff — it presumably lives in
Open Findings ResolvedNone. This PR does not resolve any existing SEC-* findings. 🔐 Threat model: |
Adds a gated end-to-end test against the real
`mlx-community/gemma-4-26b-a4b-it-8bit` model and its real downloaded
`chat_template.jinja` (rendered by swift-jinja):
- Prompt A/B: rendering a prior assistant tool_call through the real template
produces a single brace via the fixed translation
(`call:run_terminal{command:<|"|>ls -la<|"|>}`) but a corrupting double brace
when arguments are forwarded as the raw JSON string
(`call:run_terminal{{"command":"ls -la"}}`).
- Generation: with the fixed multi-turn history, the real model emits and the
server parses a clean `run_terminal(command: "cat hello.txt")` — no
double-brace, no split-at-colon key corruption.
Gated by DARKBLOOM_LIVE_MLX_TESTS=1 + DARKBLOOM_LIVE_MLX_GEMMA=1 (and a
metallib via MLX_METALLIB_SOURCE/.build); skipped in normal CI.
Refs #249.
Summary
Fixes #249 — multi-turn tool calling with Gemma (
gemma-4-26b) corruptsfunction.argumentsafter the first turn for every OpenAI-shaped tool-callingSDK (Claude Code, Codex, AI SDK, OpenAI SDK, …).
OpenAI delivers
function.argumentsas a JSON string. When the providerrenders a prior assistant
tool_callsmessage into the prompt, that string isforwarded verbatim into the model's downloaded
chat_template.jinja, whichtakes the
is stringbranch and dumps it inside its own{ … }block →double brace:
The model imitates that double-brace example on the next turn, and the Gemma
output parser then splits the inner object at its first colon, corrupting the
arguments (
{"command":"ls -la"}→{"{\"command\"":"\"ls -la\"}"}) andcompounding turn over turn.
Confirmed against the exact template the fleet runs: the prod runtime manifest
pins the
gemma4chat template to hash2dfbfc7d…, which matches the downloadedgemma-4-26b-a4b-it-8bitchat_template.jinja(lines 200-203 = the buggyis stringbranch). Note the bug is template-family-wide (the 4bit template hasthe same branch) — the root cause is ours, not the quant.
Fix
Decode the JSON-string
argumentsinto an object at the translation boundary sothe template takes the
is mappingbranch (command:<|"|>ls -la<|"|>). TheOpenAI wire contract is unchanged —
argumentsstays aStringto/fromclients; only the template-input shape is normalized. Non-JSON / non-object
arguments fall back to the raw string. The chat template and the output parser
are intentionally left untouched.
MultiModelBatchSchedulerEngine+Translation.swifttemplateMessageDict()— the liveapplyChatTemplatepath — now callsMLXLMCommon.decodeToolCallArguments.libs/mlx-swift-lm→ the shareddecodeToolCallArgumentshelper + the
MLXLMServermirror fix (fix(server): decode OpenAI tool-call arguments before chat-template rendering mlx-swift-lm#33).Tests
provider-swift/Tests/ProviderCoreTests/ToolCallTemplateArgumentsTests.swift:asserts
templateMessageDict()yields a mapping (double-brace regression guard)and keeps non-JSON args as a string.
swift testgreen locally; verified with anegative control (reverting the one-line change makes the test fail with the raw
{"command":"ls -la"}string).Merge order⚠️
libs/mlx-swift-lmgitlink here to the mergedmainSHA(this PR currently pins the PR-33 branch commit
2ebc20fso CI can build).If Security: Free inference via streaming — billing happens after response delivery #33 is squash-merged, update the submodule pointer before merging this PR.
Need help on this PR? Tag
@codesmithwith what you need. Autofix is disabled.