Skip to content

fix(core): handle empty keyword table node IDs - #23360

Open
imharjot wants to merge 1 commit into
run-llama:mainfrom
imharjot:fix/empty-keyword-table-node-ids
Open

imharjot wants to merge 1 commit into
run-llama:mainfrom
imharjot:fix/empty-keyword-table-node-ids

Conversation

@imharjot

@imharjot imharjot commented Oct 4, 2026 •

Copy link
Copy Markdown

Description

Reading KeywordTable.node_ids raises a TypeError when the table is empty. This happens with a new empty SimpleKeywordTableIndex, after deleting its last node, and after saving and reloading either state.

Start the union from an empty set so these cases return set(). Populated tables still return the distinct node IDs in a separate set. No new dependencies.

Fixes #23359

New Package?

  • Yes
  • No

Version Bump?

  • Yes
  • No — this changes llama-index-core, which is exempt.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • I added new unit tests to cover this change

The four empty-table cases fail with the original code and pass with the fix. They exercise creation, deletion, persistence, and reload through the index APIs. A fifth test checks populated IDs and that changing the returned set doesn't change the keyword table. The tests use real keyword extraction and the local MockLLM.

From llama-index-core:

  • Keyword-table and storage suites: 31 passed.
  • Full core suite, with existing CI skip gates (env -u OPENAI_API_KEY CI=1 uv run --frozen -- pytest tests -q): 1,504 passed, 22 skipped, 6 xfailed.
  • uv run --frozen -- mypy llama_index: 479 source files passed.
  • uv run make format and uv run make lint: passed. Root pre-commit checks and commit hooks also passed for both changed files.

The first full-suite run used an invalid dummy OpenAI key and enabled three existing live-provider tests, which failed authentication. The CI-gated run above skips those tests. No provider credentials are needed for the new tests.

Suggested Checklist

  • I have performed a self-review of my own code
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes
  • I ran uv run make format; uv run make lint

AI assistance was used for the one-line change in KeywordTable.node_ids and the new regression tests. Verification included fail-before/pass-after checks, the full core suite under its CI skip conditions, source type checking, and the repository's formatting and lint checks.

@VANDRANKI VANDRANKI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Community review (does not clear the merge gate). I read the diff and the surrounding KeywordTable dataclass in data_structs.py. llama_index is not installed in my environment, so I did not run the new tests. I ran the underlying Python expression instead.

Verified: set.union(*{}.values()) raises TypeError: unbound method set.union() needs an argument, and set().union(*{}.values()) returns set(). So KeywordTable.node_ids raises on an empty table, and the one-line change fixes it. An empty table happens after the last node is deleted: _delete_node in indices/keyword_table/base.py removes keywords whose node set is empty, so the table can legitimately become {}, and the same applies to a freshly loaded persisted empty index. Both are in your parametrized test.

Notes:

  • set.union(a, b) and set().union(a, b) both return a new set, so callers still cannot mutate the table through the returned value. Your second test (node_ids.clear() leaves the table alone) locks that in, which is a nice guard against a future "optimization" that returns the stored set for a single-keyword table.
  • I searched the repo for other set.union(* uses in llama-index-core and found only this one, so there are no sibling instances of the bug.
  • Only data_structs.py and tests reference .node_ids on this struct in core as far as my code search showed. I did not check the integration packages, so downstream callers that relied on the TypeError (unlikely) are unchecked.
  • Test imports load_index_from_storage and StorageContext at module top. The persisted case writes into tmp_path, which is fine.

Looks correct and minimal to me. I would be comfortable with this merging once CI is green.

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]: KeywordTable.node_ids raises on an empty keyword index

2 participants