Skip to content

Add constrained decoding for JSON schema response formats#247

Open
pminervini wants to merge 1 commit into
antirez:mainfrom
pminervini:issue-210-structured-json
Open

Add constrained decoding for JSON schema response formats#247
pminervini wants to merge 1 commit into
antirez:mainfrom
pminervini:issue-210-structured-json

Conversation

@pminervini
Copy link
Copy Markdown

@pminervini pminervini commented May 25, 2026

Tentative fix for #210.

This adds a small decoder for OpenAI-compatible response_format / Responses text.format JSON 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/completions and /v1/responses, handles json_object and json_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.

@pminervini
Copy link
Copy Markdown
Author

pminervini commented May 25, 2026

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

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