Skip to content

fix: report per-piece line ranges for PythonCodeSplitter secondary splits - #13437

Merged
sjrl merged 3 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/python-code-splitter-secondary-line-range
Nov 12, 2026
Merged

sjrl merged 3 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/python-code-splitter-secondary-line-range

Conversation

@astroknot-sheep

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

When an oversized function or method goes through the line-based secondary split, every piece got a deep copy of the whole unit's metadata, including start_line / end_line. So all pieces of one function pointed at the same full range.

_secondary_split now maps each piece back to the file lines it covers, using the split_idx_start offset DocumentSplitter already sets. The unit source ends at unit.end_line and may begin a few lines earlier (blank lines and comments before the def), so the first physical line is end_line - line_count(source) + 1. Each piece's range is clamped to the function's start_line..end_line, which matches how primary chunks treat leading blank lines and comments. Lines are counted with io.StringIO(newline=""), the same \n / \r\n / \r rule _extract_units uses after #13301.

When strip_docstrings=True removed a docstring, the unit source no longer lines up with the file, so those pieces keep the function's full range as before.

How did you test it?

  • New test test_secondary_split_pieces_report_their_own_line_range: checks that each piece's start_line..end_line slice of the source matches the piece content, and that the ranges are increasing and span the function. It fails on main and passes with the fix.
  • pytest test/components/preprocessors/test_python_code_splitter.py: 75 passed.
  • ruff check / ruff format --check clean.

Notes for the reviewer

Other metadata (unit_kinds, qualified_name, secondary_split_index, ...) is unchanged. Only the line range is narrowed per piece. The class docstring mentions this.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

This PR was fully generated with an AI assistant. I have reviewed the changes and run the relevant tests.

@astroknot-sheep
astroknot-sheep requested a review from a team as a code owner October 26, 2026 03:30
@astroknot-sheep
astroknot-sheep requested review from julian-risch and removed request for a team October 26, 2026 03:30
@vercel

vercel Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@astroknot-sheep is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Oct 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @astroknot-sheep, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from julian-risch October 26, 2026 04:37
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Oct 26, 2026
@HaystackBot
HaystackBot marked this pull request as draft October 26, 2026 04:37
@HaystackBot
HaystackBot marked this pull request as ready for review October 26, 2026 04:46
@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Oct 26, 2026
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @astroknot-sheep! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@chrikrah chrikrah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@astroknot-sheep from my side this can be merged.

non-blocking: only the strip_docstrings=True fallback your description names kept the full range, (3, 35).

# git archive 1b8e4fa9c, Python 3.12.3; haystack resolves into each tree
$ python -m pytest test/components/preprocessors/test_python_code_splitter.py -q -p no:cacheprovider
75 passed            # head 1b8e4fa9c
1 failed, 74 passed  # python_code_splitter.py from base 622967d
# probe, 30-line body, length 8, overlap 2: method with decorator, CRLF and docstring
# cases all map each piece to its own lines; with strip_docstrings=True: (3, 35) x5
# not run: the rest of test/components/preprocessors

Want an issue for mapping that case?

@astroknot-sheep

Copy link
Copy Markdown
Contributor Author

Thanks a lot for testing this so carefully, @chrikrah!

Yes, the strip_docstrings=True path still falls back to the full range. Once docstrings are stripped, the split text no longer lines up with the source lines, so I left that case out to keep this PR small. If the maintainers want it handled, I'm happy to open a separate issue and send a follow-up for it.

@HaystackBot
HaystackBot removed the request for review from julian-risch November 5, 2026 08:27
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Nov 5, 2026
@HaystackBot
HaystackBot marked this pull request as draft November 5, 2026 08:27
@HaystackBot
HaystackBot marked this pull request as ready for review November 5, 2026 08:51
@HaystackBot HaystackBot removed the cla-pending PR is in draft until the contributor signs the CLA label Nov 5, 2026
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @astroknot-sheep! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Nov 6, 2026
@sjrl
sjrl removed the request for review from julian-risch November 6, 2026 12:46
@sjrl sjrl self-assigned this Nov 6, 2026
@sjrl
sjrl self-requested a review November 6, 2026 12:46
@astroknot-sheep

astroknot-sheep commented Nov 6, 2026 via email

Copy link
Copy Markdown
Contributor Author

@astroknot-sheep

Copy link
Copy Markdown
Contributor Author

Thanks @sjrl! Applied both suggestions: single backticks in the docstring, and the chunk.content is not None check so mypy passes. Ran the splitter tests, mypy and ruff locally, all green.

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/converters
  pptx.py
  haystack/components/preprocessors
  python_code_splitter.py
  sentence_tokenizer.py
  text_cleaner.py
  haystack/components/retrievers
  auto_merging_retriever.py
  haystack/document_stores/in_memory
  document_store.py
Project Total  

This report was generated by python-coverage-comment-action

@astroknot-sheep

Copy link
Copy Markdown
Contributor Author

Thanks @sjrl! Updated the docs and added the clamp comment as you asked. Splitter tests, mypy and ruff are green locally.

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@sjrl
sjrl merged commit 4ad4146 into deepset-ai:main Nov 12, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants