run-llama/llama_index · error · ImportError

Please install nltk: `pip install nltk`

Error message

Please install nltk: `pip install nltk`

What it means

rake_extract_keywords() lazily imports nltk at call time and converts ImportError into a user-facing ImportError telling you to pip install nltk. nltk itself is an optional dependency: it is only needed when extracting keywords with the RAKE strategy, so the package imports fine without it and the failure appears late, at index build time.

Source

Thrown at llama-index-core/llama_index/core/indices/keyword_table/utils.py:33

    tokens = [t.strip().lower() for t in re.findall(r"\w+", text_chunk)]
    if filter_stopwords:
        tokens = [t for t in tokens if t not in globals_helper.stopwords]

    token_counts = Counter(tokens)
    keywords = [keyword for keyword, count in token_counts.most_common(max_keywords)]
    return set(keywords)


def rake_extract_keywords(
    text_chunk: str,
    max_keywords: Optional[int] = None,
    expand_with_subtokens: bool = True,
) -> Set[str]:
    """Extract keywords with RAKE."""
    try:
        import nltk
    except ImportError:
        raise ImportError("Please install nltk: `pip install nltk`")
    try:
        from rake_nltk import Rake
    except ImportError:
        raise ImportError("Please install rake_nltk: `pip install rake_nltk`")

    r = Rake(
        sentence_tokenizer=nltk.tokenize.sent_tokenize,
        word_tokenizer=nltk.tokenize.wordpunct_tokenize,
    )
    r.extract_keywords_from_text(text_chunk)
    keywords = r.get_ranked_phrases()[:max_keywords]
    if expand_with_subtokens:
        return set(expand_tokens_with_subtokens(keywords))
    else:
        return set(keywords)


def extract_keywords_given_response(

View on GitHub (pinned to afd0fef371)

Solutions

  1. pip install nltk (and rake_nltk — both are required, see the sibling error) or install the extras bundle that includes them.
  2. Verify the import early in your startup: python -c "import nltk; import rake_nltk" before ingestion runs.
  3. If you cannot add deps, use SimpleKeywordTableIndex (regex-based extraction) which needs neither package.

Example fix

# before
index = RAKEKeywordTableIndex.from_documents(docs)  # ImportError: install nltk

# after
pip install nltk rake_nltk
index = RAKEKeywordTableIndex.from_documents(docs)
Defensive patterns

Strategy: validation

Validate before calling

def rake_available() -> bool:
    try:
        import nltk  # noqa: F401
        import rake_nltk  # noqa: F401
        return True
    except ImportError:
        return False

assert rake_available(), "RAKE keyword extraction requires nltk and rake_nltk"

Try / catch

try:
    index = RAKEKeywordTableIndex.from_documents(docs)
except ImportError as e:
    if "nltk" in str(e):
        index = SimpleKeywordTableIndex.from_documents(docs)  # no extra deps
    else:
        raise

Prevention

When it happens

Trigger: Building a KeywordTableIndex (or calling as_retriever with KeywordTableRetrieverMode.RAKE) on a base llama-index install without the extras; SimpleKeywordTableIndex vs RAKEKeywordTableIndex — using RAKEKeywordTableIndex.from_documents triggers rake_extract_keywords.

Common situations: Deploying to slim Docker images that only pip install llama-index-core; swapping SimpleKeywordTableIndex for RAKEKeywordTableIndex without adding deps; CI passing (no RAKE path exercised) while production ingestion fails.

Related errors


AI-assisted analysis of run-llama/llama_index@afd0fef371 (2026-08-15). Data as JSON: /api/errors/170cc618ef3ae861. Report an issue: GitHub.