{"record":{"id":"bc941aa2bb0b942c","repo":"rohitg00/ai-engineering-from-scratch","slug":"retrieved-per-query-and-gold-per-query-must-have-t","errorCode":null,"errorMessage":"retrieved_per_query and gold_per_query must have the same length ({len(retrieved_per_query)} vs {len(gold_per_query)})","messagePattern":"retrieved_per_query and gold_per_query must have the same length \\((.+?) vs (.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"phases/19-capstone-projects/68-rag-eval-precision-recall/code/main.py","lineNumber":65,"sourceCode":"\ndef recall_at_k(retrieved: list[str], gold: set[str], k: int) -> float:\n    if not gold:\n        return 0.0\n    top = set(retrieved[:k])\n    return len(top & gold) / len(gold)\n\n\ndef reciprocal_rank(retrieved: list[str], gold: set[str]) -> float:\n    for i, doc_id in enumerate(retrieved):\n        if doc_id in gold:\n            return 1.0 / (i + 1)\n    return 0.0\n\n\ndef mean_reciprocal_rank(retrieved_per_query: list[list[str]],\n                         gold_per_query: list[set[str]]) -> float:\n    if len(retrieved_per_query) != len(gold_per_query):\n        raise ValueError(\n            f\"retrieved_per_query and gold_per_query must have the same length \"\n            f\"({len(retrieved_per_query)} vs {len(gold_per_query)})\"\n        )\n    if not retrieved_per_query:\n        return 0.0\n    return sum(reciprocal_rank(r, g) for r, g in zip(retrieved_per_query, gold_per_query)) / len(retrieved_per_query)\n\n\ndef dcg_at_k(retrieved: list[str], graded: dict[str, int], k: int) -> float:\n    s = 0.0\n    for i, doc_id in enumerate(retrieved[:k]):\n        rel = graded.get(doc_id, 0)\n        if rel <= 0:\n            continue\n        s += ((2 ** rel) - 1) / math.log2(i + 2)\n    return s\n\n","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/19-capstone-projects/68-rag-eval-precision-recall/code/main.py#L47-L83","documentation":"mean_reciprocal_rank averages per-query MRR, which requires one retrieved list per query; mismatched lengths mean at least one query has no retrieved results or an orphaned result list with no gold set, so the mean would be computed over the wrong pairs.","triggerScenarios":"Thrown at phases/19-capstone-projects/68-rag-eval-precision-recall/code/main.py:65 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Build both lists in a single loop over queries so they stay aligned","Skip queries with empty gold sets before assembling the arrays","Assert len(retrieved_per_query) == len(gold_per_query) in your eval harness before calling"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}