Skip to content

Commit

Permalink
[Bug] Ensure grammar.match raises appropriately on incomplete input (
Browse files Browse the repository at this point in the history
…#1016)

Force-feed `ByteParser` an EOS token if it's not done by the time all
input has been consumed. This lets us raise an exception when a string
fails to complete a grammar
  • Loading branch information
hudson-ai authored Sep 10, 2024
1 parent c0a0bfb commit 418fc03
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions guidance/_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ def match(

try:
parser.consume_bytes(byte_string)
if not allow_partial:
parser.force_done()
except _parser.ByteParserException:
if raise_exceptions:
raise
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/test_grammar.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import guidance
from guidance import gen, models, optional, select, string
from guidance._parser import ByteParserException


def test_select_reset_pos():
Expand Down Expand Up @@ -132,4 +133,12 @@ def test_partial_match(self, string):
match = g.match(f"123{string}", allow_partial=True)
assert match is not None
assert match.partial
assert match.captures["mycap"] == string
assert match.captures["mycap"] == string

def test_raises_on_incomplete_input(self):
g = "123" + gen(regex=r"\d+x?", name="mycap")
# Ok since we allow partial
assert g.match(b"123", raise_exceptions=True, allow_partial=True) is not None
# Shold raise since we don't allow partial
with pytest.raises(ByteParserException):
g.match(b"123", raise_exceptions=True)

0 comments on commit 418fc03

Please sign in to comment.