Matching the Grammar

Some Large Language Model runners support formal grammars. It can be used to force the output to follow a certain structure (for example, speaking only in emojis or outputting just the valid moves from the Portable Game Notation).

It still does not guarantee that the output will be valid (in a semantic sense), but at least matching the formal grammar guarantees it will follow the correct structure.

One of the popular uses is to force a Large Language Model to match a specific JSON Schema.

llama.cpp

llama.cpp supports GBNF formal grammars, which is an extension of Backus-Naur form, but with the support of some regular expressions.