Skip to content

fix: handle missing nodes key in get_leaf_nodes#351

Open
fooSynaptic wants to merge 1 commit into
VectifyAI:mainfrom
fooSynaptic:fix/get-leaf-nodes-keyerror-330
Open

fix: handle missing nodes key in get_leaf_nodes#351
fooSynaptic wants to merge 1 commit into
VectifyAI:mainfrom
fooSynaptic:fix/get-leaf-nodes-keyerror-330

Conversation

@fooSynaptic

Copy link
Copy Markdown

Fixes #330

Problem

get_leaf_nodes() crashes with KeyError: 'nodes' when traversing trees built by list_to_tree().

list_to_tree() runs clean_node() on every node. For nodes with no children, clean_node() deletes the nodes key entirely (del node['nodes']), rather than leaving it as an empty list. Later, get_leaf_nodes() checks leaf status via structure['nodes'], which raises KeyError on those nodes.

Fix

Use .get('nodes') instead of direct key access:

if not structure.get('nodes'):

When the key is absent, .get('nodes') returns None (falsy), so the node is correctly treated as a leaf. This matches how other helpers in the same file already access nodes (e.g. format_structure, is_leaf_node).

Tests

Added tests/test_issue_330.py:

  • leaf dict without a nodes key is returned as a leaf
  • list_to_tree()get_leaf_nodes() end-to-end traversal does not raise

pytest tests/test_issue_330.py -v — 2 passed locally.

@fooSynaptic

Copy link
Copy Markdown
Author

Hi @KylinMountain — gentle ping when you have a moment.

This PR fixes #330 (KeyError: 'nodes' in get_leaf_nodes after list_to_tree()). I noticed a similar one-line fix was merged in #331, but main still has the direct structure['nodes'] access, so this re-applies the guard and adds regression tests in tests/test_issue_330.py.

Happy to adjust anything per your feedback — thanks for maintaining PageIndex!

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.

Bug: get_leaf_nodes raises KeyError on leaf nodes due to missing nodes key

1 participant