{"record":{"id":"15b90e4abdc6ddc7","repo":"deepset-ai/haystack","slug":"top-k-must-be-greater-than-0","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/answer_joiner.py","lineNumber":106,"sourceCode":"        self, join_mode: str | JoinMode = JoinMode.CONCATENATE, top_k: int | None = None, sort_by_score: bool = False\n    ) -> None:\n        \"\"\"\n        Creates an AnswerJoiner component.\n\n        :param join_mode:\n            Specifies the join mode to use. Available modes:\n            - `concatenate`: Concatenates multiple lists of Answers into a single list.\n        :param top_k:\n            The maximum number of Answers to return. Must be `None` or greater than 0.\n        :param sort_by_score:\n            If `True`, sorts the answers by score in descending order.\n            If an answer 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: dict[JoinMode, Callable[[list[list[AnswerType]]], list[AnswerType]]] = {\n            JoinMode.CONCATENATE: self._concatenate\n        }\n        self.join_mode_function: Callable[[list[list[AnswerType]]], list[AnswerType]] = join_mode_functions[join_mode]\n        self.join_mode = join_mode\n        self.top_k = top_k\n        self.sort_by_score = sort_by_score\n\n    @component.output_types(answers=list[AnswerType])\n    def run(self, answers: Variadic[list[AnswerType]], top_k: int | None = None) -> dict[str, Any]:\n        \"\"\"\n        Joins multiple lists of Answers into a single list depending on the `join_mode` parameter.\n\n        :param answers:\n            Nested list of Answers to be merged.\n","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/joiners/answer_joiner.py#L88-L124","documentation":"AnswerJoiner validates top_k at construction: if it is set (not None) it must be > 0, otherwise the joiner would produce an empty/invalid selection of answers. haystack raises ValueError immediately in __init__.","triggerScenarios":"AnswerJoiner(top_k=0) or AnswerJoiner(top_k=-5) — commonly from a computed or YAML-config-driven value that resolved to 0 or negative.","commonSituations":"top_k read from env/config where a sentinel 0 means 'unlimited'; arithmetic producing 0 (e.g. int division); forgetting that None means 'keep all' but 0 is invalid.","solutions":["Pass top_k=None to keep all answers instead of 0","Use a positive integer for top_k","Clamp computed values: top_k = max(1, top_k) when top_k is not None"],"exampleFix":"// before\njoiner = AnswerJoiner(top_k=0)\n// after\njoiner = AnswerJoiner(top_k=None)  # or a positive int","handlingStrategy":"validation","validationCode":"if top_k is not None and top_k <= 0:\n    top_k = None  # or raise, depending on intent","typeGuard":null,"tryCatchPattern":"try:\n    joiner = AnswerJoiner(top_k=cfg_top_k)\nexcept ValueError as e:\n    if \"top_k\" in str(e):\n        joiner = AnswerJoiner(top_k=None)","preventionTips":["Treat 0 as invalid; use None for 'all answers'","Clamp computed values with max(1, top_k)","Validate YAML/env config values before constructing"],"tags":["python","haystack","joiner","validation","config"],"backgroundTag":"invalid-parameter-value","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}