Skip to content

fix: use deepcopy instead of shallow copy when stripping orig_elements - #4402

Open
chuenchen309 wants to merge 2 commits into
Unstructured-IO:mainfrom
chuenchen309:fix/chunking-orig-elements-shallow-copy
Open

fix: use deepcopy instead of shallow copy when stripping orig_elements#4402
chuenchen309 wants to merge 2 commits into
Unstructured-IO:mainfrom
chuenchen309:fix/chunking-orig-elements-shallow-copy

Conversation

@chuenchen309

@chuenchen309 chuenchen309 commented Jul 15, 2026

Copy link
Copy Markdown

Problem

_Chunker._orig_elements copies each element before clearing its metadata.orig_elements, with an explicit comment stating this is because "these elements don't belong to us (the user may have downstream purposes for them)." But copy.copy() is a shallow copy — the copy's .metadata is the same object as the original's .metadata — so orig_element.metadata.orig_elements = None mutates the caller's own element in place, exactly contradicting the stated intent.

Sibling _TableChunker._orig_elements already does this correctly with copy.deepcopy(self._table).

Fix

Use copy.deepcopy instead of copy.copy, matching the already-correct sibling implementation.

Testing

AI-Generated disclosure

I used an AI coding assistant (Claude) to help identify this bug and draft the fix. I independently reproduced the caller mutation with a throwaway script before writing the test, cross-checked against the already-correct sibling _TableChunker implementation, and ran the full local test suite before preparing this change.

Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot 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.

No issues found across 4 files

Shadow auto-approve: would auto-approve. Fixes a shallow-copy bug that mutated the caller's element metadata. The change is bounded to one line, matches the already-correct sibling implementation, and adds a regression test. No operational, security, or product tradeoffs.

Re-trigger cubic

chuenchen309 and others added 2 commits July 16, 2026 06:05
_Chunker._orig_elements copies each element before clearing its
metadata.orig_elements, with an explicit comment stating this is
because "these elements don't belong to us (the user may have
downstream purposes for them)." But copy.copy() is a shallow copy --
the copy's `.metadata` is the same object as the original's `.metadata`
-- so `orig_element.metadata.orig_elements = None` mutates the
caller's own element in place, exactly contradicting the stated
intent.

Sibling `_TableChunker._orig_elements` already does this correctly
with `copy.deepcopy(self._table)`.

Added a regression test confirming a caller's element still has its
original `.metadata.orig_elements` value after `_Chunker._orig_elements`
is computed. Confirmed it fails against the pre-fix shallow copy and
passes with deepcopy. Ran the full test_unstructured/chunking/ suite
(334 passed) plus ruff check/format (clean).

Disclosure: I used an AI coding assistant (Claude) to help identify
this bug and draft the fix. I independently reproduced the caller
mutation with a throwaway script before writing the test, cross-checked
against the already-correct sibling _TableChunker implementation, and
ran the full local test suite before preparing this change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Adds the CHANGELOG entry and version bump required by CONTRIBUTING.md's
PR checklist for the orig_elements shallow-copy fix in the previous
commit.
@chuenchen309
chuenchen309 force-pushed the fix/chunking-orig-elements-shallow-copy branch from c3d6478 to ef6eed0 Compare July 15, 2026 22:05
@zeweihan

Copy link
Copy Markdown

@/tmp/gh_comment_unstructured_4402.md

@chuenchen309

Copy link
Copy Markdown
Author

Heads up — your last comment came through as just a literal @/tmp/gh_comment_unstructured_4402.md path, so the intended content didn't attach. Happy to take a look if you re-post it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants