Skip to content

tools: SparkQwen 0.4.0 tool/grammar follow-ups: the tool-call-or-JSON union, required/named tool deferral, literal-XML fixes (ported from Enntity) - #283

Merged
AzeezIsh merged 17 commits into
mainfrom
port/sq-tools-followups
Oct 31, 2026

Conversation

@AzeezIsh

Copy link
Copy Markdown
Contributor

Summary

Next part of bringing SparkQwen 0.4.0's tool calling onto main, stacked on #279 (merged). Ported from Enntity spark-shared (AGPL-3.0, data-angel), credited per commit.

  • Poolside argument typing (9913d54f4) and the tool-loop repeat notice (236beb904), including 0.4.0's repeat-turn thinking-budget escalation.
  • The tool-call-or-JSON union: tools + response_format under tool_choice: auto compile one grammar that picks the tool branch or the JSON branch by mass. Commits: 66218e884, 98a3fa218, aaa09c293, 5db3b6156, 9f075c468, 8f812bd57, plus its xgrammar prerequisite 1921b33f7 (Grammar::union, schema-bounded compile).
  • The 10-22 literal-XML and stream fixes: 0ce591b63, 19bfc90bd, baa93620b, 4bcbc9671, 17e5c92b0, 318569b87, ba0b7bd08, 095ff2237.
  • Required and named tool_choice defer the tool grammar until reasoning ends (ed381734d). A hunk audit against 0.4.0 also restored the defer_first_token machinery, the union-aware tool_call_bias and role_literal_leaks.
  • This fixes a bug that is on main today: with streaming plus tool_choice: "required" or a named tool, main ends with finish_reason=length and no tool call. 0.4.0 emits the call. Measured on the pair with main 48f73b27c.

Test plan (reiner + spark2, TP=EP=2; receipts in reiner's ~/code/night1027/out/stack-c3d64fea4/, stackt-64a9e1a71/)

  • spark-server bin tests 2863/0 and xgrammar 835/0. clippy (with and without --tests) and fmt are clean.
  • Against SparkQwen 0.4.0's engine on its spark config (union_check.py, 8 cases × block/stream):
    • 15/16 identical. Streaming required and named tool calls now match 0.4.0.
    • The one remaining diff, a newline after ```xml in a streamed fence, is fixed in the last PR of this stack.
    • api_parity.py: 26/26 identical. It covers auto single and parallel tools, tool_choice none, json_object, stop, n=2, unset max_tokens, effort high, a tool-result round, and /v1/messages and /v1/responses (both block and stream).
  • rc16 profile: C1 and C8 greedy 8/8 identical to main. Tools 21/21.
  • BFCL ST-995 on the top of the stack (running).

Generated with Devin

data-angel and others added 17 commits October 31, 2026 04:07
GLM's chat template renders a string argument raw and any other value as
JSON, so `<arg_value>true</arg_value>` is the string "true" or the boolean
true depending only on the parameter's declared type. The parser JSON-parsed
every value before the schema was consulted, and coerce_all has no string
arm, so string parameters came back as booleans, numbers, lists and objects:
Java/JavaScript literals in BFCL (`"true"`, `"[60, 30]"`), zip codes and
version strings ("1.10" -> 1.1). Keep the text as written, as qwen3_coder
does; coerce_all (wants_typed_arguments) types it from the schema, and
resolve_schema_type now also reads type arrays (`["integer", "null"]`).

On the MLPerf edge-agentic BFCL leg (official harness, SparkGLM), 63 of 995
samples failed on exactly this; rescoring those outputs with the values kept
as strings estimates 83.7 -> 88.6 overall.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 9913d54)
When the same tool call returned the same result three times, the loop
detector hard-masked <tool_call> for a turn (and the soft bias already
pushed against it at three repeats). A model that still wants to act then
narrates the action it cannot take: omp on GLM-5.3-Flash repeated one
paragraph for 756 tokens after its third identical grep.

From the second identical round (same calls and arguments, same non-error
result), append a <REPEAT NOTICE> to the latest tool result, give that
turn the xhigh thinking rung unless the client set an explicit budget,
and keep <tool_call> open and unbiased. Error repeats keep their existing
hints; requests without identical repeats are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 236beb9)
compile_tool_or_json_grammar unions a parser's own structural-tag tool
grammar with the response_format JSON (json_schema with the strict
options and limits of compile_json_schema, or the builtin JSON grammar
for json_object). The tool branch is the parser's auto-mode tags made
required from the first output token, with parallel calls after it, so
the first token picks a call or the bare JSON: fenced JSON and prose
are refused. Generic over every structural-tag parser; the JSON branch
is scoped to the one compile.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 66218e8)
…mmar

With both set, Atlas enforced only the tool-call grammar
(sampling_setup.rs: "response_format + tools both set; enforcing
tool-call grammar"), so on the turn the model answered instead of
calling a tool the content was free and GLM wrapped correct JSON in
```json fences that strict clients reject (ToolEvalBench TC-65..69).

grammar_choice is the request -> grammar matrix: auto/unset with a
response_format now gets GrammarSpec::ToolCallOrJson, the union of the
parser's tool grammar and the schema, a strict (response_format)
grammar; "required"/named keeps the tool grammar alone, "none" the
schema alone, and every request without a response_format is unchanged.
OpenAI and vLLM enforce the schema on the final answer the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 98a3fa2)
Masked token by token, the union let the model's runner-up decide: when
GLM means to answer with a ```json fence (its top token, masked), the
tool opener outranks `{` and every answer becomes a tool call. At the
union's first output token the branch is now chosen by mass: the tool
branch when the model puts more probability on tool-branch tokens than
on everything else (an answer in any shape), else the answer; that
position's mask is then the chosen branch's alone (grammar::branch).

The answer branch's first-token mask comes from the response_format
JSON compiled alone (GrammarEngine::json_start_mask). The choice runs
wherever host logits meet the mask: the logits pipeline
(GrammarBitmaskApply) and sample_token_with_grammar, whose greedy fast
path no longer takes the top token alone at the branch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit aaa09c2)
Grammar::union ports upstream's Grammar::Union over the existing
GrammarUnion functor. compile_schema_grammar compiles a grammar that
embeds a client JSON schema under the same max_schema_grammar_bytes
bound compile_json_schema enforces, so a union with a schema cannot
dodge it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 1921b33)
…) — required tool grammars defer to after reasoning
@AzeezIsh
AzeezIsh marked this pull request as ready for review October 31, 2026 15:12
@AzeezIsh
AzeezIsh merged commit 5d8f87a into main Oct 31, 2026
25 of 26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants