Fixing Non-Deterministic RAG Evaluation Stability
- •RAG evaluation instability often stems from non-deterministic retrieval rather than inherent model flakiness.
- •Reciprocal Rank Fusion causes varying Recall@K scores when tied retrieval results appear in inconsistent orders.
- •Adding a deterministic tie-breaker like an ID column to SQL queries ensures reproducible evaluation results.
RAG evaluation results can fluctuate significantly due to non-deterministic retrieval processes rather than model instability, according to developer Vasyl. During the development of the "Ask this Book" feature for the open-source platform TextStack, the author discovered that the Recall@K metric varied between identical runs despite using the same query, documents, and model. The issue stems from the Reciprocal Rank Fusion (RRF) algorithm used to merge semantic search results from pgvector and lexical search results from PostgreSQL full-text search.
RRF relies on the rank of retrieved items rather than raw scores to calculate fused results. Consequently, if tied scores cause the order of returned chunks to change, the resulting Top-K set and downstream evaluation metrics shift accordingly. The author identified that SQL queries using "ORDER BY score DESC" often produced unpredictable ordering when multiple rows shared identical scores, as PostgreSQL does not guarantee tie-breaking behavior for equal values.
To resolve this, the author implemented a deterministic tie-breaker by updating the SQL queries to "ORDER BY score DESC, id". This change ensures that equally-ranked rows maintain a consistent order across runs, resulting in reproducible retrieval and stable evaluation outputs. The author emphasizes that developers should treat retrieval, ranking, and data loading layers with the same rigor as model parameters like temperature, as non-deterministic pipeline stages can invalidate evaluation trust and mask genuine performance issues.