Skip to content

Tool parsers: return well-formed calls that break only their schema (GLM5, Gemma, MiMo V2; d-inference#2156) - #434

Merged
jonathan308 merged 8 commits into
mainfrom
fix/glm5-tool-grammar-only
Nov 15, 2026
Merged

jonathan308 merged 8 commits into
mainfrom
fix/glm5-tool-grammar-only

Conversation

@jonathan308

@jonathan308 jonathan308 commented Nov 15, 2026 •

Copy link
Copy Markdown

Proposed changes

Fixes the SDK half of d-inference#2156: a GLM-5.3-Flash call that keeps the GLM grammar but omits a required argument comes back from Darkbloom as raw <tool_call> text with finish_reason: "stop", because GLM5ToolCallParser refuses it. A matrix over every served format found the same class in two more parsers.

Parser Refused before (well-formed frame) What the provider did with it
GLM5ToolCallParser missing required argument; undeclared argument (additionalProperties absent or false); parameter without a usable type; union with string; value of another type auto: the whole trailing batch as text, finish_reason: stop
GemmaFunctionParser undeclared argument under additionalProperties: false auto: the frame as visible text
MiMoV2ToolCallParser union (["string","integer"], anyOf); value of another type auto: HTTP 500 (native MiMo tool output is invalid)
Harmony, Qwen 3.5, XML, Nemotron, JSON nothing (already grammar-only) —

The three parsers now check their frame grammar and the declared function name only and return such a call as generated, for the caller to validate, as vLLM and SGLang do. Declared types only choose how a value reads:

  • GLM5 (types from type, nullable and anyOf/oneOf branches; GLM5ToolCallParser.argumentTypes(_:parameters:) exposes them so d-inference's tool-suffix lexer reads frame boundaries the same way): only string → raw text; a JSON value of a declared non-string type, or any JSON but a string where no string is declared → that JSON (quoted delimiters inside are data); anything else → JSON when its raw text (up to the first </arg_value>) parses (for a union with string, only as one of the other types), else the raw text.
  • MiMo V2: every value it accepted before reads the same; a union or a value of another type it refused → JSON when it parses as a declared non-string type (any JSON when no string is declared), else raw text.
  • Gemma: undeclared arguments are kept.

Frames that break the grammar still fail (open at the end, misplaced or nested tags, a repeated argument, an undeclared function name), and one such GLM5 frame still refuses the batch at EOS.

Kill switch: DARKBLOOM_GRAMMAR_ONLY_TOOL_PARSING=0 (off/false/no) restores the three parsers' schema refusals (GrammarOnlyToolParsing; each parser also takes grammarOnly: in init).

Companion: d-inference#2157 (into dev) pins this branch, decides GLM's trailing run call by call and forwards the switch. This PR is not to be merged into main without the owner's OK; the 0.10.14 fold moves the d-inference pin to its merge commit.

Review

Refactor pass and independent fresh-context review (separate sessions; the review fuzzed ~2M suffix/schema pairs against main): 0 differences with the switch off, 0 changed values for calls the old parsers accepted. Its fixes are c6b31248, ebfd713f and (delta review nits) c85e84cf (union branch types, JSON kept for non-string types but never a top-level string, MiMo legacy-first, the old GLM refusal tables run with grammarOnly: false).

Validation (local, Xcode 27.2 / Swift 6.4.2, M3 Ultra)

  • Red first: 881963e5 (GLM5 tests) and cde37cd0 (the format matrix, SchemaOnlyToolCallTests) failed before their fixes; the matrix on main 0ed99d07 fails for GLM5 (every shape), Gemma (undeclared under additionalProperties: false) and MiMo V2 (union, mismatched type), single and parallel.
  • After (ebfd713f): swift test --filter 'MLXLMServerTests|\.UnitTests/|Tool' 811 + 339 + 59 Swift Testing and the XCTest suites passed.
  • scripts/check_forkdiff.py OK. Formatting: Xcode 27.2 swift format on the changed Swift files (the Lint job's check). pre-commit run --all-files also reformats ~350 files main does not format; those changes were discarded.

DevNet

d-inference#2157 (pinning c85e84cf) is merged into dev → DevNet 0.10.13-dev.branch.188. W1 on pair-2: PASS, no defects — ON 330/330 single-Mac + 24/24 pair responses returned every schema-breaking call as tool_calls (Gemma, GPT-OSS, Qwen 3.5-9B / 3.8, Bonsai; stream + non-stream); DARKBLOOM_GRAMMAR_ONLY_TOOL_PARSING=0 brought Gemma's refusal back. GLM5/MiMo models were not loadable on that pair, so they ran on a 256 GB M5 Ultra (same DevNet build): GLM-5.3-Flash 110/110 and MiMo V2.6 90/90 with every schema-breaking call (the issue's repro, a parallel batch with one bad call, undeclared and mistyped arguments) returned as tool_calls; with the switch off, GLM reproduced the issue's markup + finish_reason: stop byte for byte and MiMo its 500.

Checklist

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure: code, tests, commit messages and this description were written by an AI agent (Claude Opus 5.5 in OpenCode) for the repository owner; the attestation above is left for the owner.

jonathan308 and others added 8 commits November 14, 2026 22:37
A GLM-5.3-Flash frame that follows the tool-call grammar but omits a
required argument, carries an argument the schema does not declare, or
gives a parameter no usable type is refused today, so the provider
returns its markup as text (d-inference#2156). These tests expect the
call the model made; frames that break the grammar still fail closed.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
GLM5ToolCallParser refused a well-formed frame whose call left out a
required argument, carried an argument the schema does not declare, or
filled a parameter without a usable type, so servers returned the frame
as text (d-inference#2156). Its header already said parsing is not
schema validation.

The parser now checks the grammar and the declared function name only
and returns such a call as generated. A value declared only as string
stays raw text; a value of another declared type is JSON when it
matches that type; any other value is JSON when its raw text parses
(for a union with string, only as one of the other types), else that
raw text, as vLLM and SGLang read it. Without a type, a value ends at
its first </arg_value>. Grammar failures (open or nested frames,
misplaced tags, a repeated argument, an undeclared function) still fail
and still refuse the whole batch at EOS.

Tests that pinned the old refusals now pin the returned values.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
GLM5ToolCallParser.argumentTypes(_:parameters:) returns the JSON types a
declaration gives an argument (the exact declared key's schema, else
additionalProperties, plus a nullable null). They only choose between a
value's raw and JSON readings. d-inference's GLM tool-suffix lexer reads
frame boundaries the way this parser does and now takes the same types
from here instead of keeping its own copy of the rule.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
… (red)

One matrix over the served formats (Gemma, Harmony, Qwen 3.5, XML,
Nemotron, MiMo V2, GLM5, JSON), whole and streamed, alone and beside
valid calls: a call that omits a required argument, adds an undeclared
one (additionalProperties absent or false), fills an untyped, a nullable
union or a mismatched parameter keeps its format's grammar, so it is
returned as generated. Red today for Gemma (an undeclared argument under
additionalProperties false) and MiMo V2 (a union or a value of another
type); GLM5 passes after the previous commits (red on main).

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
The matrix test found two more parsers that refuse a well-formed call
for its schema: Gemma (an undeclared argument under
additionalProperties: false; the provider then shows the frame as text)
and MiMo V2 (a union or a value of another type; the provider then
fails the response with a 500).

- GemmaFunctionParser no longer refuses undeclared arguments.
- MiMoV2ToolCallParser reads a value of a declared type other than
  string as JSON when it parses as one (for a union with string, only as
  one of the other types), else as its raw text; an untyped value stays
  raw text, as before.
- GrammarOnlyToolParsing (DARKBLOOM_GRAMMAR_ONLY_TOOL_PARSING; unset or
  any other value on, 0/off/false/no off) switches the three parsers
  back to their schema refusals. GLM5ToolCallParser, GemmaFunctionParser
  and MiMoV2ToolCallParser take grammarOnly in init, defaulting to it;
  GLM5's refusals are re-checked after a parse with the old rules.
- Tests: the matrix is green; each parser's switch-off refusals, the
  environment values, and the Gemma and MiMo codec tests that pinned
  the refusals now pin the returned values.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
… switch test of its own

Behavior-preserving cleanup from the dedicated refactor pass:
GLM5ToolCallParser's two copies of the seven JSON type names become
jsonTypeNames; MiMoV2ToolCallParser's doc comment states its three
value cases plainly; the environment checks of GrammarOnlyToolParsing
leave the per-shape test for their own test.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…it read

From the independent review of #434 + d-inference#2157:

- GLM5ToolCallParser.argumentTypes reads anyOf/oneOf branch types, as
  MiMo does, so Pydantic's Optional[str] keeps 123, 1.0, true and "q"
  as text (finding 5).
- Where no string is declared, a JSON value that is not a string keeps
  its JSON reading even if its type differs, so an integer-typed
  {"cmd":"x </arg_value> y"} is that object instead of a failed frame;
  a top-level JSON string is never kept, so it cannot run across frames
  (finding 2).
- MiMoV2ToolCallParser reads every value it accepted before exactly as
  before and only reads the values it refused (unions, other types) as
  generated: a typeless nullable 1.0 stays "1.0" (finding 6).
- Tests: the old fail-closed tables run against GLM5ToolCallParser
  (grammarOnly: false) (finding 4); Pydantic optional strings; the two
  value-boundary cases; MiMo values that parsed before.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
…nion test

The header comment now says what the reader does since the review fixes
(JSON kept for a declared non-string type, or any non-string JSON where
no string is declared; types from argumentTypes). A test pins that a
string-or-null value split as {"a":" and "} across two frames reads as
two raw strings.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@jonathan308
jonathan308 marked this pull request as ready for review November 15, 2026 10:53
@jonathan308
jonathan308 merged commit fe9b528 into main Nov 15, 2026
21 checks passed
jonathan308 added a commit that referenced this pull request Nov 15, 2026
d-inference dev (bd67dec73, #2157) pins mlx-swift-lm at #434's head. The
provider's Muse Glimmer stack must build on the same SDK, so this branch
takes #434 until it merges into main.

Co-Authored-By: Claude <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 15, 2026
#434 (c85e84c) merged into main as fe9b528, whose tree equals c85e84c's. This branch already
merged c85e84c, so no file changes. d-inference dev (#2158) now pins main fe9b528, and the stack's
SDK pin must contain it.

Co-Authored-By: Claude <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 15, 2026
jonathan308 added a commit that referenced this pull request Nov 15, 2026
…#434's merge)

Co-Authored-By: Claude <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 15, 2026
jonathan308 added a commit that referenced this pull request Nov 15, 2026
…#434's merge)

Co-Authored-By: Claude <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 15, 2026
jonathan308 added a commit that referenced this pull request Nov 15, 2026
…#434's merge)

Co-Authored-By: Claude <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 15, 2026
jonathan308 added a commit that referenced this pull request Nov 15, 2026
…#434's merge)

Co-Authored-By: Claude <noreply@anthropic.com>
jonathan308 added a commit that referenced this pull request Nov 15, 2026
jonathan308 added a commit that referenced this pull request Nov 15, 2026
…#434's merge)

Co-Authored-By: Claude <noreply@anthropic.com>
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.

1 participant