chat : enforce string enums in Qwen XML tool arguments - #28668
Open
arc-uri-el wants to merge 1 commit into
Open
Conversation
Use allowed XML literals instead of the unrestricted string production. Require the parameter-close boundary when matching alternatives with shared prefixes. Cover rejected values, empty and escaped strings, parsing, streaming and ordinary-string behavior in the existing chat suite. Assisted-by: GPT-6 Astra
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.
Overview
The Qwen XML tool grammar accepts arbitrary strings for a parameter declared as
{"type":"string","enum":["list","load"]}. For example,<parameter=action>\nerase\n</parameter>is accepted even thougheraseis not an allowed value.The handler selects its unrestricted XML string rule whenever the parameter resolves to a string. It therefore bypasses the JSON converter that would enforce the enum for JSON arguments.
For a nonempty string enum, build the XML value rule from the allowed literal strings instead. Check the closing-parameter boundary when choosing each literal, so an earlier shorter entry does not consume the prefix of a longer one. Ordinary strings and non-string enums retain their existing paths. This patch adds no root-union support, schema adapter, public API, model or GPU change.
Additional information
Validation
Base:
91f6a6cf361385700bbe15981f0f39909df77498.Separate matching baseline/candidate Linux x86_64 builds used GCC 15.2, Release, shared libraries, native CPU and OpenMP. All seven selected parser/grammar suites passed on both builds, with zero compiler warnings:
test-chat,test-json-schema-to-grammar,test-chat-peg-parser,test-chat-auto-parser,test-chat-template,test-grammar-parserandtest-grammar-integration.The exact new
test-chatbinary fails against baseline libraries at the invalid-enum assertion (expected rejection, observed acceptance) and passes against candidate libraries. The added test covers Qwen3-Coder and Qwen3.5 templates, invalid strings, shared-prefix alternatives, empty strings, embedded quotes/newlines, parsed/streamed tool arguments, parallel calls and an unrestricted-string positive control.This replaces the broader root-union proposal in #28651 following the portability objection there. That representation can be adapted on the client side; ordinary enum enforcement is an independent defect. The same focused fix was submitted first as Halo staging #26.
Not run for this isolated patch: model inference, full CI, other-platform builds, perplexity or performance sweeps. No throughput or general model-quality improvement is claimed.
Requirements