Skip to content

common/peg : implement ac parser for stricter grammar generation#24869

Merged
aldehir merged 5 commits into
ggml-org:masterfrom
aldehir:ac-parser
Jun 21, 2026
Merged

common/peg : implement ac parser for stricter grammar generation#24869
aldehir merged 5 commits into
ggml-org:masterfrom
aldehir:ac-parser

Conversation

@aldehir

@aldehir aldehir commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Overview

Even after #24839, users are still seeing the model escape the grammar. It's because the exclusion grammar can accept a partial prefix of the delimiter and each use of until(delim) is typically followed by a literal(delim).

This PR uses the same AC type and adds an including variant: consume all characters up to and including a delimiter. This way it terminates on first occurrence of \n</parameter>\n.

Fixes #24863

Additional information

New parser:

// Wraps a child parser but emits a GBNF grammar built from the Aho-Corasick
// automaton of `delimiters`, matching everything up to and including the
// first delimiter. Parsing delegates entirely to the child, which is
// responsible for consuming the delimiter (e.g. until(D) + literal(D)).
common_peg_parser ac(const common_peg_parser & p, const std::vector<std::string> & delimiters) {
    if (delimiters.empty()) {
        throw std::runtime_error("ac parser requires at least one delimiter");
    }
    return add(common_peg_ac_parser{p, delimiters});
}
common_peg_parser ac(const common_peg_parser & p, const std::string & delimiter) { return ac(p, std::vector<std::string>{delimiter}); }

Requirements

@jschmied

Copy link
Copy Markdown
Contributor

Tested, seams to work

@aldehir aldehir merged commit 52b3df0 into ggml-org:master Jun 21, 2026
25 checks passed
@mtasic85

Copy link
Copy Markdown
Contributor

This might be expected behavior, but I still occasionally get in pi (0.79.9) and Qwen3.6 27B:

 <tool_call>
 <function=bash>
 <parameter=command>
 git fetch
 </parameter>
 </function>
 </tool_call>
 ./build/bin/llama-server --version
version: 9755 (0ef6f06d5)
built with GNU 16.1.1 for Linux x86_64

@aldehir

aldehir commented Jun 22, 2026

Copy link
Copy Markdown
Contributor Author

@mtasic85 appears to be a well formed tool call. Do you see this in the content or thinking?

@mtasic85

Copy link
Copy Markdown
Contributor

This is seen in the content. Thinking was disabled.

@aldehir

aldehir commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

@mtasic85 can you provide any verbose logs of the server when this occurs? I'm having a hard time finding a way to reproduce. I don't have a reason to believe it's related to the change here but may be related to the earlier whitespace updates.

papamoose pushed a commit to papamoose/llama.cpp that referenced this pull request Jun 27, 2026
…l-org#24869)

* common/peg : implement ac parser

* cont : extract functions

* cont : tidy up

* cont : remove a test

* cont : move ac() def
adrianhoehne pushed a commit to adrianhoehne/llama.cpp that referenced this pull request Jul 5, 2026
…l-org#24869)

* common/peg : implement ac parser

* cont : extract functions

* cont : tidy up

* cont : remove a test

* cont : move ac() def
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Everything test related

Projects

None yet

5 participants