{"record":{"id":"db4fd2f6200530c7","repo":"deepset-ai/haystack","slug":"invalid-value-for-word-count-threshold-word-coun","errorCode":null,"errorMessage":"Invalid value for word_count_threshold: {word_count_threshold}. word_count_threshold must be > 0.","messagePattern":"Invalid value for word_count_threshold: (.+?)\\. word_count_threshold must be > 0\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/rankers/lost_in_the_middle.py","lineNumber":53,"sourceCode":"    for doc in result[\"documents\"]:\n        print(doc.content)\n    ```\n    \"\"\"\n\n    def __init__(self, word_count_threshold: int | None = None, top_k: int | None = None) -> None:\n        \"\"\"\n        Initialize the LostInTheMiddleRanker.\n\n        If 'word_count_threshold' is specified, this ranker includes all documents up until the point where adding\n        another document would exceed the 'word_count_threshold'. The last document that causes the threshold to\n        be breached will be included in the resulting list of documents, but all subsequent documents will be\n        discarded.\n\n        :param word_count_threshold: The maximum total number of words across all documents selected by the ranker.\n        :param top_k: The maximum number of documents to return.\n        \"\"\"\n        if isinstance(word_count_threshold, int) and word_count_threshold <= 0:\n            raise ValueError(\n                f\"Invalid value for word_count_threshold: {word_count_threshold}. word_count_threshold must be > 0.\"\n            )\n        if isinstance(top_k, int) and top_k <= 0:\n            raise ValueError(f\"top_k must be > 0, but got {top_k}\")\n\n        self.word_count_threshold = word_count_threshold\n        self.top_k = top_k\n\n    @component.output_types(documents=list[Document])\n    def run(\n        self, documents: list[Document], top_k: int | None = None, word_count_threshold: int | None = None\n    ) -> dict[str, list[Document]]:\n        \"\"\"\n        Reranks documents based on the \"lost in the middle\" order.\n\n        Before ranking, documents are deduplicated by their id, retaining only the document with the highest score\n        if a score is present.\n","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/rankers/lost_in_the_middle.py#L35-L71","documentation":"LostInTheMiddleRanker's __init__ rejects a word_count_threshold that is an int <= 0. The ranker uses this value as the maximum total number of words across all selected documents, so a zero or negative value is meaningless and would make document selection impossible. The library raises ValueError eagerly at construction time to fail fast.","triggerScenarios":"Calling LostInTheMiddleRanker(word_count_threshold=0) or any negative integer; the check only fires when the value is an int instance, so None, floats, or non-numeric values pass validation.","commonSituations":"Constructing the ranker from config files where a placeholder 0 was left; computing the threshold dynamically (e.g. len(docs) on an empty list or a subtraction going negative); YAML/JSON deserialization filling in 0 for missing keys.","solutions":["Pass a positive integer, e.g. LostInTheMiddleRanker(word_count_threshold=1024).","If the value comes from config, validate/coerce it before construction and fall back to a sensible default.","If you intended no word limit, omit the parameter and use top_k-based selection instead of passing 0."],"exampleFix":"// before\nranker = LostInTheMiddleRanker(word_count_threshold=0)\n// after\nranker = LostInTheMiddleRanker(word_count_threshold=1024)","handlingStrategy":"validation","validationCode":"def ensure_valid_word_count_threshold(v):\n    if isinstance(v, int) and v <= 0:\n        raise ValueError(f\"word_count_threshold must be > 0, got {v}\")\n    return v\n# call before: ensure_valid_word_count_threshold(cfg[\"word_count_threshold\"])","typeGuard":"def is_positive_int(v) -> bool:\n    return isinstance(v, int) and v > 0","tryCatchPattern":"try:\n    ranker = LostInTheMiddleRanker(word_count_threshold=cfg_threshold)\nexcept ValueError as e:\n    logger.warning(\"Falling back to default word_count_threshold: %s\", e)\n    ranker = LostInTheMiddleRanker()","preventionTips":["Never pass 0 as a sentinel for 'no limit'; use None or omit the parameter.","Validate config-derived integers with a positive-int check before component construction.","Note the isinstance(v, int) check: floats/None bypass validation, so coerce types explicitly."],"tags":["python","validation","ranker","constructor-argument"],"backgroundTag":"invalid-parameter-value","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}