Skip to content

Commit 10ac60b

Browse files
committed
Resolves #3878 by enforcing existence of root node before returning valid grammar structure. This is an alternate fix location to put the required root node in sampling.cpp instead of grammar-parser.cpp.
1 parent 8030da7 commit 10ac60b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

common/sampling.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_
1717
return nullptr;
1818
}
1919

20+
// Ensure that there is a "root" node.
21+
if (result->parsed_grammar.symbol_ids.find("root") == result->parsed_grammar.symbol_ids.end()) {
22+
fprintf(stderr, "%s: grammar does not contain a 'root' symbol\n", __func__);
23+
delete result;
24+
return nullptr;
25+
}
26+
2027
std::vector<const llama_grammar_element *> grammar_rules(result->parsed_grammar.c_rules());
2128

2229
result->grammar = llama_grammar_init(

0 commit comments

Comments
 (0)