llama.cpp icon indicating copy to clipboard operation
llama.cpp copied to clipboard

Qwen3-Coder: tool-call markup written inside answer text (a code example) starts the lazy tool grammar -> false tool call or HTTP 500 "Unexpected empty grammar stack"

Open RinkusKhan opened this issue 1 week ago • 2 comments

Name and version

llama-server, release b12634 (42f6a58fd1bfd475d9f09ea3bf7ef671c059eaf2, 2026-11-22), Vulkan build. Also seen on eab8ee41 (2026-08-25), so not a regression.

Operating systems / hardware

Linux (Ubuntu 26.04, Intel Arc MTL iGPU, Vulkan) and Windows 11 (Intel iGPU, Vulkan). Same result on both.

Model

Qwen3-Coder-30B-A3B-Instruct, UD-Q4_K_XL GGUF.

Command line

llama-server -m Qwen3-Coder-30B-A3B-Instruct-UD-Q4_K_XL.gguf -ngl 999 -c 16384 -np 1 -fa on --jinja -ctk q8_0 -ctv q8_0

Problem description

When the model is asked to write code about its own tool-call format (tests for a parser, docs), it writes the markup as text, e.g. inside a Python string:

assert xml("python_repl", code="print(1)") == '''<function=python_repl>\n<parameter=code>...

The lazy tool-call grammar triggers on <function= / <tool_call> even though it is inside a quoted string in the answer. Then one of two things happens:

  1. The example becomes a real tool call (python_repl with {"code":"print(1)"}) -- 4 of 6 runs below. A client executes it.
  2. The next token does not fit the call grammar and the request fails with HTTP 500 -- 2 of 6 runs: got exception: Unexpected empty grammar stack after accepting piece: >\ (8449) (the > followed by a \n escape) or ... piece: >< (1784).

Expected: markup the model writes inside its answer text (in a code block or string) is not a tool call; at least, a token the grammar cannot accept should end the call attempt (or fall back to text) instead of failing the whole request.

Reproduction

One request (repeat with seeds 1-6; temperature 1.0, top_p 0.95, top_k 20 = the model card's settings):

{"messages":[{"role":"user","content":"Our coordinator rescues tool calls that a model wrote as plain text. Extend it to read the Qwen3-Coder form:\n<tool_call>\n<function=NAME>\n<parameter=KEY>\nVALUE\n</parameter>\n</function>\n</tool_call>\nWrite a helper xml(name, **params) that renders that text, then write 8 unit tests as Python code in your answer. Include a test for xml(\"python_repl\", code=\"print(1)\") (a tool nobody owns -> no rescue), a call cut off mid-value, two calls back to back, and a value that itself contains </parameter>. Show the tests as a code block; do not run anything."}],
 "tools":[{"type":"function","function":{"name":"python_repl","description":"Run Python code and return stdout.","parameters":{"type":"object","properties":{"code":{"type":"string"}},"required":["code"]}}}],
 "max_tokens":4096,"stream":true,"temperature":1.0,"top_p":0.95,"top_k":20,"seed":3}

Results on b12634 (Vulkan, Intel Arc), seeds 1-6: seeds 1, 2, 4, 6 -> a python_repl tool call built from the test's string literal; seed 3 -> 500 >< (1784); seed 5 -> 500 >\ (8449). The same prompt without the tool-markup ("write an INI renderer + tests") never fails. On eab8ee41 the same setup gave 2 of 4 HTTP 500 and 2 of 4 tool calls.

Relevant log output

E slot      iterate: id  0 | task 3 | got exception: Unexpected empty grammar stack after accepting piece: >\ (8449)
E srv    send_error: task id = 3, error: got exception: Unexpected empty grammar stack after accepting piece: >\ (8449)

Possibly related: #32818, #30419, #32260 (other "empty grammar stack" paths).

RinkusKhan avatar Nov 23 '26 03:11 RinkusKhan

Your 500 is thrown, not sampled. In src/llama-grammar.cpp, llama_grammar_accept_token throws Unexpected empty grammar stack after accepting piece (line 1854) whenever every stack dies on a piece; llama_grammar_accept_str has the same throw at 1791. Both sit in the sampling path of llama-server, so a constraint mismatch surfaces as HTTP 500 instead of a rejected token.

The path that reaches the throw is the lazy-trigger one in llama_grammar_accept_impl (1721). Until a trigger matches, tokens are buffered (awaiting_trigger, 1726-1737). On a match the buffered pieces are replayed into the grammar (1744-1750) and only then does it constrain. Two consequences:

  1. Everything the model emitted between the trigger match and the last sampled token was never mask-vetted. The replay feeds it to the grammar, and if the grammar cannot consume it, the stacks go empty and you get the throw rather than a bad token being suppressed.
  2. That is what writing tool markup inside code or prose does: the first marker arms the grammar, the rest of the written text is replayed, the stack dies -> 500. Your seeds 1/2/4/6 are the same arming where the replay happened to be consumable, which is why the same content came back as a phantom python_repl call instead of a crash.

Two-minute test that separates the layers: resend the same prompt with the tools block removed from the request. If the 500 disappears, it is the tool-grammar arming path, not the model and not Vulkan. Then, with tools, run --verbose and send the log lines around the throw plus the tool schema you sent.

Workaround worth trying today: skip the --jinja tool grammar for that request and constrain with an explicit --grammar-file, or keep tools out of the request while you validate the prompt. Note -ctk q8_0 -ctv q8_0 is not implicated: a KV-cache quant bug would corrupt the text, not empty a parser stack.

agent-mcocdaa avatar Nov 23 '26 10:11 agent-mcocdaa

Reporter of #32818 here - same mechanism family, and this report is the half we could not fix from the inkling side: #32833 removed inkling's loose triggers, but the replay path still throws when buffered text does not match the grammar root, so this class remains open. The "Expected" paragraph here matches the second option we raised in #32818 (degrade instead of throwing on mismatch) - would be worth treating these as one robustness issue for the lazy-grammar path rather than per-format fixes.

nikosleft avatar Nov 23 '26 11:11 nikosleft