Repository navigation
fix(server): type XML tool-call arguments by the request schema - #1575
Merged
Merged
Conversation
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
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
Closed
5 of 6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Qwen3-Coder (
<function=NAME><parameter=KEY>VALUE</parameter>) and MiniMax M2 (<invoke name><parameter name>) tool-call parsers never received the request'stools, so they guessed each argument's JSON type from the raw text. Astring-typed parameter whose value looked numeric or boolean was rewritten:02134became2134,1e5became100000.0,truebecame the boolean; aninteger-typed parameter written as5.0came out as the float5.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_coderandtry_minimax_m2taketools, resolve the called function withminimax_m3_function_schema, and pass the per-parameter schema down throughextract_qwen_parameters/extract_minimax_parametersto the newcoerce_xml_param, which delegates to the sharedminimax_m3_coerce_leaf.coerce_minimax_paramis deleted.coerce_xml_paramkeeps two rules ahead of the shared coercion, both requiring a declaration to apply. The bare wordnullin any casing is JSON null, because these grammars have no other spelling for one, except under a plainstringdeclaration 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 aboolean-declared parameter accepts the words a strict JSON read rejects (yes,on,1and their negatives, plus any casing oftrue/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 onefn(&str)pointer type, which is the reason the two parsers could not be handedtools. It is now an array of a smallFormatParserenum (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 inparse_tool_callsare corrected: MiniMax M3 is no longer "the only parser that needs the request tool schema", and the GLM/LongCat block no longer refers to anfn(&str)array that does not exist.The shared
integerarm 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 throughf64: a round trip rounds before any guard can inspect the result, which would turn9007199254740991.5into an integer and bring9007199254740993.0back off by one. Doing it as text is exact at every magnitude and keeps the exponent form out, which matters becauseminimax_m3_typed_coercetakes the firstanyOfalternative that succeeds andanyOf: [integer, string]should still hand an identifier like1e5to the string alternative. Because the arm is shared,integer-typed5.0becomes5for MiniMax M3 too, andcoerce_kv_valuepicks 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_schemanow falls back to the bare name after the first.when an exact match misses, the same normalizationfilter_by_toolsapplies. 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
numberarm now tries an exact integer literal before the float parse, so anumber-typed value keeps the form the model wrote (5stays5,5.0stays5.0). That arm is shared with MiniMax M3, so anumber/num/float/doubleparameter written as an integer now serializes as5rather than5.0. JSON Schema accepts an integer as anumber, 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
{"type":"string"}021342134"02134"{"type":"string"}truetrue"true"{"type":"string"}1e5100000.0"1e5"{"type":"string"}nullnull"null"{"type":["string","null"]}nullnullnull{"type":"boolean"}yes/on/TRUEtruetrue{"type":"integer"}5.05.05{"type":"integer"}5.55.55.5(loose fallback, call kept){"type":"number"}555{"type":"object"}{not json"{not json""{not json"{"anyOf":[integer,string]}1e5"1e5""1e5"5/true/null5/true/nullyes/on/none/TRUEtrue/true/null/true"yes"/"on"/"none"/"TRUE"functions.fcall,{"type":"string"}021342134"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 warningscargo fmt --all -- --check26 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
f64round trip gets wrong; theanyOf: [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: aboolean-declaredyes/on/TRUEdegraded to a string; thenullshortcut overrode an explicitstringdeclaration; the integer rule'sf64round trip rounded a fractional value into an integer and returned an off-by-one past 2^53, and its exponent form let ananyOfinteger 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_coercereturns out of itsanyOf/oneOfloop on the first key present (pre-existing); theminimax_m3_prefix on the now-shared helpers deserves a rename in its ownrefactor:PR; and an array-typed parameter carrying one bare scalar yields that scalar rather than a one-element array, whichxml_array_typed_bare_scalar_takes_the_item_typedocuments 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:Expected:
choices[0].message.tool_calls[0].function.argumentsparses to{"zip": "02134", "days": 3}, withzipa JSON string (leading zero intact) anddaysa JSON integer. Repeating the request with"stream": truegives the same arguments in the final tool-call delta, since both routes callparse_tool_calls(text, tools).Closes #1336