Skip to content

PythonCodeSplitter: secondary-split pieces all report the whole function's start_line/end_line #13436

Closed

Description

@astroknot-sheep

Describe the bug
When PythonCodeSplitter falls back to the line-based secondary split for an oversized function or method, every piece gets the start_line / end_line of the whole function. A function on lines 7-47 split into 9 pieces gives 9 documents that all say start_line=7, end_line=47, so a retrieved piece can't be mapped back to the code it contains (e.g. for "see lines X-Y" citations or editor links).

To Reproduce

from haystack import Document
from haystack.components.preprocessors import PythonCodeSplitter

body = "".join(f"    value_{i} = compute_something_long({i}, factor={i * 3})\n" for i in range(30))
source = "import os\n\n# helper\n\ndef big():\n" + body
splitter = PythonCodeSplitter(min_effective_lines=1, max_effective_lines=5, secondary_split_length=8, secondary_split_overlap=2)
for d in splitter.run(documents=[Document(content=source)])["documents"]:
    print(d.meta["start_line"], d.meta["end_line"], d.meta.get("secondary_split"), repr(d.content.splitlines()[0]))

Output on main (cut down):

1 1 None 'import os'
5 35 True ''
5 35 True '    value_2 = ...'
5 35 True '    value_8 = ...'
...

Expected behavior
Each piece reports the lines it covers (5-9, 8-15, 14-21, ... with the 2-line overlap), still clamped to the function's range. The primary split already does this for the chunks it builds.

Additional context
_secondary_split deep-copies _build_chunk_meta([unit], ...) into every piece and doesn't use the split_idx_start offset that DocumentSplitter already gives each piece. I have a small fix with a regression test and can open a PR.

System:

  • Haystack version: main (2.x, commit 2026-10-24)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions