[FEATURE] ReRankingContentAggregator: add minResults to guarantee a minimum number of results when minScore filters out everything
Is your feature request related to a problem? Please describe.
tl;dr: when no candidate clears minScore, ReRankingContentAggregator returns nothing, even when the correct content was retrieved. Rerankers can score correct content very low, so the model ends up answering without context.
Example: a question that contradicts a stored fact
Setup (LangChain4j 1.20.1):
- Pipeline:
CompressingQueryTransformer→LanguageModelQueryRouter→EmbeddingStoreContentRetriever→ReRankingContentAggregator - Scoring model: Cohere
rerank-multilingual-v3.0 - Stored memories:
"Ricardo's favorite color is black"and"User's favorite color is black."
The user asks whether blue is their favorite color. Both memories are retrieved, and both are exactly what the model needs to answer "No, it's black". But Cohere scores them very low:
| Query | Score (memory 1) | Score (memory 2) |
|---|---|---|
whether blue is my favorite color |
0.0079 | 0.00002 |
Does Ricardo have a stored preference indicating that blue is his favorite color? (compressed query) |
0.1061 | 0.0001 |
With minScore(0.8), both are dropped and the assistant says it has no memory of the user's favorite color.
Why lowering the threshold doesn't help
The threshold also does useful work. With the same memory set:
Query: which PR is a priority right now |
Score |
|---|---|
The PR adding support for JSON is the priority this week |
0.6883 |
User's favorite color is black. |
0.0009 |
User has a dentist appointment this week |
0.00009 |
Here minScore correctly keeps only the PR memory. The problem:
- Correct-but-contradicting content scores around 0.007.
- Irrelevant content scores around 0.0009.
- That gap is small and shifts with query wording, so no single threshold separates them reliably.
Describe the solution you'd like
Add an optional minResults to the builder. It defaults to 0, so existing behavior doesn't change.
ReRankingContentAggregator.builder()
.scoringModel(scoringModel)
.minScore(0.5)
.minResults(1)
.build();Behavior after reranking:
- Sort candidates by score, highest first.
- Keep every candidate with
score >= minScore. - If fewer than
minResultspassed, fill up with the highest-ranked remaining candidates. - Apply
maxResultsas today.
In other words: filter strictly when something is clearly relevant; otherwise still give the model the best candidate.
How it fixes the examples above
With minScore(0.5) and minResults(1):
Favorite color (whether blue is my favorite color)
| Memory | Score | Result |
|---|---|---|
User's favorite color is black. |
0.0079 | ✅ kept (fill-up, step 3) |
Ricardo's favorite color is black |
0.00002 | ❌ dropped |
- Nothing clears
0.5, so step 2 keeps zero items. 0 < minResults, so step 3 adds the top-ranked memory.- The model receives "User's favorite color is black." and answers "No, your favorite color is black."
- Today: the list is empty and the model says it has no memory of the user's favorite color.
PR priority (which PR is a priority right now)
| Memory | Score | Result |
|---|---|---|
The PR adding support for JSON is the priority this week |
0.6883 | ✅ kept (passes minScore, step 2) |
User's favorite color is black. |
0.0009 | ❌ dropped |
User has a dentist appointment this week |
0.00009 | ❌ dropped |
- The PR memory clears
0.5, sominResultsis already satisfied and step 3 does nothing. - The irrelevant memories stay out of the prompt, exactly as with today's
minScore.
Filtering stays strict when a memory clearly matches, and the correct memory still gets through when a contradicting question scores it low.
Notes:
- Validation:
minResults >= 0andminResults <= maxResults. - The change lives in
toReRankedContents, so bothaggregateandaggregateAsyncget it. RERANKED_SCOREmetadata is still attached, so downstream code can tell that a kept item scored low.
Describe alternatives you've considered
- Remove
minScore: irrelevant content reaches the prompt. - Lower
minScore: no value reliably separates contradicting from irrelevant content (see above). - Custom
ContentAggregator: what I run today. It works, but it duplicates fusion, reranking and metadata handling, and it losesquerySelectorand async support. - Subclass and override
reRankAndFilter: the filtering logic is in private methods, so the override has to redo scoring. - Wrap the aggregator (
minScore(null), then post-filter onRERANKED_SCORE): works, but it's not obvious and moves filtering away from the class that ownsminScore. - Change the default to always return one result: silently breaks users who rely on an empty result to make the model say "I don't know".
Additional context
maxResultswas added the same way, as an opt-in option: https://github.com/langchain4j/langchain4j/issues/2043- https://github.com/langchain4j/langchain4j/pull/6134 touches the same class. Happy to rebase or coordinate.
- I'm happy to contribute the PR, with unit tests for three cases: all candidates pass, some pass, none pass.
Source: langchain4j/langchain4j