Repository navigation
Description
Describe the bug
PythonCodeSplitter builds its list of source lines with str.splitlines(keepends=True), but the unit boundaries come from ast line numbers. Python only ends a line on \n, \r\n or \r, while str.splitlines() also breaks on form feed (\x0c), \x0b, \x1c to \x1e, \x85, U+2028 and U+2029.
A form feed on its own line is valid Python (PEP 8 mentions Control-L as a page separator, and Emacs-style code uses it). A U+2028 can appear in a comment or string. When the source contains one of these characters, every unit after it is sliced one line too early:
- a function's def line ends up in one chunk and its body in the next, so the chunks are no longer valid Python, although the docs say functions and methods are kept whole
- start_line and end_line point at different text than the chunk content (end_line can even be past the last line of the file)
- with strip_docstrings=True, the line that gets removed is the def line, and the docstring stays in the content (it is also copied to meta["docstrings"])
The concatenated content is still equal to the input, so nothing raises.
Error message
No error. The output is silently wrong.
Expected behavior
Chunks and start_line / end_line should be the same as for the same file without the form feed: each function stays whole, and strip_docstrings removes only the docstring.
Additional context
Python's own ast.get_source_segment avoids this with a private helper, ast._splitlines_no_ff, which splits on \n, \r\n and \r only. In PythonCodeSplitter._extract_units, replacing
source_lines = source.splitlines(keepends=True)with
source_lines = io.StringIO(source, newline="").readlines()makes the line list match the ast line numbers (newline="" splits on \n, \r\n and \r and keeps the line endings unchanged). I have a fix with tests and a release note ready and can open a PR if that's welcome.
To Reproduce
import ast
from haystack import Document
from haystack.components.preprocessors import PythonCodeSplitter
source = (
"import os\n"
"\n"
"\x0c\n"
"def first():\n"
" return 1\n"
"\n"
"\n"
"def second():\n"
' """Docstring of second."""\n'
" value = 2\n"
" return value\n"
)
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=1)
for chunk in splitter.run(documents=[Document(content=source)])["documents"]:
try:
ast.parse(chunk.content)
valid = "valid"
except SyntaxError:
valid = "INVALID"
print(chunk.meta["start_line"], chunk.meta["end_line"], valid, repr(chunk.content))
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=100, strip_docstrings=True)
(chunk,) = splitter.run(documents=[Document(content=source)])["documents"]
print(repr(chunk.content))
print(chunk.meta["docstrings"])Output on current main:
1 1 valid 'import os\n'
4 5 INVALID '\n\x0c\ndef first():\n'
8 12 INVALID ' return 1\n\n\ndef second():\n """Docstring of second."""\n value = 2\n return value\n'
'import os\n\n\x0c\ndef first():\n return 1\n\n\n """Docstring of second."""\n value = 2\n return value\n'
['Docstring of second.']
The file has 11 lines. In the first part, def first() is cut from its body and the last chunk reports end_line 12. In the second part, def second(): is gone and the docstring is still in the content.
FAQ Check
- Have you had a look at our new FAQ page?
System:
- OS: macOS 15.3.1
- Haystack version: main at 28568d5 (3.5.0-rc0), Python 3.12
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.
Activity
fix: align PythonCodeSplitter line splits with ast