Warn about a missing history component only when one could be missing - #1209
Merged
mdekstrand merged 1 commit intoSep 4, 2026
Merged
mdekstrand merged 1 commit into
mdekstrand merged 1 commit into
Conversation
`ItemKNNScorer` warns "no query items, did you omit a history component?" whenever `query.query_items` is None. That fires for two very different situations, and is only correct for one of them. `RecQuery.create` returns a query unchanged if it is already a `RecQuery`, and otherwise wraps a bare user ID in a query with no history at all. So an absent history means a pipeline gap only when the caller passed something that was not already a query. When a query arrives, a history component has already run, and an empty history is its normal result for a user who is not in the training data — a false positive, as reported in lenskit#804. The warning is now emitted only for the first case. The second still logs at debug level through the existing "user has no history, returning" message. `UserKNNScorer` is deliberately left alone. It checks the user against its training vocabulary before warning and reports "has no ratings and none provided", which is accurate for the case it covers and does not blame a component. `ItemKNNScorer` cannot do the same: it retains only items, item means and the similarity matrix, so it has no user vocabulary to consult. Adds a regression test covering both directions: a query for an unknown user produces no warning, while a bare ID for the same user still does. The test fails against the current behaviour on the first assertion. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1209 +/- ##
=======================================
Coverage 90.40% 90.40%
=======================================
Files 263 263
Lines 17516 17517 +1
=======================================
+ Hits 15836 15837 +1
Misses 1680 1680 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #804.
ItemKNNScoreremitsno query items, did you omit a history component?wheneverquery.query_itemsis None. That covers two very different situations and is only correct for one of them.RecQuery.createreturns a query unchanged if it is already aRecQuery, and otherwise wraps a bare user ID in a query with no history at all. So an absent history indicates a pipeline gap only when the caller passed something that was not already a query. When a query arrives, a history component has already run, and an empty history is its normal result for a user who is not in the training data — the false positive the issue describes.The warning is now emitted only in the first case. The second still logs at debug level via the existing
user has no history, returningmessage. This follows the direction suggested in the issue (keying off whether the query is aRecQuery).Reproduction
Against 2026.3.0, with a history component present and doing its job:
After the change that call is silent, while
scorer(999, ItemList([6, 7, 8]))— a bare ID, no history component in front — still warns.UserKNNScoreris deliberately unchangedThe issue mentions other components.
UserKNNScoreralready handles this correctly: it looks the user up in its training vocabulary before warning, and reportshas no ratings and none provided, which is accurate and does not blame a component.ItemKNNScorercannot do the same — it retains onlyitems,item_meansandsim_matrix, so it has no user vocabulary to consult, which is why the query-shape test is used instead.Testing
Adds a regression test asserting both directions via
structlog.testing.capture_logs. It fails on the first assertion against currentmainand passes with this change.tests/models/test_knn_item_item.pyis 41 passed / 9 skipped andtest_knn_user_user.pyis 40 passed / 8 skipped with the change applied.One note on my environment: the Rust toolchain on this machine could not build the extension, so I verified against the released 2026.3.0 wheel with this patch applied to the installed package, running the repo's own test file against it. The change itself is pure Python and touches no compiled code.
🤖 Generated with Claude Code