Repository navigation
fix(gpt-oss): Reject failed GPT-OSS grammars for mandatory tool choices - #5056
Merged
silencelamb merged 1 commit intoOct 24, 2026
Merged
Conversation
windreamer
requested review from
windreamer
and
a balanced review from Copilot
October 23, 2026 11:15
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The focused validation change covers all mandatory forms, preserves automatic fallback, and includes targeted regression coverage.
No open issues 🎉
What changed in this PR
Completes the GPT-OSS grammar-failure handling introduced in #4997 so mandatory tool choices cannot fall back to unconstrained generation.
Changes:
- Reject failed grammar construction for named functions and required allowed-tool choices.
- Preserve prompt-only fallback for automatic modes and clarify the parser docstring.
- Expand regression coverage to five tool-choice modes, with and without a structured response format.
| File | Description |
|---|---|
tests/test_lmdeploy/serve/parsers/test_gpt_oss_parser.py |
Covers mandatory rejection, automatic fallback, and response-format prompt injection. |
lmdeploy/serve/parsers/_openai_harmony.py |
Extends mandatory-choice validation and documents grammar-failure behavior. |
Review effort: Balanced
Collaborator
|
LGTM |
windreamer
approved these changes
Oct 23, 2026
silencelamb
approved these changes
Oct 24, 2026
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.
Motivation
The GPT-OSS parser rejects
tool_choice="required"when XGrammar cannot build the tool grammar. The equivalent mandatory forms, a named function andallowed_tools.mode="required", currently fall back to unconstrained generation. A request that requires a tool call can therefore proceed with a plain-text response.This completes the grammar-failure behavior introduced in #4997.
Modification
ToolChoiceand requiredAllowedToolChoicealongside the stringrequiredwhen grammar construction fails.autoandallowed_tools.mode="auto", including response-format prompt injection.Validation
aba987adproduced 4 failed / 6 passed. Both named-function and allowed-required modes failed with and without a response format.python -m pytest tests/test_lmdeploy/serve/parsers/test_gpt_oss_parser.py tests/test_lmdeploy/serve/parsers/test_required_tool_choice.py -q: 110 passed.git diff --checkpassed.These are parser-level CPU tests on Python 3.13.13 with real LMDeploy imports and XGrammar 0.2.8. The existing Harmony test fixture supplies a scripted token parser, and the regression simulates grammar construction failure. GPU model inference was not exercised.
Checklist