#10523·dspy

[Bug] BootstrapFewShot loses teacher demos after a teacher exception

Author: dafyy321-pixelCreated Sep 29, 2026Updated Sep 30, 2026

What happened?

BootstrapFewShot can leave the teacher's demos changed after one training example fails. The optimizer deliberately hides the example it is currently bootstrapping from the teacher's predictors. That exclusion should last for one teacher call. When that call raises and the configured error budget lets the run continue, the next example is sent to a teacher whose demos are still filtered.

The failure itself is logged, but the state leak happens afterward: the next attempt may succeed and still have been run with a different prompt from the one supplied to the optimizer.

Steps to reproduce

This uses the optimizer's per-example method so the two teacher calls and their demo lists are visible without an LM or API key:

python
import dspy

class FailingOnce(dspy.Module):
    def __init__(self):
        super().__init__()
        self.predictor = dspy.Predict("input -> output")
        self.seen_demos = []

    def forward(self, **kwargs):
        self.seen_demos.append([demo.input for demo in self.predictor.demos])
        if len(self.seen_demos) == 1:
            raise RuntimeError("teacher failed")
        return dspy.Prediction(output="ok")

first = dspy.Example(input="first", output="ok").with_inputs("input")
second = dspy.Example(input="second", output="ok").with_inputs("input")
teacher = FailingOnce()
original_demos = [first]
teacher.predictor.demos = original_demos

optimizer = dspy.BootstrapFewShot(max_errors=2)
optimizer.teacher = teacher
print("first:", optimizer._bootstrap_one_example(first))
print("restored:", teacher.predictor.demos is original_demos)
print("second:", optimizer._bootstrap_one_example(second))
print("seen:", teacher.seen_demos)

On main at 9c900c7, the relevant output is:

first: False
restored: False
second: True
seen: [[], []]

The first [] is expected: first must be hidden while the teacher works on first. The second call is for second, so it should see the original demo first. The expected state and output are restored: True and seen: [[], ['first']]. The second call succeeding does not make the lost demo harmless; it shows how the state change can survive the initial error.

Where the state leaks

_bootstrap_one_example saves each predictor's demo list and replaces it with a filtered list at lines 195–197. It invokes teacher(**example.inputs()) at line 199. Restoration is at lines 201–203, after that invocation. A teacher exception jumps to the outer handler at line 213, bypassing restoration; the handler returns False and allows another example while error_count < max_errors.

The demo list should be restored on every exit from this temporary filtering section, including an exception during the teacher call or while preparing another predictor. The accompanying PR #10522 moves restoration into finally, keeps a reference to each predictor that was actually filtered, and tests both a later bootstrap attempt and a teacher that replaces a predictor while failing.

Environment

  • DSPy 3.4.0, repository main at 9c900c7
  • Reproduced with Python 3.11 on Windows; the failing path is in Python control flow and does not require a provider connection