← Back to DAGDA | Support | Source: https://github.com/stanfordnlp/dspy/pull/9979
Skip to content

fix: GroundedProposer ignores hand-labeled demos (sub-issue 4 of #9938) - #9979

Open
shadowmodder wants to merge 1 commit into
stanfordnlp:mainfrom
shadowmodder:fix/grounded-proposer-hand-labeled-demos
Open

shadowmodder wants to merge 1 commit into
stanfordnlp:mainfrom
shadowmodder:fix/grounded-proposer-hand-labeled-demos

Conversation

@shadowmodder

Copy link
Copy Markdown

Summary

Closes sub-issue 4 of #9938 (MIPROv2 crashes / produces wrong results when max_bootstrapped_demos=0).

The bug

GenerateModuleInstruction.forward() contains a nested helper gather_examples_from_sets that filters candidate examples with:

if "augmented" in example.keys():

The "augmented" key is only ever set on bootstrapped demos (see BootstrapFewShot._bootstrap_one_example, which does dspy.Example(augmented=True, ...)). Hand-labeled examples from the training set are appended to predictor.demos as plain dspy.Example objects with no "augmented" key.

Concrete consequence: when max_bootstrapped_demos=0 and only labeled demos exist in demo_candidates, gather_examples_from_sets yields nothing, task_demos is an empty string, and the fallback fires:

if not task_demos.strip() or demo_set_i == 0:
    task_demos = "No task demos provided."

The instruction proposer LM is given zero task context even though the caller explicitly provided labeled examples.

The fix

Remove the "augmented" guard so the function yields all examples — both bootstrapped and hand-labeled. Both are equally valid as context for instruction proposal. The one-line removal is the entire change to production code.

Verification

Added test_labeled_demos_included_in_task_demos in tests/propose/test_grounded_proposer.py:

  • Constructs demo_candidates containing only labeled examples (no "augmented" key)
  • Uses demo_set_i=1 to bypass the demo_set_i == 0 shortcircuit
  • Asserts the labeled example text appears in the final prompt sent to the proposer LM
  • Asserts "No task demos provided." is absent from that prompt

All 5 tests in tests/propose/test_grounded_proposer.py pass.

Before fix: gather_examples_from_sets yields 0 examples for labeled-only demo sets.
After fix: yields the rendered example string (e.g. "Question: What is 2+2?\nAnswer: 4").

This is one of four sub-issues in #9938; sub-issue 1 (randrange crash) was addressed by #9939.

gather_examples_from_sets in GenerateModuleInstruction.forward() filtered
examples with `if "augmented" in example.keys()`, but the "augmented" key is
only set on bootstrapped demos (BootstrapFewShot line 226). Hand-labeled
examples from the training set carry no such key, so they were silently
skipped. When max_bootstrapped_demos=0 and only labeled demos exist,
task_demos always collapsed to "No task demos provided.", giving MIPROv2's
instruction proposer zero context about the actual task.

Remove the guard so both bootstrapped and labeled demos are yielded.
Add a regression test that constructs labeled-only demo_candidates at
demo_set_i=1 (bypassing the demo_set_i==0 shortcircuit) and asserts the
labeled example text appears in the prompt sent to the proposer LM.

Fixes sub-issue 4 of stanfordnlp#9938.
@greptile-apps

greptile-apps Bot commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a silent filtering bug in GenerateModuleInstruction.forward where gather_examples_from_sets required the "augmented" key — only present on bootstrapped demos — causing hand-labeled examples to always be excluded from instruction proposal context.

  • Production change: a one-line removal of the if "augmented" in example.keys() guard, so all examples (both bootstrapped and hand-labeled) are now yielded up to the existing num_demos_in_context cap.
  • Regression test: test_labeled_demos_included_in_task_demos builds a labeled-only demo_candidates, uses demo_set_i=1 to bypass the intentional first-set shortcircuit, and asserts the example text reaches the LM prompt while the "No task demos provided." fallback does not appear.

Confidence Score: 5/5

Safe to merge — the change removes a single misguided guard that was silently discarding valid input, and the rest of the forward method is untouched.

The fix is a targeted one-line deletion backed by a clear root-cause analysis. The num_demos_in_context cap that previously applied only to bootstrapped examples now applies uniformly, so there is no risk of unbounded context growth. The regression test is well-constructed, properly bypasses the unrelated demo_set_i==0 shortcircuit, and directly asserts both the presence of example text and the absence of the fallback string in the final LM prompt.

No files require special attention.

Important Files Changed

Filename Overview
dspy/propose/grounded_proposer.py Removes the "augmented" key guard in gather_examples_from_sets, allowing hand-labeled examples (which have no "augmented" key) to be included alongside bootstrapped demos as context for instruction proposal.
tests/propose/test_grounded_proposer.py Adds a regression test verifying that labeled (non-augmented) examples appear in the task_demos context sent to the proposer LM, using demo_set_i=1 to bypass the intentional demo_set_i==0 shortcircuit.

Reviews (1): Last reviewed commit: "fix: include hand-labeled demos in Groun..." | Re-trigger Greptile

@shadowmodder

Copy link
Copy Markdown
Author

This still merges cleanly against main and fixes sub-issue 4 of #9938. Could a maintainer take a look when there is time? Happy to rework it if the approach is off.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant