Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

fix: teacher discards assertion failure

Shangyinted5aa11aee1c
diff --git a/dspy/primitives/assertions.py b/dspy/primitives/assertions.py
index 42f2f306b..c6c18ee5e 100644
--- a/dspy/primitives/assertions.py
+++ b/dspy/primitives/assertions.py
@@ -115,9 +115,9 @@ def bypass_suggest_handler(func):
def bypass_assert_handler(func):
- """Handler to bypass suggest only.
+ """Handler to bypass assertion only.
- If a suggestion fails, it will be logged but not raised.
+ If a assertion fails, it will be logged but not raised.
And If an assertion fails, it will be raised.
"""
@@ -128,6 +128,18 @@ def bypass_assert_handler(func):
return wrapper
+def assert_no_except_handler(func):
+ """Handler to ignore assertion failure and return None."""
+
+ def wrapper(*args, **kwargs):
+ try:
+ return func(*args, **kwargs)
+ except DSPyAssertionError as e:
+ return None
+
+ return wrapper
+
+
def suggest_backtrack_handler(func, max_backtracks=2):
"""Handler for backtracking suggestion.
diff --git a/dspy/teleprompt/bootstrap.py b/dspy/teleprompt/bootstrap.py
index c908b408c..accedf36a 100644
--- a/dspy/teleprompt/bootstrap.py
+++ b/dspy/teleprompt/bootstrap.py
@@ -4,7 +4,7 @@ import random
import threading
from dspy.primitives import Example
-from dspy.primitives.assertions import assert_transform_module, bypass_assert_handler, suggest_backtrack_handler
+from dspy.primitives.assertions import assert_no_except_handler, assert_transform_module, bypass_assert_handler, suggest_backtrack_handler
from .teleprompt import Teleprompter
from .vanilla import LabeledFewShot
@@ -64,7 +64,7 @@ class BootstrapFewShot(Teleprompter):
self.student = assert_transform_module(self.student)
self.teacher = assert_transform_module(self.teacher,
- assertion_handler=lambda fun: bypass_assert_handler(suggest_backtrack_handler(fun)))
+ assertion_handler=lambda fun: suggest_backtrack_handler(assert_no_except_handler(fun)))
if self.max_labeled_demos and self.teacher._compiled is False:
teleprompter = LabeledFewShot(k=self.max_labeled_demos)