From 19324ce5a40a1fe1b8b22316c22c605374329fc5 Mon Sep 17 00:00:00 2001 From: suhaniiz Date: Wed, 8 Jul 2026 21:16:05 +0530 Subject: [PATCH] fix: prevent in-place dictionary mutation in rerank method #814 --- backend/app/rag/reranker.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/backend/app/rag/reranker.py b/backend/app/rag/reranker.py index f43116ba..0d83dffc 100644 --- a/backend/app/rag/reranker.py +++ b/backend/app/rag/reranker.py @@ -73,19 +73,18 @@ def rerank( # Get relevance scores scores = model.predict(pairs) - # Pair scores with documents and sort in descending order - scored = list(zip(scores, documents)) - scored.sort(key=lambda x: x[0], reverse=True) - - # Return top_k documents - reranked = [doc for _, doc in scored[:top_k]] - - # Attach rerank_score to each returned document - for (score, doc) in scored: - if doc in reranked: - doc["rerank_score"] = float(score) - - return reranked + # Create shallow copies of the documents and attach scores to prevent mutating original inputs + scored_docs = [] + for score, doc in zip(scores, documents): + doc_copy = doc.copy() # Create a copy to isolate changes + doc_copy["rerank_score"] = float(score) + scored_docs.append(doc_copy) + + # Sort the copied documents in descending order of relevance + scored_docs.sort(key=lambda x: x["rerank_score"], reverse=True) + + # Return only the top_k requested documents + return scored_docs[:top_k] # Singleton instance for global reuse