fix(dspy): raise actionable ValueError in KNN when training examples lack with_inputs() - #117
Open
detail-app[bot] wants to merge 1 commit into
Conversation
…lack with_inputs()
Greptile SummaryThis PR makes KNN initialization use the validated
Confidence Score: 5/5The PR appears safe to merge, with the intended validation behavior covered by focused regression tests. The public accessor preserves existing serialization for ordinary marked inputs while replacing the previous cryptic failure with the documented actionable error, and no concrete regression remains. Important Files Changed
Reviews (1): Last reviewed commit: "fix(dspy): raise actionable ValueError i..." | Re-trigger Greptile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Warning
GitHub issue creation failed
Detail attempted to publish this bug to GitHub, but the issue could not be created. This fix PR was created without that issue, and missing tracker references are shown as
Unknown issue.You can review and merge this PR normally. Please review your tracker integration settings before the next publish run.
Detail bug report: View on Detail
📝 Changes Description
Closes Unknown issue
Bug:
KNN.__init__serialized each training example with an inlinekey in example._input_keysfilter.Example._input_keysdefaults toNoneuntil.with_inputs()is called, so constructingKNNfrom examples that lack.with_inputs()crashed with a crypticTypeError: argument of type 'NoneType' is not iterabledeep in a list comprehension — with no hint that.with_inputs()is required. TheKNNdocstring example itself omitted.with_inputs(), so users following it hit this immediately.Fix:
_input_keysaccess withexample.inputs().items(), reusing the framework's existing guard inExample.inputs()(dspy/primitives/example.py), which raises an actionableValueError("Inputs have not been set for this example. Use \example.with_inputs()` to set them.")when_input_keys is None`. This matches the convention used across the rest of DSPy (bootstrap, evaluate, parallel, etc.) and raises the error during construction, before any embedding work.KNNdocstring example to call.with_inputs("input"), consistent withKNNFewShot's docstring.Testing:
tests/predict/test_knn.py(extramarker): one asserts a trainset with no.with_inputs()raisesValueErrormentioningwith_inputs(notTypeError); the other asserts a mixed trainset (one marked, one unmarked) still raises, confirming the check is per-example.extra): all pass with no regressions.ValueError, while the corrected docstring pattern (.with_inputs("input")) constructs and retrieves neighbors successfully.ruff checkandruff format --checkare clean; the Example doctests (--doctest-modules) pass.pyproject.tomlor CI), so that step is not applicable.✅ Contributor Checklist
Example.inputs()guard, and all verification listed above was run locally.Automatic Fixes PRs can be configured here.