Repository navigation
feat(server): enforce response_format json_schema with token masks - #937
Draft
RaunakGengiti2725 wants to merge 1 commit into
Draft
RaunakGengiti2725 wants to merge 1 commit into
RaunakGengiti2725 wants to merge 1 commit into
Conversation
A json_schema request was a 400. The schema now compiles to a byte DFA and a per-state token mask is applied to the sampled row and to every verify row, so MTP drafts stay on and a draft the schema forbids is rejected as a mismatch. Batches without a constrained request build no mask. A constrained request sits out a launch while its last token is in flight, and stays free inside a thinking span its prompt leaves open until the close tag. Over-cap schemas are a 400.
This branch has not been deployed
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.
Implements #927.
The schema compiles to a byte automaton; each sampled row allows only tokens that keep the reply a valid prefix, and end-of-sequence once complete. Under MTP each verify row is masked from its drafts' state, so drafts stay on. An open thinking span stays unmasked until its close tag. Unsupported keywords,
json_objectand schemas over 50,000 states are a 400 (docs/cli.md). Without MTP, a constrained request skips a launch while its last token is in flight.A/B against main 2fb371e. RTX 5090 (575 W), AMD EPYC 9354, driver 615.71.09. Both arms:
ft serve --model nvidia/Qwen3.6-35B-A3B-NVFP4 --enable-timings-report --moe-strategy fused --max-running-requests 1 --speculative-algorithm mtp --speculative-num-steps 3. Per server: one JSON extraction prompt, 3 times plain and 3 with a 5-field json_schema, greedy, thinking off, max_tokens 512; one interleaved run, 2 rounds reversed.Risk: the scheduler thread compiles each schema on first use, stalling running requests; first schema requests ran at 104.6 / 97.1 tok/s, later ones 536.5 to 545.5. 63-token replies; rates include fixed costs.
Not measured: the no-MTP path, other quality checks. Measured at 9617440.
Tests (no GPU): decode and verify masks (
tests/spec/test_rejection.py), schema reachingSamplingParams(tests/server/test_openai_api.py), scheduler wiring (tests/scheduler/test_json_schema_scheduler.py: red without_plan_grammar,_track_grammargate or_grammars.pop).How to review. About 1,050 of the 1,637 added lines are code, 550 are tests and 38 are docs. The schema compiler is self-contained in
grammar/json_schema.py(+594) andgrammar/matcher.py(+279). The wiring is about 180 lines inserver/openai_api.py,scheduler/scheduler.py,engine/sample.pyandserver/reasoning_parser.py.Tested on real hardware by me.