DSPy: The framework for programming—not prompting—language models
Python
allow user to set bypass_assert as optional arg in forward
manishshettymd38185842cdc
diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.pyindex 1dddcb545..9f25a67a3 100644--- a/dspy/primitives/assertions.py+++ b/dspy/primitives/assertions.py@@ -122,8 +122,13 @@ def assert_backtrack_policy(max_backtracks=2):backtrack_to, default_args={"feedback": feedback_msg})- # if last attempt, run ignoring assertion errors- if i == max_backtracks and dspy.settings.bypass_assert is False:+ # if user set bypass_assert: ignore assertion errors+ if dsp.settings.bypass_assert:+ result = func(*args, **kwargs)+ break++ # if last backtrack: ignore assertion errors+ elif i == max_backtracks and dspy.settings.bypass_assert is False:dspy.settings.configure(bypass_assert=True)result = func(*args, **kwargs)dspy.settings.configure(bypass_assert=False)@@ -132,7 +137,7 @@ def assert_backtrack_policy(max_backtracks=2):else:try:result = func(*args, **kwargs)- break # if no error, break out of backtrack loop+ breakexcept DSPyAssertionError as e:print(f"AssertionError: {e.msg}")error_msg = e.msgdiff --git a/dspy/primitives/program.py b/dspy/primitives/program.pyindex 5a8a0fbc9..024eef929 100644--- a/dspy/primitives/program.py+++ b/dspy/primitives/program.py@@ -1,4 +1,5 @@import copy+import inspectfrom dspy.primitives.module import BaseModulefrom dspy.primitives.assertions import *@@ -26,6 +27,12 @@ class Module(BaseModule, metaclass=ProgramMeta):if getattr(self, "forward", False) and not getattr(self.forward, "_decorated", False):+ args_to_vals = inspect.getcallargs(self.forward, *args, **kwargs)++ # if user has specified a bypass_assert flag, set it+ if "bypass_assert" in args_to_vals:+ dspy.settings.configure(bypass_assert=args_to_vals["bypass_assert"])+wrapped_forward = assert_backtrack_policy()(self.forward)return wrapped_forward(*args, **kwargs)else: