{"record":{"id":"3c364a4188051940","repo":"deepset-ai/haystack","slug":"top-k-must-be-0-but-got-top-k-3c364a","errorCode":null,"errorMessage":"top_k must be > 0, but got {top_k}","messagePattern":"top_k must be > 0, but got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/rankers/meta_field.py","lineNumber":121,"sourceCode":"            ranking_mode=self.ranking_mode,\n            sort_order=self.sort_order,\n            missing_meta=self.missing_meta,\n            meta_value_type=meta_value_type,\n        )\n        self.meta_value_type = meta_value_type\n\n    def _validate_params(\n        self,\n        *,\n        weight: float,\n        top_k: int | None,\n        ranking_mode: Literal[\"reciprocal_rank_fusion\", \"linear_score\"],\n        sort_order: Literal[\"ascending\", \"descending\"],\n        missing_meta: Literal[\"drop\", \"top\", \"bottom\"],\n        meta_value_type: Literal[\"float\", \"int\", \"date\"] | None,\n    ) -> None:\n        if top_k is not None and top_k <= 0:\n            raise ValueError(f\"top_k must be > 0, but got {top_k}\")\n\n        if weight < 0 or weight > 1:\n            raise ValueError(\n                f\"Parameter <weight> must be in range [0,1] but is currently set to '{weight}'.\\n'0' disables sorting \"\n                \"by a meta field, '0.5' assigns equal weight to the previous relevance scores and the meta field, and \"\n                \"'1' ranks by the meta field only.\\nChange the <weight> parameter to a value in range 0 to 1 when \"\n                \"initializing the MetaFieldRanker.\"\n            )\n\n        if ranking_mode not in [\"reciprocal_rank_fusion\", \"linear_score\"]:\n            raise ValueError(\n                \"The value of parameter <ranking_mode> must be 'reciprocal_rank_fusion' or 'linear_score', but is \"\n                f\"currently set to '{ranking_mode}'.\\nChange the <ranking_mode> value to 'reciprocal_rank_fusion' or \"\n                \"'linear_score' when initializing the MetaFieldRanker.\"\n            )\n\n        if sort_order not in [\"ascending\", \"descending\"]:\n            raise ValueError(","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/rankers/meta_field.py#L103-L139","documentation":"MetaFieldRanker._validate_params (called from both __init__ and run) rejects a top_k that is not None and <= 0. top_k caps how many documents the ranker returns; None means 'use the init value'. Raised as ValueError.","triggerScenarios":"MetaFieldRanker(top_k=0), MetaFieldRanker(top_k=-5), or ranker.run(documents=..., top_k=0) at runtime; validation runs on every init and run call.","commonSituations":"Reading top_k from config/env where a default of 0 was left; passing `results_count` from an upstream component that returned zero results; run-time overrides built dynamically.","solutions":["Pass a positive integer for top_k or None to defer to the constructor value.","Clamp at the call site: top_k = max(1, top_k) if top_k is not None else None.","Fix the config source so a valid default (e.g. 10) is used when the key is missing."],"exampleFix":"// before\nranker = MetaFieldRanker(top_k=0)\n// after\nranker = MetaFieldRanker(top_k=10)","handlingStrategy":"validation","validationCode":"if top_k is not None and top_k <= 0:\n    raise ValueError(f\"top_k must be > 0, got {top_k}\")","typeGuard":"def is_valid_top_k(v) -> bool:\n    return v is None or (isinstance(v, int) and v > 0)","tryCatchPattern":"try:\n    ranker = MetaFieldRanker(top_k=top_k, weight=weight, ranking_mode=mode, sort_order=order, missing_meta=mm)\nexcept ValueError as e:\n    logger.error(\"Invalid MetaFieldRanker params: %s\", e)\n    ranker = MetaFieldRanker()","preventionTips":["Validate all MetaFieldRanker params in one place before init; _validate_params runs on every init and run.","Clamp dynamic top_k with max(1, k).","Use None to defer to the constructor default."],"tags":["python","validation","ranker","meta-field"],"backgroundTag":"invalid-parameter-value","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}