Skip to content

fix(server): type XML tool-call arguments by the request schema - #1575

Merged
inureyes merged 5 commits into
mainfrom
fix/issue-1336-tool-call-schema-coercion
Sep 2, 2026
Merged

inureyes merged 5 commits into
mainfrom
fix/issue-1336-tool-call-schema-coercion

Conversation

@inureyes

@inureyes inureyes commented Sep 2, 2026 •

Copy link
Copy Markdown
Member

Summary

The Qwen3-Coder (<function=NAME><parameter=KEY>VALUE</parameter>) and MiniMax M2 (<invoke name><parameter name>) tool-call parsers never received the request's tools, so they guessed each argument's JSON type from the raw text. A string-typed parameter whose value looked numeric or boolean was rewritten: 02134 became 2134, 1e5 became 100000.0, true became the boolean; an integer-typed parameter written as 5.0 came out as the float 5.0. Both routes that parse a completion (non-streaming and end-of-stream) already hold the tool set, so the schema was available all along and only these two parsers could not reach it. They now type every value by its declared schema through the same helpers MiniMax M3, GLM-4.7 and LongCat already use, and every remaining lenient rule is grounded in a declared type rather than in the shape of the text.

What changed

src/server/tool_calls/formats.rs: try_qwen3_coder and try_minimax_m2 take tools, resolve the called function with minimax_m3_function_schema, and pass the per-parameter schema down through extract_qwen_parameters / extract_minimax_parameters to the new coerce_xml_param, which delegates to the shared minimax_m3_coerce_leaf. coerce_minimax_param is deleted.

coerce_xml_param keeps two rules ahead of the shared coercion, both requiring a declaration to apply. The bare word null in any casing is JSON null, because these grammars have no other spelling for one, except under a plain string declaration where the text is what the tool asked for (a query, a ref, a filename); a nullable {"type": ["string", "null"]} still takes the null. And a boolean-declared parameter accepts the words a strict JSON read rejects (yes, on, 1 and their negatives, plus any casing of true/false), which is the schema basis the old free-text guess never had. Without a declaration those same words stay the strings the model wrote, matching the three schema-aware grammars. A value that fits neither its declared type nor a JSON literal stays a string, so an unparseable argument still never drops the call.

src/server/tool_calls/parser.rs: the format table was an array of one fn(&str) pointer type, which is the reason the two parsers could not be handed tools. It is now an array of a small FormatParser enum (Plain / WithTools) in the same order as before, so dispatch order is unchanged: Functionary v3.1 still claims a JSON body ahead of Qwen3-Coder, and Qwen3-Coder still runs ahead of Functionary v3.2. Two stale comments in parse_tool_calls are corrected: MiniMax M3 is no longer "the only parser that needs the request tool schema", and the GLM/LongCat block no longer refers to an fn(&str) array that does not exist.

The shared integer arm now also accepts a literal written with a zero fraction (5.0, -5.000). The fraction is stripped textually and the integer part parsed exactly, rather than round-tripping through f64: a round trip rounds before any guard can inspect the result, which would turn 9007199254740991.5 into an integer and bring 9007199254740993.0 back off by one. Doing it as text is exact at every magnitude and keeps the exponent form out, which matters because minimax_m3_typed_coerce takes the first anyOf alternative that succeeds and anyOf: [integer, string] should still hand an identifier like 1e5 to the string alternative. Because the arm is shared, integer-typed 5.0 becomes 5 for MiniMax M3 too, and coerce_kv_value picks the same rule up for GLM-4.7 and LongCat; every other declared type in that key/value grammar keeps the loose fallback it has always used.

minimax_m3_function_schema now falls back to the bare name after the first . when an exact match misses, the same normalization filter_by_tools applies. Without it a model that namespaces its calls (functions.get_weather) missed the schema lookup, every value silently took the loose rules, and the call still succeeded, so the whole fix no-opped with nothing to show for it.

The number arm now tries an exact integer literal before the float parse, so a number-typed value keeps the form the model wrote (5 stays 5, 5.0 stays 5.0). That arm is shared with MiniMax M3, so a number/num/float/double parameter written as an integer now serializes as 5 rather than 5.0. JSON Schema accepts an integer as a number, and it removes a precision loss past 2^53 that the previous float-only arm had, but it is a visible change beyond what the issue asked for.

Behavior table

schema raw before after
{"type":"string"} 02134 2134 "02134"
{"type":"string"} true true "true"
{"type":"string"} 1e5 100000.0 "1e5"
{"type":"string"} null null "null"
{"type":["string","null"]} null null null
{"type":"boolean"} yes / on / TRUE true true
{"type":"integer"} 5.0 5.0 5
{"type":"integer"} 5.5 5.5 5.5 (loose fallback, call kept)
{"type":"number"} 5 5 5
{"type":"object"} {not json "{not json" "{not json"
{"anyOf":[integer,string]} 1e5 "1e5" "1e5"
none 5 / true / null 5 / true / null unchanged
none yes / on / none / TRUE true / true / null / true "yes" / "on" / "none" / "TRUE"
functions.f call, {"type":"string"} 02134 2134 "02134"

One further no-schema difference: the fallback now runs a full JSON parse rather than only trying one when the text starts with { or [, so a schema-free value written as a JSON literal ([1, 2], or a quoted string) is parsed as that literal. This is the behavior MiniMax M3, GLM-4.7 and LongCat already had, so the four grammars now agree.

Test plan

  • cargo test --profile test-fast --features metal,accelerate --lib server::tool_calls (376 passed, 0 failed)
  • cargo test --profile test-fast --features metal,accelerate --lib server::muse_atem (11 passed, 0 failed)
  • cargo test --profile test-fast --features metal,accelerate --lib server::routes::chat (48 passed, 0 failed)
  • cargo clippy --profile test-fast --lib --tests --features metal,accelerate -- -D warnings
  • cargo fmt --all -- --check

26 new unit tests cover: string, integer, number, boolean, object, array and no-schema values in both XML grammars; the fractional-value fallback that keeps the call; the null rule across plain-string, nullable and schema-free declarations; the boolean words with and without a declaration; a namespaced call name; a request whose schema names a different function; the textual integer rule including both values an f64 round trip gets wrong; the anyOf: [integer, string] exponent case; the shared integer rule reaching MiniMax M3, GLM-4.7 and LongCat; and the unchanged dispatcher order (JSON body to Functionary v3.1, XML body to Qwen3-Coder). The four cases the issue asked to keep green (qwen3_coder_single_call_multiple_params_with_type_coercion, minimax_m2_numeric_params, minimax_m2_boolean_param, minimax_m2_null_param) pass unchanged.

Changes during review

Review of the first commit found five defects, four of them regressions the schema-driven path introduced against the guesses it replaced, all fixed in f222376: a boolean-declared yes/on/TRUE degraded to a string; the null shortcut overrode an explicit string declaration; the integer rule's f64 round trip rounded a fractional value into an integer and returned an off-by-one past 2^53, and its exponent form let an anyOf integer alternative claim an identifier; and the schema lookup missed entirely for a namespaced call name. The // Used by: comments the repository's contributor guidelines require were also added to the five shared helpers whose caller set widened from three grammars to five.

Three findings were deliberately left for follow-ups rather than widened into this PR: minimax_m3_typed_coerce returns out of its anyOf/oneOf loop on the first key present (pre-existing); the minimax_m3_ prefix on the now-shared helpers deserves a rename in its own refactor: PR; and an array-typed parameter carrying one bare scalar yields that scalar rather than a one-element array, which xml_array_typed_bare_scalar_takes_the_item_type documents as current behavior.

Server sanity check (not run here, no checkpoint on this host)

This is parser-only work covered by unit tests, so no model was loaded. To confirm end to end against a Qwen3-Coder export that emits the <function=...><parameter=...> grammar:

./target/release/mlxcel-server -m models/<qwen3-coder-dir> --port 8080
curl -s localhost:8080/v1/chat/completions -H 'content-type: application/json' -d '{
  "model": "x", "messages": [{"role": "user", "content": "Look up the forecast for ZIP code 02134 for the next 3 days using the tool."}],
  "tools": [{"type": "function", "function": {"name": "forecast", "parameters": {"type": "object",
    "properties": {"zip": {"type": "string"}, "days": {"type": "integer"}}, "required": ["zip", "days"]}}}]}'

Expected: choices[0].message.tool_calls[0].function.arguments parses to {"zip": "02134", "days": 3}, with zip a JSON string (leading zero intact) and days a JSON integer. Repeating the request with "stream": true gives the same arguments in the final tool-call delta, since both routes call parse_tool_calls(text, tools).

Closes #1336

The Qwen3-Coder (`<function=NAME><parameter=KEY>VALUE</parameter>`) and MiniMax M2 (`<invoke name><parameter name>`) parsers never saw the request's `tools`, so they guessed each argument's JSON type from the raw text. A `string`-typed parameter whose value looked numeric or boolean was rewritten: `02134` became `2134`, `1e5` became `100000.0`, `true` became the boolean. An `integer`-typed parameter written as `5.0` came out as the float `5.0`. Both routes that parse a completion already have the tool set, so the schema was there the whole time and only the two parsers could not reach it.

`try_qwen3_coder` and `try_minimax_m2` now take `tools`, look the called function up with `minimax_m3_function_schema`, and type every value through the shared `minimax_m3_coerce_leaf` used by MiniMax M3, GLM-4.7 and LongCat. `coerce_minimax_param` is gone, replaced by `coerce_xml_param`, which keeps one schema-independent rule (`null` in any casing is JSON null, the only spelling these grammars have for it) and drops the guesses that had no schema basis: `yes`/`on` to `true`, `no`/`off` to `false`, `none`/`nil` to null. Those words are now the strings the model wrote. A value that fits neither its declared type nor a JSON literal stays a string, so an unparseable argument still never drops the call.

The dispatcher's format table was an array of one `fn(&str)` pointer type, which is why the two parsers could not be handed `tools` in the first place. It is now an array of a `FormatParser` enum with `Plain` and `WithTools` variants, in the same order as before: Functionary v3.1 still claims a JSON body ahead of Qwen3-Coder, and Qwen3-Coder still runs before Functionary v3.2.

The shared integer rule now also accepts a zero-fraction float literal (`5.0`, `1e5`), bounded by 2^53 so a value past exact float precision declines instead of rounding silently. That arm is shared, so `integer`-typed `5.0` becomes `5` for MiniMax M3 as well, and `coerce_kv_value` picks it up for GLM-4.7 and LongCat; every other declared type in that key/value grammar keeps the loose fallback it has always used. The `number` arm now tries an exact integer literal before the float parse, so a `number`-typed value keeps the form the model wrote (`5` stays `5`, `5.0` stays `5.0`) instead of being turned into a float.

Validated with `cargo test --profile test-fast --features metal,accelerate --lib server::tool_calls` (370 passed), the same command scoped to `server::muse_atem` (11 passed), `cargo clippy --profile test-fast --lib --tests --features metal,accelerate -- -D warnings`, and `cargo fmt --all -- --check`. 20 new unit tests cover the string, integer, number, object and no-schema cases in both XML grammars, the null rule, a schema that names a different function, the 2^53 boundary, and the unchanged dispatcher order.

Closes #1336
@inureyes inureyes added type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority area:inference Generation, sampling, decoding (incl. speculative, DRY) status:review Under review labels Sep 2, 2026
Review of the first commit found five defects, four of them regressions the schema-driven path introduced against the guesses it replaced.

A `boolean`-declared parameter lost the words the old free-text guess accepted. The strict typed arm takes only `true`/`True`/`false`/`False`, so under `{"type": "boolean"}` a model writing `yes`, `on`, `1` or `TRUE` came out as that string, which is worse than the guess for exactly the declaration that justifies reading it as a boolean. `coerce_xml_param` now maps those words (and their negatives) for a `boolean`-declared parameter only; without the declaration they stay the text the model wrote, matching MiniMax M3, GLM-4.7 and LongCat.

The `null` shortcut ran ahead of the schema, so a parameter declared a plain `string` whose text was `null` reached the tool as JSON null: the same type violation this issue is about, and a divergence from MiniMax M3, which keeps `"null"` there. It now yields the string under a plain `string` declaration and stays null everywhere else, including a nullable `{"type": ["string", "null"]}` and the schema-free case.

The integer rule parsed through `f64`, which rounds before any check can see it. `9007199254740991.5` arrived with an empty fraction and became an integer, and `9007199254740993.0` came back off by one; the 2^53 bound the comment claimed as protection was applied after the damage. The rule is now textual: strip a zero fraction, then parse the integer part exactly. That is exact at every magnitude, and it drops the exponent form, which under `anyOf: [integer, string]` let the integer alternative claim an identifier such as `1e5` that the string alternative used to own.

`minimax_m3_function_schema` matched the call name exactly, but models namespace their calls (`functions.get_weather`) and `filter_by_tools` strips that prefix only after parsing. The schema lookup missed, every value silently took the loose rules, and the call still succeeded, so the whole fix no-opped with nothing to show for it. The lookup now falls back to the bare name after the first `.`, the same normalisation `filter_by_tools` applies.

Also adds the `// Used by:` comments CLAUDE.md requires on the five shared helpers whose caller set widened from three grammars to five.

Six new tests cover the boolean words with and without a declaration, the null rule across plain-string, nullable and schema-free cases, a namespaced call name, the MiniMax M2 mirror of the schema-free rules, an array-typed parameter carrying a bare scalar, and the `anyOf` exponent case. `integer_literal_rule_is_textual_and_exact` replaces the old bound test and pins both rounding failures.

Validated with `cargo test --profile test-fast --features metal,accelerate --lib server::tool_calls` (376 passed), the same command scoped to `server::muse_atem` (11) and `server::routes::chat` (48), `cargo clippy --profile test-fast --lib --tests --features metal,accelerate -- -D warnings`, and `cargo fmt --all -- --check`.

Refs #1336
@inureyes
inureyes merged commit 3eeb196 into main Sep 2, 2026
12 checks passed
@inureyes
inureyes deleted the fix/issue-1336-tool-call-schema-coercion branch September 2, 2026 02:01
@inureyes inureyes added this to the 0.7 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:inference Generation, sampling, decoding (incl. speculative, DRY) priority:medium Medium priority status:done Completed type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(server): coerce Qwen3-Coder and MiniMax M2 tool-call arguments by the request schema

1 participant