bugger.
All challenges/Pythoneasyai-retrieval-ranking-v1
THE INVESTIGATION

The retriever that ranks the wrong document first

Python RAG retrieval bug with cosine distance

The relevant document is in the index, but the RAG application sends a worse match to its answer step.

Applied AIRAGRetrievalFixed vectors

What’s going wrong

Original Applied AI lab. This is a simulated RAG retrieval stage using fixed vectors, not a historical upstream defect or a live model call.

A support assistant cites the wrong policy even though the matching document is indexed. The vector adapter can report cosine similarity or cosine distance, and its vector rows need not arrive in document-store order. Repair retrieve so the answer step receives the correct document identities in relevance order. Start with the supplied trace, then run the visible reproduction.

Read one JSON object with documents, vectors, query, metric and k. documents contains 0 to 64 objects {"id":"refund-policy"}; IDs are unique nonempty ASCII strings. vectors contains exactly one {"documentId":"refund-policy","values":[1,0]} for each document, in any order. query and every values array have the same length from 1 to 8; all entries are integers from -10 to 10 and every vector, including query, is nonzero. metric is "similarity" or "distance". k is an integer from 0 to 67. Inputs satisfy this contract.

Compute scores with the supplied Python math.sqrt dot-product formula: each norm is math.sqrt(sum(value * value for value in vector)); similarity is sum(a * b for a, b in zip(query, vector)) divided by (query_norm * vector_norm), and distance is 1 - similarity. Use these Python float computations in this order. Print {"results":[{"id":"refund-policy","score":0.0}]}. Return at most k results in best-match-first order: larger similarity is better, smaller distance is better. Sort the unrounded computed Python float score. A tie means equal computed scores, not approximate equality or equality after rounding; break such ties by document ID in ascending Python string order. Then round each returned score using Python round(score, 6). Return the score in the requested metric. An empty index or k=0 returns an empty results array. Only the Python standard library is needed.

Expected behavior

Return the most relevant document IDs with their own scores, regardless of metric direction, document order or vector-row order.

Reproduction

Execution evidence

Simulated system · deterministic replay

Fixed query and vector-index responses from the first visible reproduction. No embedding API or answer model is called.

  1. 01Query
    query=[1,0]; request metric=distance, k=2
  2. 02Vector index
    refund-policy -> [1,0], cosine distance 0.0; shipping-policy -> [0,1], cosine distance 1.0
  3. 03Retriever output
    Starter returns shipping-policy (1.0) before refund-policy (0.0).
  4. 04Answer context
    The answer step would receive the shipping policy first. Inspect retrieval before changing the prompt.
Explain your repair before submitting

Which observation contradicts the intended behavior? Where does the code break that contract? Explain why your change fixes the cause and which new regression case would detect it.

This evidence describes the starter. Use Run to observe your current code; Submit checks additional regressions. No live model calls are made.

01 Distance ranks a worse policy first
{"documents":[{"id":"refund-policy"},{"id":"shipping-policy"}],"vectors":[{"documentId":"refund-policy","values":[1,0]},{"documentId":"shipping-policy","values":[0,1]}],"query":[1,0],"metric":"distance","k":2}
{"results":[{"id":"refund-policy","score":0.0},{"id":"shipping-policy","score":1.0}]}
02 Similarity keeps the correct passing control
{"documents":[{"id":"refund-policy"},{"id":"shipping-policy"}],"vectors":[{"documentId":"refund-policy","values":[1,0]},{"documentId":"shipping-policy","values":[0,1]}],"query":[1,0],"metric":"similarity","k":2}
{"results":[{"id":"refund-policy","score":1.0},{"id":"shipping-policy","score":0.0}]}
03 Vector rows do not share document-store positions
{"documents":[{"id":"shipping-policy"},{"id":"refund-policy"}],"vectors":[{"documentId":"refund-policy","values":[1,0]},{"documentId":"shipping-policy","values":[0,1]}],"query":[1,0],"metric":"similarity","k":1}
{"results":[{"id":"refund-policy","score":1.0}]}

Before you begin

  • Python lists, dictionaries and sorted keys
  • Dot products and vector lengths; the formula is supplied in the report
  • RAG separates retrieval from answer generation

Need a nudge?

Reveal one investigation hint at a time. Try each clue before opening the next.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.