feat(vllm): add grammar and structured output support
Description
This PR fixes #6857
Adds grammar and structured output support to the vLLM backend, enabling users to enforce structured outputs via JSON schema, JSON object, and BNF/GBNF grammar constraints.
Problem
The vLLM backend ignored all structured output parameters:
- The
Grammarfield from the proto was never read - A phantom
GuidedDecodingmapping referenced a non-existent proto field response_formatwithjson_schemaorjson_objecthad no effect on vLLM- Missing
import timecaused a runtime crash on video input
Solution
Proto (backend.proto):
- Added
JSONSchema(field 52) andResponseFormat(field 53) toPredictOptions, allowing backends to receive the raw JSON schema and format type natively
Go endpoints (chat.go, completion.go):
- Extract the raw JSON schema string from
response_format: {type: "json_schema", ...}and store it onconfig.JSONSchema - Set
config.ResponseFormatto the format type (json_object/json_schema) - GBNF grammar is still generated in parallel for llama.cpp compatibility
- Added
json_schemagrammar support to the completion endpoint (was missing)
Go backend (options.go, model_config.go):
- Pass
JSONSchemaandResponseFormatthroughgRPCPredictOptsto backends
vLLM backend (backend.py):
- Support both
StructuredOutputsParams(vLLM latest) andGuidedDecodingParams(vLLM <=0.8.x) with graceful import fallback - Handle three structured output modes with priority:
JSONSchema>json_object>Grammar - Fix missing
import timeandimport json - Remove phantom
GuidedDecodingmapping
Docs (constrained_grammars.md):
- Updated compatibility notice to include vLLM
- Added vLLM section with examples for JSON schema, JSON object, and grammar
How It Works
User request (response_format or grammar parameter)
→ chat.go/completion.go: extracts raw JSON schema → config.JSONSchema
→ also generates GBNF grammar → config.Grammar (for llama.cpp compat)
→ options.go: passes both via gRPC PredictOptions
→ vLLM backend: uses StructuredOutputsParams/GuidedDecodingParams
→ llama.cpp backend: uses Grammar (GBNF) as before — no regression
Verification
- xgrammar (used by vLLM) explicitly follows the GBNF spec from llama.cpp, so grammar passthrough works
GuidedDecodingParams.jsonandStructuredOutputsParams.jsonboth accept JSON stringsgrammar_is_likely_lark()in vLLM correctly identifies GBNF as non-Lark (via::=detection)- No conflict with existing
config.ResponseFormatusage (image endpointb64_jsongoes through a different code path)
Notes for Reviewers
- The proto generated Go files are gitignored and built at compile time — only
backend.protois committed - Compatible with both vLLM <=0.8.x (
GuidedDecodingParams/guided_decoding) and latest (StructuredOutputsParams/structured_outputs) - No regression for llama.cpp or other backends
Signed commits
- [x] Yes, I signed my commits.
@mudler
Deploy Preview for localai ready!
| Name | Link |
|---|---|
| Latest commit | 278e7e25de9799ba90cd8475928650bcd6e576c2 |
| Latest deploy log | https://app.netlify.com/projects/localai/deploys/69b454077c0aee00078d12a0 |
| Deploy Preview | https://deploy-preview-8806--localai.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify project configuration.
GM @mudler Would you review my PR? Thank you for your time
Hi @localai-bot would you review this PR? Thank you
Hi @richiejp @sozercan would you review my PR if you have a moment? Thank you
Also can you create e2e tests for this?
Also can you create e2e tests for this?
Would you review again?
this sounds like an important/cool feature
Also can you create e2e tests for this?
Would you review again?
I still don't see any tests? You also need to rebase on master and see my other comment. Possibly related #9328 @mudler.
this is irrelevant now since https://github.com/mudler/LocalAI/commit/daa0272f2e692f5836153684f535bacc61e2c4f8 and https://github.com/mudler/LocalAI/commit/d67623230f519433ef866f67901065f9a82dc9cb i think
Heads-up from testing on vLLM 0.23.0 (arm64 / CUDA 13): I hit the same GuidedDecodingParams → StructuredOutputsParams rename this PR handles. The existing request.Grammar path is actually silently broken on >= 0.23 — the from vllm.sampling_params import GuidedDecodingParams import fails, so HAS_GUIDED_DECODING is False and the whole guided-decoding block is skipped (response_format ignored, unconstrained output).
I opened #10339 with a minimal fix that adapts the existing path to the renamed class/field (with a fallback for older vLLM). It looked complementary to the native-json_schema-via-metadata direction here — I noticed this PR adds _structured_output_cls / _structured_output_field but doesn't consume them yet, so on 0.23 it wouldn't enforce on its own. Happy to consolidate however you'd prefer.
I'm not sure if we need this one after https://github.com/mudler/LocalAI/pull/10343 ?
Hi Richie — these two PRs address different layers of the same area, but #10343 is functionally sufficient on its own:
#10343 is a compatibility hotfix. The import failure on vLLM ≥0.23 meant the existing request.Grammar path was silently bypassed and response_format: json_schema returned unconstrained text. Just verified on production (v4.5.0, vLLM 0.23.0): the request now returns schema-conformant JSON correctly. Six-line patch, kept the existing GBNF-conversion route alive.
#8806 adds a native JSON-Schema pass-through (new proto fields JSONSchema + ResponseFormat, Go endpoints fill them, backend uses them directly). That's a quality/efficiency improvement over the GBNF detour, plus it adds the missing import time for the video path — but it's not required for functional structured-output support.
So: if the goal is "structured outputs work" → #10343 already does that. If the goal is "vLLM's native structured_outputs gets the raw JSON-Schema instead of a GBNF translation" → #8806 is the right architectural step. That's a maintainer call — I don't have a stake either way. Just flagging that #8806 will need a rebase since backend.py has moved (my #10343/#10351 and the related volume patches are now upstream).
— Poseidon