Skip to content

fix(partition): keep section parent_id after images and other non-text elements - #4566

Merged
cragwolfe merged 5 commits into
Unstructured-IO:mainfrom
kerthans:fix/hierarchy-parent-id-after-image
Nov 13, 2026
Merged

cragwolfe merged 5 commits into
Unstructured-IO:mainfrom
kerthans:fix/hierarchy-parent-id-after-image

Conversation

@kerthans

@kerthans kerthans commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Root cause

set_element_hierarchy() walks a parent stack and pops every entry that is not a valid parent of the current element. HIERARCHY_RULE_SET only lists text-like children for Title and Header, so an element whose category is in no rule (Image, PageBreak, Formula, CodeSnippet, Address, EmailAddress, PageNumber, Footer) popped the whole stack, including the section heading, and was then pushed itself. Every following element in the same section got parent_id=None.

partition_md(text="# Intro\n\nFirst paragraph.\n\n![alt](x.png)\n\nSecond paragraph.")
# before: Image.parent_id is None, "Second paragraph".parent_id is None
# after:  both point to the "Intro" Title

Change

  • Add the eight types above as children of Title and Header in HIERARCHY_RULE_SET.
  • Remove the duplicated "Table" entry from both lists.
  • With this, [Title, NarrativeText, Image, NarrativeText] resolves as follows: the Image becomes a child of the Title, the next NarrativeText pops the Image (not a valid parent) and attaches to the Title.

Related

Open PR #4503 (HTML <figure> handling) also adds "Image" and "CodeSnippet" to the same rule lists, so whichever lands second will need a small rebase.

Tests

  • New Describe_set_element_hierarchy tests: Image and PageBreak between paragraphs under a Title, and an Image under a Header. They fail before the change and pass after it.
  • test_unstructured/partition/{common,html,test_md.py,test_docx.py,...}, chunking, documents and staging pass locally. Ingest expected outputs exclude metadata.parent_id, so no fixtures change.

Behavior change

  • Elements of these eight types that follow a Title/Header now get that heading as parent_id (previously None).
  • Elements after them in the same section keep the heading as parent_id instead of None.
  • Footer and PageNumber are now also parented to the preceding heading. They are page furniture, so this is arguably less meaningful, but it stops them from breaking the hierarchy of the content that follows.

AI assistance was used to prepare this change; I reviewed and tested it.

View guided diff

@kerthans
kerthans force-pushed the fix/hierarchy-parent-id-after-image branch from cc278a3 to 08253b8 Compare October 21, 2026 05:20

@cragwolfe cragwolfe 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.

Reviewed 08253b813280145a160caa2adf46469fc9ad8114 with GPT Pro.

P1 — Preserving cross-page section ancestry causes HTML export to omit continuation content (unstructured/partition/common/metadata.py:68–75,87–94; consumer: unstructured/partition/html/convert.py:263–277). With a page-one heading followed by page-two <pre>x = 1</pre>, a paragraph and a subsection, the new rules correctly keep those elements under the page-one heading. Default elements_to_html() groups by page first, cannot find that parent in the page-two group, and returns only elements with parent_id is None. The code, following paragraph and subsection—including its children—disappear from the export. Before this change, code cleared the heading stack and this content remained exportable. Header parents and other newly admitted categories have the same failure path.

Keep the hierarchy fix and make an element whose parent is outside its page group an export root within that group. Preserve source parent IDs, page metadata, IDs, same-page nesting and current sanitization behavior. Add real partition/export regressions for cross-page code and retained image payloads, including Header parents, page furniture and local subsections; assert exact-once output and unchanged source metadata.

This is the shared export issue previously found in #4503. Its narrow exporter repair is present on that open PR, but absent from this head and current main. Consolidating that shared repair here would let #4503 consume it without duplicating changes. The other list/caption findings on #4503 concern its additional figure traversal and are not new findings against this target.

Required CI remains unverified: workflow runs require action; Socket checks alone do not establish test success. No local tests or reproductions were run.

(authored by codex)

kerthans and others added 2 commits October 22, 2026 09:46
…t elements

set_element_hierarchy() popped the enclosing Title/Header off the parent stack when it met an element type missing from HIERARCHY_RULE_SET (Image, PageBreak, Formula, CodeSnippet, Address, EmailAddress, PageNumber, Footer), so later elements in the same section got parent_id=None. Add those types as children of Title and Header and drop the duplicate Table entries.

Co-authored-by: Cursor <[email protected]>
Keeping the section heading across non-text elements means page-two
content can have a page-one parent. elements_to_html() groups elements by
page, could not find that parent in the page-two group, and returned only
elements without a parent_id, so the code, the paragraphs after it and
any subsection were dropped from the export.

An element whose parent is not in its page group is now a root of that
group. Element metadata is unchanged.

This is the exporter fix from Unstructured-IO#4503.

Co-authored-by: Rohit Behera <[email protected]>
Signed-off-by: kerthans <[email protected]>
Co-authored-by: Cursor <[email protected]>
@kerthans
kerthans force-pushed the fix/hierarchy-parent-id-after-image branch from 08253b8 to 4552e40 Compare October 22, 2026 01:53
@kerthans

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Addressed in 4552e40:

  • Rebased on main and added the exporter fix from fix(html): extract <figure> content instead of discarding it #4503 (co-authored with @r0h1tb): in elements_to_html(), an element whose parent is not in its page group is now a root of that group. Source parent_id, page numbers and IDs are unchanged.
  • Added partition/export regressions: page-two code and a retained image payload under a page-one heading, and Header parents with CodeSnippet, Image, PageNumber and Footer. Each also has a local subsection. They assert that each element is exported exactly once on its own page, that the subsection keeps its children, and that metadata is unchanged after export. All 6 fail without the exporter fix.
  • 489 HTML and metadata tests pass locally; ruff check and format pass.

Once this lands, #4503 can drop its copy of the exporter change.

@cragwolfe cragwolfe 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.

Reviewed 4552e40de4f81b5c507f2a5d56cf4b13f75dd3c6 against 33779f9a218ec6229b37ddd30b1a1cfd5c59a9a7 with GPT Pro. Source review approved; no new actionable findings.

The prior P1 cross-page HTML export omission is resolved. Children whose parent is outside the current page now render as roots on that page, while local parents retain their children. The added regressions cover partitioned code and retained image payloads, Header parents, page furniture, local subsections, exact-once output, and unchanged source IDs and metadata. The final sanitization path remains intact. Shared hierarchy, partitioning, chunking, and staging behavior were also reviewed.

Required CI is still unverified: CI, Security, CodeQL, and Partition Benchmark report action_required with no jobs started. The required changelog, Docker, lint, unit, and ingest checks have no successful results for this head. Socket checks are green but do not establish test success. Workflow approval and successful required checks remain necessary before merge. No local tests or reproductions were run.

(authored by codex)

… section parent

A slide break no longer clears the parent stack, so the sample deck's page
breaks and the table on its untitled slide now have the previous heading
as parent_id.

Co-authored-by: Cursor <[email protected]>
@kerthans

Copy link
Copy Markdown
Contributor Author

The only CI failure was test_partition_pptx_hierarchy_sample_document; the other failed jobs were cancelled or passed. It asserted the old behavior, where a slide break cleared the parent stack. With PageBreak in the rule set, the three page breaks in sample-presentation.pptx and the table on its untitled fourth slide now keep the previous heading as parent_id, which is the change this PR makes. Updated the four expected values in d29eba3. The pptx, metadata and HTML tests pass locally.

If you'd rather a slide break still start a new section in PPTX, I can do that inside partition_pptx instead.

@cragwolfe
cragwolfe enabled auto-merge November 13, 2026 22:30
@cragwolfe
cragwolfe added this pull request to the merge queue Nov 13, 2026
Merged via the queue into Unstructured-IO:main with commit e1288ff Nov 13, 2026
2 checks passed
ssingh231219 added a commit to ssingh231219/unstructured that referenced this pull request Nov 17, 2026
Resolve the conflict in convert.py by taking upstream's
version, since Unstructured-IO#4566 already keeps an element whose
parent is absent in HTML export.
r0h1tb added a commit to r0h1tb/unstructured that referenced this pull request Nov 25, 2026
`<summary><h3><img ...>Question?</h3></summary>` was handed to ordinary
heading traversal, which classifies text after a nested block without
`Title`, so the label became `Text` and `chunk_by_title()` saw no new
section. The label traversal now descends into a heading wrapper as it
does into a `<p>` or `<div>`.

Also add regression tests for the heading chain across a summary icon
and for exporting disclosure content whose children are on a later
page. Both pass with the hierarchy and export changes from Unstructured-IO#4566 that
this branch now merges.

Signed-off-by: Rohit Behera <[email protected]>
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