DSPy: The framework for programming—not prompting—language models
Python
Fix #8703 - fixing module and feedback mismatch (#8777)
Lucas Martinse78c1058d58d
diff --git a/dspy/teleprompt/gepa/gepa.py b/dspy/teleprompt/gepa/gepa.pyindex 87994968a..1f63f17ce 100644--- a/dspy/teleprompt/gepa/gepa.py+++ b/dspy/teleprompt/gepa/gepa.py@@ -240,6 +240,9 @@ class GEPA(Teleprompter):track_best_outputs: Whether to track the best outputs on the validation set. track_stats mustbe True if track_best_outputs is True. The optimized program's `detailed_results.best_outputs_valset`will contain the best outputs for each task in the validation set.+ warn_on_score_mismatch: GEPA (currently) expects the metric to return the same module-level score when+ called with and without the pred_name. This flag (defaults to True) determines whether a warning is+ raised if a mismatch in module-level and predictor-level score is detected.seed: The random seed to use for reproducibility. Default is 0.Note:@@ -292,6 +295,7 @@ class GEPA(Teleprompter):wandb_api_key: str | None = None,wandb_init_kwargs: dict[str, Any] | None = None,track_best_outputs: bool = False,+ warn_on_score_mismatch: bool = True,# Reproducibilityseed: int | None = 0,):@@ -350,6 +354,7 @@ class GEPA(Teleprompter):self.use_wandb = use_wandbself.wandb_api_key = wandb_api_keyself.wandb_init_kwargs = wandb_init_kwargs+ self.warn_on_score_mismatch = warn_on_score_mismatchif track_best_outputs:assert track_stats, "track_stats must be True if track_best_outputs is True."@@ -475,6 +480,7 @@ class GEPA(Teleprompter):rng=rng,reflection_lm=self.reflection_lm,custom_instruction_proposer=self.custom_instruction_proposer,+ warn_on_score_mismatch=self.warn_on_score_mismatch)# Instantiate GEPA with the simpler adapter-based APIdiff --git a/dspy/teleprompt/gepa/gepa_utils.py b/dspy/teleprompt/gepa/gepa_utils.pyindex 66b3b4636..50f8b6ea7 100644--- a/dspy/teleprompt/gepa/gepa_utils.py+++ b/dspy/teleprompt/gepa/gepa_utils.py@@ -13,6 +13,7 @@ from dspy.evaluate import Evaluatefrom dspy.primitives import Example, Predictionfrom dspy.teleprompt.bootstrap_trace import TraceData+logger = logging.getLogger(__name__)class LoggerAdapter:def __init__(self, logger: logging.Logger):@@ -63,6 +64,7 @@ class DspyAdapter(GEPAAdapter[Example, TraceData, Prediction]):rng: random.Random | None = None,reflection_lm=None,custom_instruction_proposer: "ProposalFn | None" = None,+ warn_on_score_mismatch: bool = True):self.student = student_moduleself.metric_fn = metric_fn@@ -73,6 +75,7 @@ class DspyAdapter(GEPAAdapter[Example, TraceData, Prediction]):self.rng = rng or random.Random(0)self.reflection_lm = reflection_lmself.custom_instruction_proposer = custom_instruction_proposer+ self.warn_on_score_mismatch = warn_on_score_mismatchif self.custom_instruction_proposer is not None:# We are only overriding the propose_new_texts method when a custom@@ -103,6 +106,7 @@ class DspyAdapter(GEPAAdapter[Example, TraceData, Prediction]):# Cache predictor names/signaturesself.named_predictors = list(self.student.named_predictors())+def build_program(self, candidate: dict[str, str]):new_prog = self.student.deepcopy()for name, pred in new_prog.named_predictors():@@ -252,8 +256,12 @@ class DspyAdapter(GEPAAdapter[Example, TraceData, Prediction]):captured_trace=trace,)d["Feedback"] = fb["feedback"]- assert fb["score"] == module_score, f"Currently, GEPA only supports feedback functions that return the same score as the module's score. However, the module-level score is {module_score} and the feedback score is {fb.score}."- # d['score'] = fb.score+ if fb["score"] != module_score:+ if self.warn_on_score_mismatch:+ logger.warning("The score returned by the metric with pred_name is different from the overall metric score. This can indicate 2 things: Either the metric is non-deterministic (e.g., LLM-as-judge, Semantic score, etc.) or the metric returned a score specific to pred_name that differs from the module level score. Currently, GEPA does not support predictor level scoring (support coming soon), and only requires a feedback text to be provided, which can be specific to the predictor or program level. GEPA will ignore the differing score returned, and instead use module level score. You can safely ignore this warning if using a semantic metric, however, if this mismatch is caused due to predictor scoring, please return module-level scores. To disable this warning, set warn_on_score_mismatch=False.")+ self.warn_on_score_mismatch = False+ fb["score"] = module_score+items.append(d)if len(items) == 0: