{"record":{"id":"3a371b92e5582b41","repo":"deepset-ai/haystack","slug":"expected-chars-per-line-must-be-at-least-1","errorCode":null,"errorMessage":"expected_chars_per_line must be at least 1.","messagePattern":"expected_chars_per_line must be at least 1\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"haystack/components/preprocessors/python_code_splitter.py","lineNumber":130,"sourceCode":"            module-level docstring is kept in place since it is itself a top-level unit.\n        :param preserve_class_definition: If ``True`` (default), chunks that contain class\n            members but not the class header are prefixed with the bare class signature\n            (decorators plus the ``class Foo(...):`` lines) in source order.\n        :param secondary_split_overlap: Line overlap for the secondary splitter; only used\n            in the oversized fallback. The primary AST split never adds overlap.\n        :param secondary_split_length: Lines per chunk for the secondary splitter.\n            Defaults to ``max_effective_lines`` when ``None``.\n        :raises ValueError: If any parameter is invalid (negative, zero where positive is\n            required, or ``min_effective_lines > max_effective_lines``).\n        \"\"\"\n        if min_effective_lines < 1:\n            raise ValueError(\"min_effective_lines must be at least 1.\")\n        if max_effective_lines < 1:\n            raise ValueError(\"max_effective_lines must be at least 1.\")\n        if min_effective_lines > max_effective_lines:\n            raise ValueError(\"min_effective_lines must not be greater than max_effective_lines.\")\n        if expected_chars_per_line < 1:\n            raise ValueError(\"expected_chars_per_line must be at least 1.\")\n        if oversized_factor < 1:\n            raise ValueError(\"oversized_factor must be at least 1.\")\n        if secondary_split_overlap < 0:\n            raise ValueError(\"secondary_split_overlap must be non-negative.\")\n        if secondary_split_length is not None and secondary_split_length < 1:\n            raise ValueError(\"secondary_split_length must be at least 1.\")\n\n        self.min_effective_lines = min_effective_lines\n        self.max_effective_lines = max_effective_lines\n        self.expected_chars_per_line = expected_chars_per_line\n        self.oversized_factor = oversized_factor\n        self.strip_docstrings = strip_docstrings\n        self.preserve_class_definition = preserve_class_definition\n        self.secondary_split_overlap = secondary_split_overlap\n        self.secondary_split_length = secondary_split_length\n\n    def _effective_lines(self, text: str) -> int:\n        \"\"\"Return the number of *effective lines* for ``text`` (see class docstring).\"\"\"","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/components/preprocessors/python_code_splitter.py#L112-L148","documentation":"PythonCodeSplitter's __init__ requires expected_chars_per_line >= 1; this estimate drives length-based chunk calculations. Zero or negative estimates are invalid, so ValueError is raised.","triggerScenarios":"Calling PythonCodeSplitter(expected_chars_per_line=0) or negative, usually from a config default of 0, a failed statistic computation (e.g. mean over an empty corpus), or a division by a zero/huge denominator.","commonSituations":"Computing average line length from an empty sample, env/config placeholders left at 0, or mistaking the parameter for a boolean/flag.","solutions":["Pass a realistic estimate, e.g. expected_chars_per_line=80.","Omit the parameter to use the default.","Clamp computed values: expected_chars_per_line=max(1, int(avg)).","Fix the upstream statistics code that produced 0 (guard against empty inputs)."],"exampleFix":"// before\navg = sum(len(l) for l in lines) / max(1, len(lines)) if lines else 0\nPythonCodeSplitter(expected_chars_per_line=int(avg))\n// after\nPythonCodeSplitter(expected_chars_per_line=max(1, int(avg)) if avg else 80)","handlingStrategy":"validation","validationCode":"sample = [len(l) for l in lines] or [80]\nexpected_chars_per_line = max(1, int(sum(sample) / len(sample)))","typeGuard":"def is_valid_char_estimate(v) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 1","tryCatchPattern":"try:\n    splitter = PythonCodeSplitter(expected_chars_per_line=estimate)\nexcept ValueError as e:\n    logging.warning(\"Invalid expected_chars_per_line (%s), using 80\", e)\n    splitter = PythonCodeSplitter(expected_chars_per_line=80)","preventionTips":["Guard average-length computations against empty inputs.","Never leave numeric config placeholders at 0 in production.","Clamp with max(1, int(value)).","Use a realistic default (e.g. 80) when no statistics are available."],"tags":["python","validation","constructor-argument","haystack"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}