Skip to content

fix: prevent infinite whitespace in grammar rules (fixes #93)#95

Open
awanawana wants to merge 1 commit intoMaximilian-Winter:masterfrom
awanawana:fix/grammar-whitespace-infinite-loop
Open

fix: prevent infinite whitespace in grammar rules (fixes #93)#95
awanawana wants to merge 1 commit intoMaximilian-Winter:masterfrom
awanawana:fix/grammar-whitespace-infinite-loop

Conversation

@awanawana
Copy link
Copy Markdown

Summary

  • Fixes the grammar bug causing infinite whitespace generation in function calling
  • Changes ws ::= ([ \t\n]+) to ws ::= ([ \t\n]?) to prevent token exhaustion

Problem

As reported in #93, the current whitespace rule allows unlimited consecutive whitespace:

ws ::= ([ \t\n]+)

At temperature 0.8, this causes ~10% of function calls to fail with endless \t repetition, producing invalid JSON.

Solution

Changed to:

ws ::= ([ \t\n]?)

This allows zero or one whitespace character:

  • Prevents infinite loops
  • Maintains JSON validity (whitespace is optional in JSON)
  • More permissive than {1} (exactly one) for edge cases where no whitespace is needed

Testing

Related

Fixes #93

🤖 Generated with Claude Code

Fixes Maximilian-Winter#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>
@pabl-o-ce
Copy link
Copy Markdown
Contributor

@Maximilian-Winter peace and friendship!
ma dude!

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.

Grammar bug with function calling

2 participants