LocalAI icon indicating copy to clipboard operation
LocalAI copied to clipboard

feat(vllm): add grammar and structured output support

Open fury0928 opened this issue 8 months ago • 9 comments

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 Grammar field from the proto was never read
  • A phantom GuidedDecoding mapping referenced a non-existent proto field
  • response_format with json_schema or json_object had no effect on vLLM
  • Missing import time caused a runtime crash on video input

Solution

Proto (backend.proto):

  • Added JSONSchema (field 52) and ResponseFormat (field 53) to PredictOptions, 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 on config.JSONSchema
  • Set config.ResponseFormat to the format type (json_object / json_schema)
  • GBNF grammar is still generated in parallel for llama.cpp compatibility
  • Added json_schema grammar support to the completion endpoint (was missing)

Go backend (options.go, model_config.go):

  • Pass JSONSchema and ResponseFormat through gRPCPredictOpts to backends

vLLM backend (backend.py):

  • Support both StructuredOutputsParams (vLLM latest) and GuidedDecodingParams (vLLM <=0.8.x) with graceful import fallback
  • Handle three structured output modes with priority: JSONSchema > json_object > Grammar
  • Fix missing import time and import json
  • Remove phantom GuidedDecoding mapping

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.json and StructuredOutputsParams.json both accept JSON strings
  • grammar_is_likely_lark() in vLLM correctly identifies GBNF as non-Lark (via ::= detection)
  • No conflict with existing config.ResponseFormat usage (image endpoint b64_json goes through a different code path)

Notes for Reviewers

  • The proto generated Go files are gitignored and built at compile time — only backend.proto is 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

fury0928 avatar Mar 06 '26 02:03 fury0928

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

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify avatar Mar 06 '26 02:03 netlify

GM @mudler Would you review my PR? Thank you for your time

fury0928 avatar Mar 06 '26 12:03 fury0928

Hi @localai-bot would you review this PR? Thank you

fury0928 avatar Mar 09 '26 13:03 fury0928

Hi @richiejp @sozercan would you review my PR if you have a moment? Thank you

fury0928 avatar Mar 12 '26 12:03 fury0928

Also can you create e2e tests for this?

richiejp avatar Mar 13 '26 16:03 richiejp

Also can you create e2e tests for this?

Would you review again?

fury0928 avatar Mar 13 '26 18:03 fury0928

this sounds like an important/cool feature

detain avatar Apr 12 '26 08:04 detain

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.

richiejp avatar Apr 13 '26 10:04 richiejp

this is irrelevant now since https://github.com/mudler/LocalAI/commit/daa0272f2e692f5836153684f535bacc61e2c4f8 and https://github.com/mudler/LocalAI/commit/d67623230f519433ef866f67901065f9a82dc9cb i think

detain avatar Apr 14 '26 00:04 detain

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.

pos-ei-don avatar Jun 15 '26 03:06 pos-ei-don

I'm not sure if we need this one after https://github.com/mudler/LocalAI/pull/10343 ?

richiejp avatar Jun 24 '26 08:06 richiejp

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

pos-ei-don avatar Jun 24 '26 08:06 pos-ei-don