Skip to content

[Bug]: gemma4 tool-call format produces degenerate output on natural-language prompts (reproduces at 4-bit AND 8-bit weights) #69

Closed

Description

@psperera

Summary

When --model is a Gemma-4 checkpoint (SwiftLM detects Inferred tool call format: gemma4), requests that pass a tools array and a vague/natural-language user message cause the model to emit degenerate output — CJK/Korean tokens, repetition cascades, or zero-length replies — roughly 2 runs in 3. The same request without tools produces coherent text, and the same request with tools but an explicit user message (Use bash to run: echo X) works 100%. This points at the gemma4 tool-call format implementation, not a model-quality issue.

Environment

  • SwiftLM: b517 (release tarball SwiftLM-b517-macos-arm64.tar.gz)
  • Hardware: Apple M4 Max, 64 GB unified memory, macOS 25.4.0 (Darwin 25.4.0)
  • Models tested (both exhibit the same failure pattern):
    • mlx-community/gemma-4-26b-a4b-it-4bit
    • mlx-community/gemma-4-26b-a4b-it-8bit
  • Flags: --port 5413 --host 127.0.0.1 --mem-limit 51200 (reproduces with or without --turbo-kv)
  • SwiftLM log on load: Inferred tool call format: Optional(MLXLMCommon.ToolCallFormat.gemma4)

Minimal repro

```bash
./SwiftLM --model mlx-community/gemma-4-26b-a4b-it-4bit --port 5413 --host 127.0.0.1 &

Vague natural-language prompt — fails ~67% of runs

for i in 1 2 3; do
curl -sS -X POST http://127.0.0.1:5413/v1/chat/completions \
-H 'Content-Type: application/json' \
-d '{
"messages":[{"role":"user","content":"what is the news"}],
"max_tokens":300, "temperature":0.7,
"tools":[{"type":"function","function":{"name":"web_search","description":"Search the web","parameters":{"type":"object","properties":{"query":{"type":"string"}},"required":["query"]}}}]
}' | python3 -c "import sys,json; d=json.load(sys.stdin); print('finish:', d['choices'][0]['finish_reason']); print(d['choices'][0]['message'].get('content','')[:200], d['choices'][0]['message'].get('tool_calls'))"
done
```

Observed output samples (verbatim from 3 runs)

  • Run A — `finish: stop`, no `tool_calls`, content = `"ing/sallyer/sallyer/sallyer/sallyer/..."` (800+ chars of repetition).
  • Run B — `finish: stop`, no `tool_calls`, content = 2 chars (essentially empty).
  • Run C — `finish: tool_calls`, clean `web_search({"query":"news today"})` ← works ~1/3.

Controls that narrow the bug to the gemma4 tool-call format

  1. Same request, no `tools` field → always produces a coherent English refusal ("I don't have access to real-time information…"). Isolates the bug to the tool-call code path.
  2. Same `tools`, explicit prompt (`"Use web_search to find news today"`) → 3/3 clean tool calls. The model can emit the tool-call structure reliably; the format just fails to steer the first-token distribution for natural-language queries.
  3. Reproduces at both 4-bit and 8-bit weights. When the 8-bit variant degenerates, the cascade is actually longer (hundreds of repeated tokens). Rules out low-bit weight fragility as the cause.
  4. Reproduces with/without `--turbo-kv` and across sampler combinations. Any distribution-clipping default tight enough to suppress the garbage tokens (e.g. `--top-p 0.95`, `--top-k 64`, `--min-p 0.05`) also suppresses the tool-call control tokens and breaks the explicit prompt case. The usable window between "stops gibberish" and "allows tool calls" appears to be zero with the current format. `--repeat-penalty 1.1` helps later-token repetition but does not change the first-token problem.

Hypothesis

The gemma4 tool-call format appears to produce a prompt shape where, at the "decide whether to tool-call" decision point for a vague user message, the first-token distribution has a broad, flat tail. Sampling (even at `temperature=0.7`) then frequently selects a low-probability junk token (commonly a Korean syllable or a sub-word like `/sallyer`), which cascades. Explicit prompts ("Use X to Y") sharpen the distribution onto the tool-call opener and bypass the failure mode.

Expected behavior

On any valid user message, the server should either emit a structured `tool_calls` response or coherent content. Degenerate output on natural-language inputs is not recoverable via sampler tuning.

Suggested next step

Comparing how `MLXLMCommon.ToolCallFormat.gemma4` builds the chat template for Gemma-4 (particularly how the tools schema is injected relative to the turn header and control tokens) against the Qwen / Hermes paths. Happy to gather additional traces, alternate prompts, or per-token timing if useful.

Activity

  1. solderzzc commented on Apr 21, 2026

    @solderzzc
    Member

    Hi, thanks for the detailed report! I was a bit delayed working on our new HomeSec benchmark suite (a standardized suite introduced to measure VLM/LLM response reliability and latency under various local home-security workloads), but we’ve now thoroughly investigated and fixed this regression!

    What was the result before and why?

    When evaluating mlx-community/gemma-4-26b-a4b-it-4bit, vague queries that provided tool schemas triggered complete degenerate failures (infinite padding loops, leaking <|channel>thought, or random foreign syllables like 야).

    We discovered that when a tool was provided but explicit thinking was disabled, the original chat_template.jinja inserted an "anti-thinking" prefix (<|channel>thought\n<channel|>). This artificially suppressed the reasoning channel. On vague queries, this constraint flattened out the first-token distribution right at the <|tool_call> decision layer causing the sampler (at temperature=0.7) to collapse and continuously sample garbage tokens.

    New Test Suite

    To prevent this pattern from surfacing again, we've integrated a dedicated Tool-Call Degeneration Regression Suite (Test 8) into run_benchmark.sh.

  2. solderzzc commented on Apr 21, 2026

    @solderzzc
    Member

    ./run_benchmark.sh

    Aegis-AI MLX Profiling Benchmark Suite    
    

    Select Action:
    0) Test 0: Run Full Automated Matrix (Offline Evaluation)

    1. Test 1: Automated Context & Memory Profile (TPS & RAM matrix)

    2. Test 2: Prompt Cache & Sliding Window Regression Test

    3. Test 3: HomeSec Benchmark (LLM Only)

    4. Test 4: VLM End-to-End Evaluation

    5. Test 5: ALM Audio End-to-End Evaluation

    6. Test 6: Omni End-to-End Evaluation

    7. Model Maintain List and Delete

    8. Test 8: Tool-Call Degeneration Regression (Gemma-4 vague-query bug)

    9. Quit
      Option (0-9): 8

    10. mlx-community/gemma-4-26b-a4b-it-8bit

    11. mlx-community/gemma-4-31b-it-8bit

    12. mlx-community/gemma-4-e4b-it-8bit

    13. mlx-community/gemma-4-26b-a4b-it-4bit

    14. mlx-community/gemma-4-26b-a4b-it-4bit

    15. mlx-community/Qwen2.5-7B-Instruct-4bit

    16. mlx-community/Qwen2.5-14B-Instruct-4bit

    17. mlx-community/phi-4-mlx-4bit

    18. baa-ai/GLM-5.1-RAM-270GB-MLX

    19. baa-ai/GLM-5.1-4bit

    20. Custom (Enter your own Hub ID)

    21. Quit
      Select a model to use: 4

    => Test 8: Tool-Call Degeneration Regression on mlx-community/gemma-4-26b-a4b-it-4bit
    (Reproduces GitHub issue: vague query + small tool = degenerate output)
    Starting server on port 5431...
    Waiting for server (up to 120s)...
    Server ready (5s)

    Running regression suite...

    ─── [1/3] Vague query WITH tool schema (must handle ambiguity naturally, tool call or text) ───
    ✅ run 1 [22.9s P=67t]: TOOL_CALL — web_search
    ✅ run 2 [15.6s P=67t]: TOOL_CALL — web_search
    ✅ run 3 [91.3s P=67t]: TEXT —
    ✅ run 4 [22.2s P=67t]: TOOL_CALL — web_search
    ✅ run 5 [87.9s P=67t]: TEXT —
    → 5/5 runs passed without degenerating

    ─── [2/3] Control: same query WITHOUT tools (must be coherent text) ───
    ✅ run 1 [9.1s P=29t]: TEXT — Since I don't know your specific location or interests, I ca
    ✅ run 2 [9.1s P=29t]: TEXT — To give you the most relevant news, could you let me know wh
    ✅ run 3 [9.0s P=29t]: TEXT — To give you the most relevant news, could own tell me what y
    → 3/3 coherent text responses

    ─── [3/3] Explicit query WITH tool schema (must always call tool) ───
    ✅ run 1 [5.4s P=71t]: TOOL_CALL — web_search
    ✅ run 2 [5.3s P=71t]: TOOL_CALL — web_search
    ✅ run 3 [5.2s P=71t]: TOOL_CALL — web_search
    → 3/3 tool_calls

    ────────────────────────────────────────────────────────────
    ✅ REGRESSION PASSED — tool-call degeneration bug is fixed.
    Vague: 5/5 | No-tool: 3/3 | Explicit: 3/3

    Cleaning up...
    ✅ Test 8 PASSED

  3. added a commit that references this issue on Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions