Sub topic 8
Re-ranking for Diversity and Variety
Prerequisites and purpose
This chapter assumes you understand candidate generation, ranking signals, predicted engagement, and the difference between a score and a probability. It also assumes you can read short Python code.
A ranker may produce an excellent score order, but that order can still make a poor feed. For example, the first ten posts might come from one author, repeat the same topic, or contain near-duplicate content. Re-ranking is a second pass that changes the ordered list to satisfy product, safety, and variety rules.
Re-ranking does not usually replace the main score. It starts with scored posts and adjusts their order.
Why score order is not enough
Suppose a feed ranker sorts posts by predicted value:
| Position | Author | Topic | Score |
|---|---|---|---|
| 1 | Ana | cooking | 0.91 |
| 2 | Ana | cooking | 0.88 |
| 3 | Ana | cooking | 0.86 |
| 4 | Ben | travel | 0.82 |
| 5 | Cy | books | 0.79 |
If Ana publishes many popular posts, pure score sorting can fill the feed with Ana's content. This may maximize short-term predicted engagement, but it can reduce perceived variety. A user may follow several authors because they want a mix, not because they want the single highest-scoring author repeatedly.
Common re-ranking rules include:
- Limit consecutive posts from one author.
- Avoid repeated topics or formats.
- Prevent near-duplicate posts.
- Reserve positions for followed accounts, local content, or different media types.
- Keep important integrity or policy filters in force.
- Avoid showing content the user has already seen.
A rule can be a hard constraint, which must never be broken, or a soft preference, which can be broken when the score difference is large.
A greedy re-ranking pass
A simple method is greedy re-ranking. It repeatedly selects the highest-scoring remaining post that does not violate the current rules.
The following function allows at most two posts from the same author in a row:
def rerank(posts, score, max_run=2):
"""Order by score, but avoid more than max_run posts in a row from one author."""
remaining = sorted(posts, key=score, reverse=True)
result = []
while remaining:
for i, post in enumerate(remaining):
recent_authors = [item.author for item in result[-max_run:]]
blocked = (
len(recent_authors) == max_run
and all(author == post.author for author in recent_authors)
)
if not blocked:
result.append(remaining.pop(i))
break
else:
# Every remaining post violates the rule, so use the best one.
result.append(remaining.pop(0))
return resultThe algorithm first sorts by score. At each position, it scans the remaining posts and chooses the first allowed post. If every remaining post is blocked, it uses the best one anyway. This fallback prevents the algorithm from getting stuck.
The result may place a lower-scoring post before a higher-scoring post. That is the cost of the diversity rule.
Trade-offs and failure modes
A strict author limit can hide a useful author too often. A topic limit can remove the content a user came specifically to find. A format rule can favor variety while reducing relevance. These rules also interact: satisfying an author limit, topic limit, and media limit at once may leave too few legal choices.
Important failure modes include:
- Over-diversification: The feed feels scattered and loses relevance.
- Under-diversification: A few authors or topics dominate every session.
- Gaming: Authors split content across accounts or use labels that evade limits.
- Position bias: A post moved lower may receive fewer reactions even if users would have liked it.
- Cold-start conflict: New authors have little score evidence but may need exposure.
A production system normally evaluates several settings with experiments. It can compare engagement with measures such as unique authors viewed, topic coverage, repeat-content rate, hides, and user retention. Diversity should be measured across sessions, not only within one screen.
Re-ranking pipeline
The final list is therefore a policy-shaped ranking, not a direct copy of model output. The model estimates value, while re-ranking decides how much repetition the user should see.
Exercise
A feed's top five posts are all from one author, but the fifth post has a much lower score than the others. What is one reasonable re-ranking action, and what trade-off does it create?
Show answer
Move the fifth post lower and select a strong post from another author. This increases variety but may reduce predicted engagement if the moved post would have performed well.