Skip to content

fix: deep-copy document metadata in PythonCodeSplitter - #12468

Open
bharadwaj-pendyala wants to merge 1 commit into
deepset-ai:mainfrom
bharadwaj-pendyala:fix/python-code-splitter-deepcopy-meta
Open

fix: deep-copy document metadata in PythonCodeSplitter#12468
bharadwaj-pendyala wants to merge 1 commit into
deepset-ai:mainfrom
bharadwaj-pendyala:fix/python-code-splitter-deepcopy-meta

Conversation

@bharadwaj-pendyala

Copy link
Copy Markdown
Contributor

Related Issues

None.

Proposed Changes:

PythonCodeSplitter shallow-copied parent metadata: appending to chunks[0].meta["tags"] on main at 75ee514 changed every chunk's tags and the input Document too. DocumentSplitter doesn't.

_build_chunk_meta now deep-copies filtered parent metadata, matching DocumentSplitter. The oversized-function secondary split also gave every piece a shallow dict() of one shared base_meta; each piece now builds its own.

The copy preserves meta["a"] is meta["b"] within a chunk, as DocumentSplitter does.

How did you test it?

Three unit tests in test_python_code_splitter.py, named after equivalents in the CSV, Markdown and hierarchical splitter test files. The two test_nested_metadata_is_not_shared_* tests fail without the fix and pass with it. test_metadata_keys_sharing_one_value_still_share_it_within_a_chunk preserves the aliasing contract.

hatch isn't available in my environment, so I ran pytest, ruff, mypy, and the pre-commit hooks directly. All passed. The only failures across test/components/preprocessors/ are the 31 NLTK-download tests that already fail on a clean checkout here.

Notes for the reviewer

Deep-copying per chunk isn't free: 200 chunks against a 5,000-entry meta takes about 4.3s, but DocumentSplitter already pays exactly that. Metadata holding something deepcopy can't handle raises TypeError, as every other splitter already does.

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

Checklist

PythonCodeSplitter shallow-copied the parent document's metadata, so every
chunk shared its nested objects with the other chunks and with the caller's
Document. The oversized-function secondary split shared one base_meta the
same way.

Deep-copy the filtered mapping in one call, which also keeps two metadata
keys pointing at one object aliased within a chunk, matching DocumentSplitter.
@vercel

vercel Bot commented Aug 26, 2026

Copy link
Copy Markdown

@bharadwaj-pendyala is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/preprocessors
  python_code_splitter.py
Project Total  

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

@bharadwaj-pendyala
bharadwaj-pendyala marked this pull request as ready for review August 29, 2026 02:16
@bharadwaj-pendyala
bharadwaj-pendyala requested a review from a team as a code owner August 29, 2026 02:16
@bharadwaj-pendyala
bharadwaj-pendyala requested review from bogdankostic and removed request for a team August 29, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant