{"record":{"id":"a07a50c857752bc0","repo":"deepset-ai/haystack","slug":"top-k-must-be-greater-than-0-a07a50","errorCode":null,"errorMessage":"top_k must be greater than 0.","messagePattern":"top_k must be greater than 0\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/joiners/document_joiner.py","lineNumber":120,"sourceCode":"            - `reciprocal_rank_fusion`: Merges and assigns scores based on reciprocal rank fusion.\n            - `distribution_based_rank_fusion`: Merges and assigns scores based on scores\n            distribution in each Retriever.\n        :param weights:\n            Assign importance to each list of documents to influence how they're joined.\n            This parameter is ignored for\n            `concatenate` or `distribution_based_rank_fusion` join modes.\n            Weight for each list of documents must match the number of inputs.\n        :param top_k:\n            The maximum number of documents to return. Must be `None` or greater than 0.\n        :param sort_by_score:\n            If `True`, sorts the documents by score in descending order.\n            If a document has no score, it is handled as if its score is -infinity.\n\n        :raises ValueError:\n            If `top_k` is not `None` and is less than or equal to 0.\n        \"\"\"\n        if top_k is not None and top_k <= 0:\n            raise ValueError(\"top_k must be greater than 0.\")\n        if isinstance(join_mode, str):\n            join_mode = JoinMode.from_str(join_mode)\n        join_mode_functions = {\n            JoinMode.CONCATENATE: DocumentJoiner._concatenate,\n            JoinMode.MERGE: self._merge,\n            JoinMode.RECIPROCAL_RANK_FUSION: self._rrf,\n            JoinMode.DISTRIBUTION_BASED_RANK_FUSION: DocumentJoiner._distribution_based_rank_fusion,\n        }\n        self.join_mode_function = join_mode_functions[join_mode]\n        self.join_mode = join_mode\n        if weights:\n            weight_sum = sum(weights)\n            if weight_sum == 0:\n                raise ValueError(\"The provided `weights` must not sum to zero.\")\n            self.weights: list[float] | None = [float(i) / weight_sum for i in weights]\n        else:\n            self.weights = None\n        self.top_k = top_k","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/joiners/document_joiner.py#L102-L138","documentation":"DocumentJoiner.__init__ rejects a top_k that is not None and not > 0. top_k bounds how many documents are kept after joining; zero or negative values are invalid at construction time.","triggerScenarios":"DocumentJoiner(top_k=0) or DocumentJoiner(top_k=-5). Note None is allowed and means 'use the runtime top_k only'.","commonSituations":"Config defaults of 0 mistaken for 'no limit'; computing top_k from an empty/zero-valued variable; confusion with the runtime run() check which allows 0.","solutions":["Pass a positive integer for top_k.","Pass top_k=None to skip truncation at init time and control it in run().","Validate config-sourced values before constructing: top_k must be int > 0 or None."],"exampleFix":"// before\nDocumentJoiner(join_mode=\"merge\", top_k=0)\n// after\nDocumentJoiner(join_mode=\"merge\", top_k=None)  # or a positive int like 10","handlingStrategy":"validation","validationCode":"if top_k is not None and (not isinstance(top_k, int) or top_k <= 0):\n    raise ValueError(\"DocumentJoiner top_k must be a positive int or None\")\njoiner = DocumentJoiner(top_k=top_k)","typeGuard":"def is_valid_init_top_k(v: int | None) -> bool:\n    return v is None or (isinstance(v, int) and not isinstance(v, bool) and v > 0)","tryCatchPattern":"try:\n    joiner = DocumentJoiner(join_mode=mode, top_k=top_k)\nexcept ValueError as e:\n    logger.warning(\"invalid top_k %r, using None\", top_k)\n    joiner = DocumentJoiner(join_mode=mode, top_k=None)","preventionTips":["Treat 0 as invalid at init; use None for 'no init-level limit'","Sanitize config defaults that use 0","Coerce strings from YAML/JSON configs to int before constructing"],"tags":["validation","python","haystack"],"backgroundTag":"invalid-parameter-value","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}