Skip to content

feat(server): enforce response_format json_schema with token masks - #937

Draft
RaunakGengiti2725 wants to merge 1 commit into
FlashML-org:mainfrom
RaunakGengiti2725:pr/json-schema-decoding
Draft

RaunakGengiti2725 wants to merge 1 commit into
FlashML-org:mainfrom
RaunakGengiti2725:pr/json-schema-decoding

Conversation

@RaunakGengiti2725

@RaunakGengiti2725 RaunakGengiti2725 commented Nov 20, 2026 •

Copy link
Copy Markdown

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_object and 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.

main this PR
plain, decode tok/s, median (range) of 6 598.7 (577.5 to 603.9) 601.6 (600.2 to 602.1)
json_schema HTTP 400 x6 200 x6, 6 of 6 schema-valid, median 539.6 tok/s
MTP drafts accepted, per kind 282 of 288 (plain) 282 of 288 (both)

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 reaching SamplingParams (tests/server/test_openai_api.py), scheduler wiring (tests/scheduler/test_json_schema_scheduler.py: red without _plan_grammar, _track_grammar gate 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) and grammar/matcher.py (+279). The wiring is about 180 lines in server/openai_api.py, scheduler/scheduler.py, engine/sample.py and server/reasoning_parser.py.

Tested on real hardware by me.

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

No deployments
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