Skip to content

feat: add support for GBNF grammar - #20

Open
oglego wants to merge 2 commits into
mozilla-ai:mainfrom
oglego:feature/support-gbnf-grammar
Open

feat: add support for GBNF grammar#20
oglego wants to merge 2 commits into
mozilla-ai:mainfrom
oglego:feature/support-gbnf-grammar

Conversation

@oglego

@oglego oglego commented Aug 21, 2026

Copy link
Copy Markdown

Summary

This PR partially addresses #18 - adding support for GBNF grammar. I believe that I have everything for this wired in correctly, I also tried to follow the current coding style as best as I could, but if anything needs to be changed please let me know.

Changes

The main items that I have added on this PR are:

  • ModelConfig gains grammar (GBNF string) and grammar_root (rule name, defaults to "root").

  • load_grammar_file() reads a .gbnf file into ModelConfig::grammar.

  • initialize_context() applies the grammar to the sampler chain when set.

  • examples/grammar/ — minimal sentiment classifier demonstrating grammar-constrained output.

  • tests/test_grammar.cpp — config defaults, file loading, missing-file error handling.

For testing this locally and interacting with it I just used the recommended model (granite-4.0-micro-Q8_0.gguf).

Thanks in advance for the review!

oglego added 2 commits August 19, 2026 17:23
fix: update comment on GBNF grammar

fix: update comments in model.h for GBNF grammar

test: update test suite for GBNF grammar support

refactor: clean up grammar test formatting

feat: update GBNF grammar support with example and reset fix

Update GBNF grammar support to ModelConfig via a `grammar` string and
`grammar_root` rule name, applied to the sampler chain in
initialize_context(). load_grammar_file() reads a .gbnf file into
ModelConfig::grammar.

The grammar sampler retains its parse position across calls to
generate_from_tokens(), so a completed grammar from one turn forces
EOS immediately on the next. Reset only the grammar sampler (not the
whole chain) at the start of each turn, via a non-owning pointer
kept on Model, so the dist sampler's RNG and other stateful samplers
are left untouched when an explicit seed is configured.

Add examples/grammar, a minimal sentiment classifier constrained to
{"sentiment": "positive"|"negative"|"neutral"} via sentiment.gbnf,
demonstrating the feature end-to-end. Document grammar-constrained
output in the README and register the example in CMakeLists.txt.

fix: allow custom grammar roots in grammar example

Add a -r flag to the grammar demo, update the README usage examples,
and polish the grammar documentation wording.

fix: comment formatting in model.cpp

fix: update formatting for comments in model.h
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