run-llama/llama_index · error · ImportError

Please install rake_nltk: `pip install rake_nltk`

Error message

Please install rake_nltk: `pip install rake_nltk`

What it means

After confirming nltk is present, rake_extract_keywords() lazily imports rake_nltk (the RAKE algorithm wrapper). If that second optional package is missing it raises ImportError with 'pip install rake_nltk'. Like nltk, it is only pulled in on the RAKE keyword-extraction path, so the error surfaces only when a RAKEKeywordTableIndex or retriever_mode='rake' actually runs.

Source

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

    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(
    response: str, lowercase: bool = True, start_token: str = ""
) -> Set[str]:
    """
    Extract keywords given the GPT-generated response.

View on GitHub (pinned to afd0fef371)

Solutions

  1. pip install rake_nltk (keep nltk installed too — both are required by this function).
  2. Add both to your dependency manifest so environments are reproducible: nltk and rake_nltk alongside llama-index-core.
  3. Alternatively avoid RAKE entirely with SimpleKeywordTableIndex, which requires no extra packages.

Example fix

# before
# nltk installed, rake_nltk missing
index = RAKEKeywordTableIndex.from_documents(docs)  # ImportError

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

Strategy: validation

Validate before calling

import importlib.util

for mod in ("nltk", "rake_nltk"):
    assert importlib.util.find_spec(mod) is not None, f"missing {mod}: pip install {mod}"

Try / catch

try:
    index = RAKEKeywordTableIndex.from_documents(docs)
except ImportError as e:
    if "rake_nltk" in str(e):
        index = SimpleKeywordTableIndex.from_documents(docs)
    else:
        raise

Prevention

When it happens

Trigger: nltk installed but rake_nltk not; building RAKEKeywordTableIndex.from_documents(docs); calling KeywordTableIndex.as_retriever(retriever_mode=KeywordTableRetrieverMode.RAKE) — both paths execute rake_extract_keywords.

Common situations: Installing only nltk after seeing the previous error and hitting this second guard; minimal deployments of llama-index-core; lockfiles generated on machines that never exercised the RAKE path.

Related errors


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