mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[GH-ISSUE #3758] [Engine] Grammar-constrained decoding for json_schema, so an agent's answers always fit its schema #1581
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#1581
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @qiuzan2001 on GitHub (Oct 22, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/3758
Area
[Engine] (speed, kernels, models, the server and API)
What do you want to do?
I want to implement grammar-constrained decoding for
response_format: json_schemamyself, if you agree with the scope. This issue asks whether you want it in Strata and how it should be built. I will not send a large pull request before you answer.Why: I run a browser agent (browser_use) on Strata. It sends a JSON schema with every request and needs the model's answer to fit that schema every time.
Today Strata asks for the schema in the prompt and checks the answer afterwards. The engine has no grammar decoder (
serve/structured.py, docs/DETAILS.md "schema prompting followed by server validation"). So a low-bit model sometimes writes JSON of the wrong shape, and the agent has to retry.What happened on my PC (counted from the agent's own log, one run of about 90 agent steps):
{"keys":"Escape"}where{"send_keys":{"keys":"Escape"}}is correct). Orthinkingcomes back as an object instead of a string and the action field is missing.I want an answer that fits the schema by construction: while the model writes the answer, only tokens that keep it valid are allowed. Speed should stay close to today's, so the MTP and suffix drafts should still work under the constraint.
Setup: Strata 0.2.2 (engine 0.1.47), Swift 1.5 IQ3_S, RTX 5070 Ti 16 GB, 76 GB RAM, Ubuntu, CUDA 13.1.
Side note: on my install the Python package
jsonschemais not installed (it is not in requirements.txt or setup.py), so the server only checks that the answer is one JSON object. Wrong-shape answers pass the server and fail in the client. Installing it makes the server catch them, but it does not make the model write the right shape, so it does not remove the retries.What do you do today instead?
Nothing that removes the failures. The agent retries. Lower temperature and a smaller action schema should help, but they cannot guarantee a valid answer.
Earlier work and questions
This was asked before. Those threads were closed in the 0.2.0-beta tracker reset (closed, not rejected): #2368 (tracking issue), #334, #1328 (ncphri's token masks that keep the MTP drafts under the mask), #1431, #976. I also saw that someone is working on llguidance support in a fork. I am not asking you to reopen a declined proposal. I would like to continue this work, with new evidence from a real agent workload.
Questions, so that I do not build the wrong thing:
If you agree, I will keep this thread updated and open a draft pull request for the CUDA version first. Before it is ready for review I will measure: how many answers fit the schema with and without the mask on a set of agent-style requests, decode speed with and without drafts under the mask, and that greedy output is identical when the unmasked answer was already valid.
@Cal1sto commented on GitHub (Oct 31, 2026):
Second data point + implementation-level findings from another box (Strata 0.1.45, RTX 5090, Qwen3.8-Flash-Next IQ3_S, 512K context). Happy to see this being picked up — here is what our code reading and an outside-the-engine prototype established, which I think changes the shape of the answer:
The engine is closer than the docstring suggests — no new GPU kernel needed.
src/core/verify.cpp:5005) over candidate lists capped atkSelMax= 64 (include/strata/core/spec_prob.hpp). A grammar mask at that point is a host-side set intersection on <= 64 entries — no device traffic per step.Verifier::set_logit_bias(verify.cpp:759), andinclude/strata/core/logit_bias.hpptreats-100as a hard exclusion (tested there to 103k bans).What we measured today over the HTTP surface (automaton compiled from JSON-schema, full real vocabulary of 248,320 tokens, per-token byte-level validation, 13/13 automaton unit tests):
logit_bias: semantically correct, but 39.1 s per token (3.7 MB payload, 248k banned ids). A dead end outside the engine — and exactly the argument for doing it inside, where the mask is free at the sampling site./v1/chat/completionsis impossible (the chat template closes the assistant turn: prefilling{"ville":makes the model restart from scratch). So token-level constraint cannot be bolted on client-side at all today; it has to live server-side.Demand nuance, counted honestly: on this box, 0 of 3,091 logged requests used
response_format— our agents go through native tool calls. The win materializes once clients adopt the guarantee; the guarantee requires this issue's work.Side note, cheap fix for anyone validating schemas server-side:
jsonschemawas missing from the server venv and the validation silently degraded to "is a JSON object" (warning logged once). Worth listing as an actual dependency ofstructured.py.@OP if you're still planning this: the host-side sampling site is where I'd put the automaton — same point
logit_biasalready reaches, behindVerifier::set_logit_bias. That keeps the PR small and kernel-free, which seems like the kind of patch this repo would merge.@Have-Purple commented on GitHub (Nov 4, 2026):
Measured cost on a 45-run agent suite (15 tasks × 3 reps) at 128K, two packs, temperature 0:
the task. Malformed replies also occurred at 64K, so context length is not the cause.
ambiguousandbudget, both tasks where the reply is prose plus a tool callrather than a short answer.
That is the practical argument for this: the model can complete the task, but the harness cannot
parse the answer. A grammar would remove the class rather than penalise it.