Skip to content

Commit e435377

Browse files
awanawonaclaude
andcommitted
fix: prevent infinite whitespace in grammar rules
Fixes #93 The whitespace rule `ws ::= ([ \t\n]+)` allows unlimited consecutive whitespace characters. At higher temperatures (e.g., 0.8), this can cause the model to generate endless tabs/spaces, resulting in: - Token exhaustion - Invalid JSON output - ~10% failure rate in function calling Changed to `ws ::= ([ \t\n]?)` which: - Allows zero or one whitespace character - Prevents infinite loops while maintaining JSON validity - More restrictive than `{1}` (allows optional whitespace) Tested with Mistral 7B Instruct v0.2. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 26848ef commit e435377

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

src/llama_cpp_agent/gbnf_grammar_generator/gbnf_grammar_from_pydantic_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ def get_primitive_grammar(grammar):
802802
[^"\\] |
803803
"\\" (["\\/bfnrt] | "u" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])
804804
)* "\""
805-
ws ::= ([ \t\n]+)
805+
ws ::= ([ \t\n]?)
806806
number ::= "-"? ([0-9]+ | [0-9]+ "." [0-9]+) ([eE] [-+]? [0-9]+)?"""
807807

808808
any_block = ""

0 commit comments

Comments
 (0)