Add constrained decoding for JSON schema response formats#247
Add constrained decoding for JSON schema response formats#247pminervini wants to merge 1 commit into
Conversation
|
Q: why not e.g. https://github.com/mlc-ai/xgrammar or https://github.com/guidance-ai/llguidance ? @fry69 See convo in #210 -- happy to ditch this and e.g. add -DENABLE_LLGUIDANCE ( |
37a250c to
810a936
Compare
|
I tested this PR on an M5 Max 128GB and it works well for the main structured-output cases. Environment:
What passed:
One small edge case:
I opened a small follow-up PR against this branch: The fix only masks pure whitespace tokens while the output buffer is still only whitespace. Once JSON has started, whitespace is allowed normally. |
|
hey! hope you guys won't mind, I think this may be a better way of enabling support for structured outputs in ds4: pminervini#1 (comment) -- will try to look into this over the weekend |
|
Let me close this, will try to propose a better one. |
|
FYI: I found some time to toy around with structured outputs and checked this PR out (apparently Looking forward for another stab at this. |
Tentative fix for #210.
This adds a small decoder for OpenAI-compatible
response_format/ Responsestext.formatJSON modes: during generation, tokens that would cause the current output to stop being a valid prefix of the requested JSON object/schema are masked out.This is roughly the same idea as in llama.cpp’s JSON-schema grammar path, where the schema guides the decoding process. Right now it supports
/v1/chat/completionsand/v1/responses, handlesjson_objectandjson_schema, disables thinking for structured JSON outputs (not sure about this, will check how e.g. vllm handles this for LRMs), and keeps the schema out of the prompt ("follow this JSON schema" also seems to work for simple schemas -- example in the issue -- but I would not trust that too much).Will share a script to stress-test this with schemas of varying complexity.