diff --git a/guidance/_guidance.py b/guidance/_guidance.py index 510cac9e0..67cab91af 100644 --- a/guidance/_guidance.py +++ b/guidance/_guidance.py @@ -53,15 +53,20 @@ def wrapped(*args, **kwargs): if no_args: f._self_call_placeholder_ = Placeholder() - # call the function to get the grammar node - node = f(_null_grammar, *args, **kwargs) - if not isinstance(node, (Terminal, str)): - node.name = f.__name__ - - # replace all the placeholders with our generated node - if no_args: - replace_grammar_node(node, f._self_call_placeholder_, node) - del f._self_call_placeholder_ + try: + # call the function to get the grammar node + node = f(_null_grammar, *args, **kwargs) + except: + raise + else: + if not isinstance(node, (Terminal, str)): + node.name = f.__name__ + # replace all the placeholders with our generated node + if no_args: + replace_grammar_node(node, f._self_call_placeholder_, node) + finally: + if no_args: + del f._self_call_placeholder_ return node diff --git a/tests/unit/test_decorator.py b/tests/unit/test_decorator.py index 9c38feba5..62042f814 100644 --- a/tests/unit/test_decorator.py +++ b/tests/unit/test_decorator.py @@ -1,5 +1,6 @@ +import pytest import guidance -from guidance import gen, models +from guidance import gen def test_dedent_basic(): """Test that dedent functionality in f-strings works across Python versions.""" @@ -133,4 +134,16 @@ def inconsistent_indentation(lm): # lm = guidance.models.Mock() # result = lm + outer_function() -# assert result == "Inner function variable:\nouter_var: outer_value\n" \ No newline at end of file +# assert result == "Inner function variable:\nouter_var: outer_value\n" + +def test_exception_on_repeat_calls(): + @guidance(stateless=True, dedent=False) + def raises(lm): + assert False + with pytest.raises(AssertionError): + raises() + with pytest.raises(AssertionError): + # Test against failure to reset the grammar function; + # improper handling may not raise and may instead return + # a Placeholder grammar node + raises()