Skip to content

fix(server): keep schema-less MiniMax M3 leaves as strings under a tool schema - #3754

Merged
inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/minimax-m3-schema-context-leaves
Dec 4, 2026
Merged

inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:fix/minimax-m3-schema-context-leaves

Conversation

@Jaeyeong-CHOI

@Jaeyeong-CHOI Jaeyeong-CHOI commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #3739.

With a tool schema in the request, mlx-vlm's MiniMax M3 parser keeps a leaf that has no schema of its own as the string the model wrote (only null becomes JSON null). mlxcel ran the same leaf through the shared loose fallback, so the tool received true, 42 and 2134 for the text true, 42 and 02134. The last one loses the leading zero of a ZIP code or account id. Without tools, mlx-vlm JSON-decodes the leaf and otherwise keeps it as written; mlxcel also tried an integer parse there, so 02134 still became 2134.

This PR ports mlx-vlm's schema_context rule to the MiniMax M3 path only, together with the two schema lookups that decide which leaves it reaches (additionalProperties, and schemas that carry no type). The shared fallback that MiniMax M2, Qwen3-Coder, GLM-4.7 and LongCat use is unchanged.

Reference: Neywa-Labs/mlx-vlm@2208690a mlx_vlm/tools/parsers/minimax_m3.py (_parse_invocation, _node_to_value, _nodes_to_object, _coerce_scalar, _schema_kinds, _schema_property). The file is unchanged at the current HEAD (3c1d8394).

What changed

src/server/tool_calls/formats.rs, MiniMax M3 path only:

  • try_minimax_m3 sets schema_context when the request declares at least one tool. This is mlx-vlm's bool(tools) or isinstance(schema, dict): a function schema can only come from a non-empty tool list, so an empty list counts as no tools, and a call to a function the list does not name is still in the context.
  • minimax_m3_parse_arguments and minimax_m3_element_value carry the flag down the existing descent the way _node_to_value passes it:
    • once set, it stays set;
    • an element with a schema sets it for its children;
    • item children of an element whose schema admits neither an array nor an object are read without it. mlx-vlm reads that list with _node_to_value(child) and its default schema_context=False, so with tools the items of an undeclared item list still decode ([42, true]).
  • Whether a schema admits an array or an object is decided by a port of mlx-vlm's _schema_kinds (new minimax_m3_schema_kinds): every listed type counts; without a type, the anyOf/oneOf alternatives, then items (array) and properties/additionalProperties (object); its aliases (map, sequence, arr, ...). So Pydantic's Optional[list] ({"anyOf": [{"items": {}, "type": "array"}, {"type": "null"}]}) keeps the context for its items. It is used only for this decision; value coercion keeps minimax_m3_schema_type.
  • New minimax_m3_leaf decides how a leaf is read:
    • with a schema that gives the coercion a type to apply (a type mlxcel knows, an enum, or an anyOf/oneOf with such an alternative), through minimax_m3_coerce_leaf, unchanged;
    • otherwise by the schema-less rule. That covers no schema, and also {}, a description or title alone (Pydantic Any), anyOf: [{}, {"type": "null"}] (Pydantic Optional[Any]), a boolean schema, a type name mlxcel does not know, and items: {} (Pydantic List[Any]). mlx-vlm's schema step finds nothing to apply in these either and leaves the value to schema_context.
    • An array-typed leaf that is not a JSON array literal is one occurrence of a repeated tag, so its own schema is items: untyped or missing items gives the schema-less rule, typed items the declared type as before.
  • New minimax_m3_schemaless_leaf reads a leaf with no usable schema:
    • empty text gives "";
    • in the context, null in any casing gives JSON null and any other text stays the raw string;
    • outside it, the text is serde_json::from_str, or else the raw string.
  • minimax_m3_child_in_schema falls back to an additionalProperties schema when properties does not name the child, as mlx-vlm's _schema_property does (additionalProperties: true names no schema). Without this, Pydantic's Dict[str, int] values would have no schema and, with the rule above, stay strings. The function is private and only called from the M3 descent.
  • Mixed text ($text) goes through minimax_m3_leaf too, because mlx-vlm reads it as one more child element. Before, it was always a string.
  • The try_minimax_m3 doc comment no longer says a value with no schema takes the loose fallback; it describes the new rule.
leaf text, no schema of its own before, tools given after, tools given (= mlx-vlm) before, no tools after, no tools (= mlx-vlm)
true true "true" true true
42 42 "42" 42 42
02134 2134 "02134" 2134 "02134"
True true "True" true "True"
+5 / 5. 5 / 5.0 "+5" / "5." 5 / 5.0 "+5" / "5."
null / NULL null / "NULL" null / null null / "NULL" null / "NULL"
[1, 2] [1, 2] "[1, 2]" [1, 2] [1, 2]
`` (empty) "" "" "" ""

With tools, for the schema shapes the review of this change raised:

schema of the value text before after (= mlx-vlm)
{}, {"description": ...}, {"title": ...} 02134 / true 2134 / true "02134" / "true"
{"type": "array", "items": {}} (List[Any]), items 42, true, 02134 [42, true, 2134] ["42", "true", "02134"]
anyOf: [{"items": {}, "type": "array"}, {"type": "null"}] (Optional[list]), items 42, true, 02134 [42, true, 2134] ["42", "true", "02134"]
{"type": "object", "additionalProperties": {"type": "integer"}} (Dict[str, int]), a key 5 5 5
same with {"type": "boolean"} true true true
same with {"type": "string"} (Dict[str, str]) 02134 2134 "02134"

Tests

New tests in src/server/tool_calls/formats.rs:

  • minimax_m3_object_key_value_true_stays_a_string_under_a_tool_schema: mlx-vlm's test_object_keys_with_json_legal_punctuation_are_kept, with value true expected as "true". Only its key without a / (aws:SourceAccount) is ported, because a / in a tag name is fix(server): MiniMax M3 tool-call parser truncates parameter names at "/" #3738 (PR fix(server): keep '/' in MiniMax M3 tool-call tag names #3741).
  • minimax_m3_schemaless_leaves_match_mlx_vlm_with_and_without_tools: the issue's repro, checked with tools, with None and with an empty tool list.
  • minimax_m3_schemaless_leaf_boundaries: 23 texts, each with and without tools. Covers empty, whitespace only, 0, 00, padded 42, -7, +5, 5., 2.5, 1e5, u64::MAX, true/True/false, null/NULL/Null, none, a quoted string, a JSON array, a JSON object, broken JSON and prose. The tool used declares no parameters.
  • minimax_m3_call_to_an_undeclared_function_keeps_schemaless_leaves
  • minimax_m3_typed_leaves_are_unchanged_next_to_schemaless_ones: integer, boolean and a typed key inside an object still take their declared types.
  • minimax_m3_item_lists_take_the_schema_context_mlx_vlm_gives_them: an {"type": "array"} list gives ["42", "true"]; an undeclared list gives [42, true].
  • minimax_m3_mixed_text_takes_the_schemaless_leaf_rule
  • minimax_m3_schemaless_rule_does_not_reach_the_other_xml_grammars: Qwen3-Coder, MiniMax M2, GLM-4.7 and LongCat still read an undeclared 02134 as 2134.
  • minimax_m3_many_schemaless_leaves_stay_linear: 1,024 leaves, the per-element child cap.
  • minimax_m3_additional_properties_schema_types_the_values: Dict[str, int] (5 → 5, 02134 → 2134), Dict[str, bool] (true → true), Dict[str, str], additionalProperties: true and {} (strings), mixed text under Dict[str, int] ($text → 7), and additionalProperties on the parameters object next to a declared string property.
  • minimax_m3_schema_without_a_type_takes_the_schemaless_rule: a description alone (under parameters without type), {}, {"title": ...} (true and null), Optional[Any] (02134 and null), a boolean schema, an unknown type name, {"type": "null"}, List[Any] items, and repeated tags declared array with items: {} and with no items.
  • minimax_m3_item_list_context_follows_mlx_vlm_kind_inference: item lists under Optional[list], anyOf: [array, null], oneOf: [array, string], items without type, sequence, arr, list, ["string", "array"], ["null", "array"] (strings); under the object kinds map, properties only, additionalProperties only, anyOf: [object, null], anyOf: [{}, {}] (strings, see Deviations for the shape); under string and enum (decoded, [42, true, "02134"]).
  • minimax_m3_declared_types_keep_their_mlxcel_reading: pins the deliberate differences listed under Deviations.

New tests in src/server/tool_calls/stream_parity_tests.rs:

  • minimax_m3_schemaless_leaves_are_the_same_streamed_and_not: the issue's text through StreamFilter under every chunking the harness generates (whole, per character, widths 2/3/5/7, every two-piece split). Run once with the schema'd tool and once with a tool that declares no parameters. The content and arguments must match the non-streamed parse.
  • minimax_m3_schema_shapes_are_the_same_streamed_and_not: Dict[str, int], Optional[list] and {} values in one call, under the same chunkings.

Every expected value that the tests attribute to mlx-vlm is the output of mlx-vlm's parse_tool_call on the same text and tool list (Python 3.12.3, the 2208690a file and the HEAD file both run standalone; the outputs are byte-identical). The values in minimax_m3_declared_types_keep_their_mlxcel_reading were run through mlx-vlm too; its results are the ones listed under Deviations.

Deviations from mlx-vlm

Each item below was checked by running mlx-vlm's parser on the same input.

  • A leaf with a declared type whose parse fails keeps the loose fallback. fix(server): type XML tool-call arguments by the request schema #1575 chose to keep that fallback for a failed declared-type parse (its behaviour table: integer, 5.5 → 5.5); in this repository the 5.5 case is pinned by the Qwen3-Coder and LongCat tests, and this PR pins it for MiniMax M3 as well. mlx-vlm returns the raw string in each of these, because its context check runs after its schema step:
    • integer, 5.5: mlxcel 5.5, mlx-vlm "5.5"; the same for an additionalProperties: {"type": "integer"} value;
    • integer, 5.0: mlxcel 5 (integer accepts a zero fraction), mlx-vlm "5.0";
    • object (or any declared type) given a scalar 02134: mlxcel 2134, mlx-vlm "02134".
      The issue is about values the tool did not type, so this PR leaves these alone. The same goes for mlx-vlm's own schema step (for example a boolean accepting 1/0).
  • An item schema inside anyOf/oneOf is still used: Optional[List[int]] items 42, true, 02134 give [42, true, 2134] here, where mlx-vlm only reads a top-level items and gives ["42", "true", "02134"].
  • A repeated tag declared as an array with typed items (items: {"type": "integer"}, 42 and 7) gives [42, 7] here (existing behaviour); mlx-vlm checks each occurrence against the array schema itself and gives ["42", "7"].
  • A leaf whose schema has items or properties but no type is read by the schema-less rule here, because the kind inference is used only for the item-list context. So 02134 stays "02134" as in mlx-vlm, but a JSON literal ([1, 2] under {"items": {}}, {"a": 1} under {"properties": {}}) also stays a string here, where mlx-vlm decodes it to the array or object.
  • The shape of an item list under an object kind (map, properties or additionalProperties only, anyOf: [object, null]): mlx-vlm reads it as an object, {"item": ["42", "true", "02134"]}; mlxcel keeps its array, ["42", "true", "02134"]. The leaves are the same strings. This and the other structure differences (mixed children under an array schema, $text collision naming, an empty leaf under an array/object type giving []/{} in mlx-vlm and "" here) are not touched.
  • JSON decode without tools uses serde_json rather than Python's json.loads. Python also decodes NaN/Infinity (to floats that are not valid JSON on output) and keeps integers beyond u64 exact. Here the first stay strings and the second become f64, as before.

Requests without tools

The no-tools branch matches mlx-vlm (True, +5, 02134 stay strings; true, 5 decode), but no server route reaches it: every route that parses tool calls (chat streaming and non-streaming, Anthropic messages, Responses, the Responses MCP loop, in-process chat) checks should_parse_tool_calls first, which is false when tools is absent or empty or tool_choice is "none". Only direct callers of the parser (parse_tool_calls(text, None)) see the no-tools behaviour.

Checks against past review findings

  1. Boundaries: the empty and whitespace-only leaf, 0/00, u64::MAX, every null casing, an empty tool list, a tool without parameters, a call to an undeclared function, {}, a boolean schema, additionalProperties: true and {} are tested.
  2. Complexity: the flag is one bool passed down the existing recursion, and each leaf does at most one JSON parse, as before. minimax_m3_schema_kinds runs once per element whose children are all item, and walks only that element's schema (its anyOf/oneOf alternatives, recursively). 1,024 leaves are tested.
  3. Other callers:
    • minimax_m3_element_value, minimax_m3_parse_arguments, minimax_m3_schema_for_child and minimax_m3_child_in_schema are only called from the MiniMax M3 path (try_minimax_m3 and the descent).
    • minimax_m3_coerce_leaf (also used by MiniMax M2 and Qwen3-Coder through coerce_xml_param), minimax_m3_typed_coerce, minimax_m3_schema_type and minimax_m3_fallback_coerce (also used by GLM-4.7 and LongCat through coerce_kv_value) are not modified, and their // Used by: lists stay accurate.
    • incremental/qwen.rs only uses minimax_m3_function_schema, which is unchanged.
    • A cross-grammar test pins the four other grammars.
  4. No comment was deleted. One sentence of the try_minimax_m3 doc comment ("A value whose declared-type parse fails (or that has no schema) falls back to ...") lost its parenthetical, because it is no longer true; a new paragraph describes the rule. One sentence of the minimax_m3_element_value doc comment now says "admits neither an array nor an object" instead of "is neither an array nor an object".
  5. cargo fmt --all -- --check passes on the final tree.

Relation to #3741

Independent of #3741 (same parser, tag names with /). This branch is based on main and merges cleanly onto current main (5bf7b8628) and onto #3741's branch, and #3741 merges cleanly onto main (git merge-tree). #3741's port of the same upstream test uses the value team-a so that it does not depend on how a schema-less leaf is read. Whichever of the two merges second switches that test to upstream's value true (expected "true" for all four keys) and drops the one-key test here (minimax_m3_object_key_value_true_stays_a_string_under_a_tool_schema), which it then covers. That combination was run (see Verification).

Verification

Linux aarch64, NVIDIA GB10 (sm_121), CUDA 13.0, rustc 1.97.1.

Every "this commit" run below used exactly the commit's tree (dd6d3be8, checked with git write-tree before and after the full gate).

  • Fail before. On main (9402cf8) plus only the new tests, cargo test --locked --profile test-fast --features cuda --lib -- --test-threads=1 server::tool_calls gives 571 passed, 13 failed. The 13 failures are 11 of the new formats.rs tests and both new stream-parity tests. The other two new tests pin unchanged behaviour (minimax_m3_schemaless_rule_does_not_reach_the_other_xml_grammars, minimax_m3_declared_types_keep_their_mlxcel_reading), so they pass on main as intended. For example, the issue repro (and, the same way, the stream-parity test, whose streamed and non-streamed results agree with each other on main too) fails with:

      left: Object {"policy": Object {"Condition": Object {"StringEquals": Object {"k": Bool(true), "zip": Number(2134), "n": Number(42)}}}, "flag": Bool(true)}
     right: Object {"policy": Object {"Condition": Object {"StringEquals": Object {"k": String("true"), "zip": String("02134"), "n": String("42")}}}, "flag": String("true")}
    

    and the schema-shape tests with, for example, {} → Number(2134) instead of String("02134"), Optional[list] items → [Number(42), Bool(true), Number(2134)], and Dict[str, str] → {"a": Number(5), "b": Number(2134)}.

  • Pass after. The same command on this commit gives 584 passed, 0 failed.

  • cargo fmt --all -- --check: pass.

  • cargo clippy --locked --workspace --all-targets --features cuda -- -D warnings: pass.

  • python3 scripts/ci/check_used_by.py: used-by: OK, 78 top-level pub fn in 2 shared files, 57 annotated, 715 caller-file references resolved, 3498 Rust files scanned. bash scripts/ci/check_used_by_test.sh: pass.

  • Full gate. make verify-test-cuda (cargo test --workspace --profile test-fast --features cuda --no-fail-fast -- --test-threads=1) gives 18,906 passed, 2 failed, 578 ignored over 207 test binaries. The two failures are mlx_test_guard_coverage::mlx_evaluating_lib_tests_take_mlx_test_guard (src/vision/encoders/nemotron_h_nano_omni_tests.rs:276 pos_embed_resample_runs_in_f32) and step3p7_parity::text_decode_delegates_sequence_local_native_batching (assertion failed: !wrapper.supports_padded_prefill()). Both fail the same way on unmodified main (9402cf8): cargo test --locked --profile test-fast --features cuda --test mlx_test_guard_coverage --test step3p7_parity --no-fail-fast -- --test-threads=1 reports 5 passed; 1 failed and 7 passed; 1 failed, with the same two test names. They were pre-existing on the base; current main fixes both (feat(speculative): add DeepSeek-V4.1 bundled DSpark drafter (#3262) #3686, test: restore MLX guard scan and no-GPU catalog test gates #3765).

  • Together with fix(server): keep '/' in MiniMax M3 tool-call tag names #3741. main + fix(server): keep '/' in MiniMax M3 tool-call tag names #3741's head at the time (65f9e9bb3) + this commit merge without conflicts (merged tree 11af160b, the same as git merge-tree). fix(server): keep '/' in MiniMax M3 tool-call tag names #3741's updated head, which only drops its CHANGELOG.md entry, merges with this commit to tree bcc27142, which differs from 11af160b only in CHANGELOG.md. With a temporary, uncommitted port of mlx-vlm's test using all four keys (aws:SourceAccount, aws:ResourceTag/team, aws:RequestTag/${TagKey}, application/json) and value true, server::tool_calls gives 588 passed, 0 failed; every key gives "true".

Limits

  • Parser-level change; no model was loaded. MiniMax-M3 (427B) does not fit the test host.
  • Metal and ROCm were not run. The change has no backend-specific code.

…ol schema

mlx-vlm's MiniMax M3 parser reads a leaf that has no schema of its own in
"schema context" once the request declares tools: it stays the string the
model wrote, except `null`. Without tools it is JSON-decoded and otherwise
kept as written. mlxcel ran every such leaf through the shared loose
fallback, so with tools `true`, `42` and `02134` came back as `true`, `42`
and `2134` (the leading zero lost), and without tools `02134` still became
`2134`.

Carry mlx-vlm's `schema_context` flag down the M3 descent and read a
schema-less leaf (and mixed `$text`) with an M3-only helper. As in
mlx-vlm, a schema that gives no type to apply (`{}`, a description alone,
`Optional[Any]`) counts as no schema, a named child falls back to an
`additionalProperties` schema, and whether an `item` list keeps the
context follows mlx-vlm's schema-kind inference (`Optional[list]`,
`items` without `type`, `map`/`sequence`/`arr`). Leaves with a declared
type and the shared fallback used by MiniMax M2, Qwen3-Coder, GLM-4.7
and LongCat are unchanged.

Closes lablup#3739.
@inureyes inureyes added status:review Under review type:bug Bug fixes, error corrections, or issue resolutions priority:medium Medium priority labels Dec 4, 2026
With lablup#3741 merged first, its port of mlx-vlm's test_object_keys_with_json_legal_punctuation_are_kept now uses upstream's value `true`, expecting the string "true" for all four keys under a tool schema. That covers the one-key test added here, so it is dropped.

Add minimax_m3_client_schema_walk_is_bounded: a 60-level anyOf nest parsed through serde_json (the request-body nesting limit is what bounds the new schema-kind recursion) still keeps the item list in schema context, and a self-referential `$ref` is never resolved, so its leaves take the schema-less rule (as in mlx-vlm and vLLM).

Refs lablup#3739, lablup#3754
@inureyes

inureyes commented Dec 4, 2026

Copy link
Copy Markdown
Member

Hardening review against #3739 and upstream, with follow-up commits pushed to this branch.

Upstream check. I compared the port with mlx-vlm mlx_vlm/tools/parsers/minimax_m3.py (_parse_invocation, _node_to_value, _nodes_to_object, _coerce_scalar, _schema_kinds, _schema_property). The context rule matches: tools present sets it, an element with a dict schema sets it for its children, and an undeclared item list is read without it. The kind inference matches as well (all listed types, then anyOf/oneOf, then enum/items/properties/additionalProperties, with the same aliases), and so does the additionalProperties fallback. vLLM's vllm/parser/minimax_m3.py also returns a schema-less leaf as the raw string. The deviations listed in the PR body are accurate and in scope.

Schema walk bounds. Neither upstream resolves $ref, and neither does this port, so a self-referential schema cannot loop. The new recursion (minimax_m3_schema_kinds, minimax_m3_schema_is_typed) only descends anyOf/oneOf values physically nested in the request JSON, which serde_json's 128-level parse limit bounds. The walk is linear in the element's schema subtree, like the existing minimax_m3_typed_coerce.

Changes pushed.

  • 7536e9e60: with fix(server): keep '/' in MiniMax M3 tool-call tag names #3741 merged first, its minimax_m3_object_keys_keep_slash_and_punctuation now uses upstream's value true and expects "true" for all four keys. The one-key test that duplicated it here is dropped. Added minimax_m3_client_schema_walk_is_bounded, which covers a 60-level anyOf nest parsed through serde_json and a self-referential $ref (no resolution, schema-less rule).
  • 04817faa8: merged current origin/main (no conflicts; nothing under src/server changed).

Validation (merged tree). cargo test --profile test-fast --lib -- tool_call: 658 passed, 0 failed. tool_parser: 3 passed. cargo clippy --all-targets -- -D warnings (default features) and cargo fmt --check are clean, and scripts/ci/check_used_by.py is OK. The streaming parity tests are part of the tool_call run. No model run: MiniMax-M3 does not fit the host.

@inureyes
inureyes merged commit 02ca1be into lablup:main Dec 4, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority:medium Medium priority status:review Under review type:bug Bug fixes, error corrections, or issue resolutions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(server): MiniMax M3 coerces schema-less tool-call leaves that mlx-vlm keeps as strings, dropping leading zeros

2 participants