Skip to content

fix(html): stop key-value graph rendering at a to_child cycle - #923

Merged
ceberam merged 2 commits into
docling-project:mainfrom
r0h1tb:fix/html-key-value-cycle
Nov 26, 2026
Merged

ceberam merged 2 commits into
docling-project:mainfrom
r0h1tb:fix/html-key-value-cycle

Conversation

@r0h1tb

@r0h1tb r0h1tb commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

export_to_html() raises RecursionError on a document whose key-value graph has a to_child cycle, because _HTMLGraphDataSerializer._render_cell_tree follows child links without tracking what it has rendered.

docling's own XBRL backend produces such a graph. Its ground truth tests/data/xbrl/groundtruth/mlac-20251231.xml.json has GeneralAndAdministrativeExpense and OperatingIncomeLoss each linked as the other's child: the backend keeps one cell per concept and adds to_child links from both the presentation and the calculation linkbase. Markdown, text and DocTags export of that document work; HTML export fails.

_render_cell_tree now tracks the cells rendered for the whole graph and skips a child that was already rendered, so each cell is rendered once and a cycle ends.

Output change: a cell with several parents is shown only under the first parent that reaches it. On main it is repeated under every parent, which also affects acyclic graphs with shared children. A cell whose children were all shown elsewhere keeps its own list item. On the mlac document the export goes from 770 list items (the earlier one-per-path version of this PR) to 503, one per cell that has a value or children.

Dense graphs, one root over k cells that all link to each other:

k list items time
9 10 0.03 s
11 12 <0.01 s
50 51 <0.01 s

On main these raise RecursionError. With one render per path, k=9 produced 623,530 list items (14.7 MB).

Tests:

  • test_html_key_value_graph_with_cycle: a two-cell cycle renders each cell once. Fails on main with RecursionError.
  • test_html_key_value_graph_shared_child_rendered_under_first_parent: a diamond (root -> A, root -> B, A -> C, B -> C) pins the full list, with C under A only and B kept. Fails on main.
  • test_html_key_value_graph_dense_cycles_render_each_cell_once: an 8-cell clique renders 9 list items. Fails on main and on the one-per-path version.

Full suite with uv sync --all-extras --all-packages: 1711 → 1714 passed, no failures. ruff passes; ty reports the same diagnostics as main.

@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @r0h1tb, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 All 2 merge protections satisfied — ready to merge.

Show 2 satisfied protections

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

🟢 Require two reviewer for test updates

When test data is updated, we require two reviewers

  • #approved-reviews-by >= 2

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline labels Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit ba910282f1. The analysis read the diff only. It did not run code from this pull request.

Warning

The AI analysis did not finish. Only the deterministic checks are shown.

CI safety: not assessed.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@github-actions github-actions 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.

AI first review (advisory): no blocker found

This review covers the full diff at d90a2a2f81. It read the code and did not run it.

The fix stops the RecursionError for a to_child cycle, and the new test traces correctly against the code. The remaining risk is that the walk follows every simple path, not every cell once. A dense cyclic graph can still take exponential time or produce huge HTML.

A maintainer makes the merge decision. This review never approves the pull request.

cell_text = f"<strong>{cell_text}</strong>: {', '.join(value_texts)}"

path = ancestors | {cell_id}
child_ids = [child_id for child_id in child_links.get(cell_id, []) if child_id not in path]

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.

question: Path-only cycle check can still blow up on dense cyclic graphs, and the PR text says "rendered once"

The check skips only children that are on the current path. A cell that several paths reach is rendered once per path. In a graph with many cross-linked to_child edges, the number of simple paths grows exponentially. XBRL graphs with both presentation and calculation links are the case this PR targets. The PR checks only the small mlac document (0.04 s). It does not show that larger XBRL graphs finish. The PR text also says "each cell on a cycle is rendered once". That holds only for the small test graph.

Please show evidence on a larger or denser cyclic graph, for example a clique of 8 to 10 to_child cells under one root. If it blows up, use a global visited set. That changes the output for DAGs with shared children, which today render once per parent. If the team wants to keep that output, add a regression test that pins the diamond behavior. Otherwise, correct the PR text.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 3, 2026
@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @r0h1tb for the fix and for the clear description.

However, I think we should address the comment left by the AI bot. The automated review comment about this is legitimate, and I confirmed it with measurements. The new check only skips a child that is on the current root-to-cell path, so it prevents infinite recursion but not exponential output. A cell that several paths reach is rendered once per path, and each back edge adds more simple paths.

All timings are on the PR branch merged with current main. The first graph is one root linked to k cells that are all linked to each other (a clique of to_child links, which is the case the bot suggested):

k (clique under one root) <li> elements HTML size time
8 69,281 1.6 MB 0.08 s
9 623,530 14.7 MB 0.52 s
10 6,235,301 147.7 MB 5.3 s
11 68,588,312 1.63 GB 60.6 s

On main the same graphs raise RecursionError, so the PR turns a crash into a run that grows about 10x per added cell. At k=11 the export is effectively a hang and needs 1.6 GB of memory for the string. A document with a handful of densely cross-linked concepts can therefore still make export_to_html() unusable, which is the scenario the PR sets out to fix.

The duplication is not only theoretical for the XBRL data cited in the description. In mlac-20251231.xml.json the key-value graph has 1,819 cells and 526 to_child links. 37 cells have more than one parent. The export currently produces 770 <li> elements, and a variant that renders each cell once produces 523, so about a third of the current output is repeated subtrees. The mlac document is small, but the PR only shows this one document (0.04 s), so it does not demonstrate that larger filings, which have more presentation and calculation links, finish. Please add evidence for a larger or denser case. The clique above is the quickest way to produce one.

The same growth already exists on main for acyclic graphs with shared children. A root with 24 cells linked in a forward-only order (no cycles) renders 8.4 million <li> elements in 4.6 s. Cycles are not the only trigger, but they make the pattern much more likely. This also means a global visited set changes the output of graphs that never had a cycle, so it needs an explicit decision.

I have the following suggestion: track the cells already rendered for the whole walk. Thread a rendered: set[int] through _render_cell_tree, add the cell on entry and skip children already in it, and skip a child inside the loop as well as in the list comprehension, because an earlier sibling's subtree can render a later sibling. I prototyped this on top of your branch. All the graphs in the table above render in under 0.03 s with one <li> per cell, the mlac document drops from 770 to 523 <li> elements and renders in 0.03 s, and tests/test_serialization.py still passes (249 passed, 1 xfailed), so no existing ground-truth file depends on the repeated subtrees. The side effect is that a cell with several parents is shown only under the first parent that reaches it, which is the output change the PR text would need to mention. Please add a regression test with a diamond (root -> A, root -> B, A -> C, B -> C) that pins which parent keeps the child, and keep the cycle test.

Please, update the PR description according to these changes.

Thanks again for the fix. The structure of the change is good, and I expect this to be a quick follow-up.

_render_cell_tree followed to_child links without tracking the path, so a cycle recursed until RecursionError and export_to_html() failed. The XBRL backend in docling produces such a cycle in tests/data/xbrl (mlac-20251231.xml): its presentation and calculation linkbases give GeneralAndAdministrativeExpense and OperatingIncomeLoss opposite parent-child links. A child already on the current path is now skipped.

Signed-off-by: Rohit Behera <126186063+r0h1tb@users.noreply.github.com>
Skipping only children on the current path stopped the recursion, but a
cell reachable through several paths was still rendered once per path, so
a densely cross-linked graph grew about tenfold per added cell. Track the
cells rendered for the whole graph and skip a child that was already
rendered, also inside the loop, since an earlier sibling's subtree can
render a later sibling.

A cell with several parents is now shown only under the first parent that
reaches it. A cell whose children were all rendered elsewhere keeps its
own list item.

Signed-off-by: Rohit Behera <126186063+r0h1tb@users.noreply.github.com>
@r0h1tb
r0h1tb force-pushed the fix/html-key-value-cycle branch from d90a2a2 to ba91028 Compare November 25, 2026 09:24
@r0h1tb

r0h1tb commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Done, on top of current main. _render_cell_tree now gets one rendered set for the whole graph and skips a child that is already in it, both when building the child list and again inside the loop.

One difference from your prototype: a cell whose children were all rendered under an earlier parent falls into the leaf branch, which returns an empty string when the cell has no value, so B in the diamond would disappear. It now keeps its own <li> without a nested list. The diamond test pins the whole list: C appears only under A, and B stays.

The clique renders k+1 list items for k = 9, 11 and 50, in at most 0.03 s. The mlac document renders 503 list items, not your 523; I checked that each cell with a value or children appears exactly once. The full suite passes (1711 → 1714), and I updated the description.

@github-actions github-actions Bot removed ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) labels Nov 25, 2026

@ceberam ceberam left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @r0h1tb for the quick and careful update.
By the way, you were right on the numbers. My prototype only checked child_ids to choose between the nested-list branch and the leaf branch.

The PR looks good to me 🚀

@ceberam
ceberam merged commit 0f9518a into docling-project:main Nov 26, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working serialization Output serializers: Markdown, HTML, LaTeX, JSON, plain-text, outline

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants