Skip to content

[HIGH] Non-deterministic recommendation results due to unstable sorting #379

@atul-upadhyay-7

Description

@atul-upadhyay-7

Description

The recommendation algorithm produces non-deterministic results when multiple projects have the same score. This causes inconsistent project recommendations for the same user inputs across different requests or server restarts.

Affected File

utils/recommender.py

Problem

In the get_recommendations() function (lines 94-124), after scoring all projects, the sorting only uses the score as the key:

scored_projects.sort(key=lambda item: item["score"], reverse=True)

When two or more projects have identical scores, Python's sort is unstable - the relative order of equal-scoring items depends on their original order in the list, which can vary based on:

  • The order they appear in projects.json
  • Previous modifications to the JSON file
  • File system ordering

Real-world Impact

Users with the same skillset, level, interest, and time availability may get different top 3 recommendations on:

  • Different days
  • After the JSON file is modified (even unrelated changes)
  • Across different server instances

This makes testing difficult and confuses users who expect consistent results.

Expected Behavior

When scores are tied, there should be a consistent secondary sort key (e.g., project ID or name alphabetically) to ensure deterministic results.

Suggested Fix

Add secondary sort criteria in get_recommendations():

scored_projects.sort(key=lambda item: (item["score"], item["project"].get("id", 0)), reverse=True)

Or sort by project title as secondary:

scored_projects.sort(key=lambda item: (item["score"], item["project"].get("title", "")), reverse=True)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions