{"record":{"id":"170cc618ef3ae861","repo":"run-llama/llama_index","slug":"please-install-nltk-pip-install-nltk","errorCode":null,"errorMessage":"Please install nltk: `pip install nltk`","messagePattern":"Please install nltk: `pip install nltk`","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"llama-index-core/llama_index/core/indices/keyword_table/utils.py","lineNumber":33,"sourceCode":"    tokens = [t.strip().lower() for t in re.findall(r\"\\w+\", text_chunk)]\n    if filter_stopwords:\n        tokens = [t for t in tokens if t not in globals_helper.stopwords]\n\n    token_counts = Counter(tokens)\n    keywords = [keyword for keyword, count in token_counts.most_common(max_keywords)]\n    return set(keywords)\n\n\ndef rake_extract_keywords(\n    text_chunk: str,\n    max_keywords: Optional[int] = None,\n    expand_with_subtokens: bool = True,\n) -> Set[str]:\n    \"\"\"Extract keywords with RAKE.\"\"\"\n    try:\n        import nltk\n    except ImportError:\n        raise ImportError(\"Please install nltk: `pip install nltk`\")\n    try:\n        from rake_nltk import Rake\n    except ImportError:\n        raise ImportError(\"Please install rake_nltk: `pip install rake_nltk`\")\n\n    r = Rake(\n        sentence_tokenizer=nltk.tokenize.sent_tokenize,\n        word_tokenizer=nltk.tokenize.wordpunct_tokenize,\n    )\n    r.extract_keywords_from_text(text_chunk)\n    keywords = r.get_ranked_phrases()[:max_keywords]\n    if expand_with_subtokens:\n        return set(expand_tokens_with_subtokens(keywords))\n    else:\n        return set(keywords)\n\n\ndef extract_keywords_given_response(","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-core/llama_index/core/indices/keyword_table/utils.py#L15-L51","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["pip install nltk (and rake_nltk — both are required, see the sibling error) or install the extras bundle that includes them.","Verify the import early in your startup: python -c \"import nltk; import rake_nltk\" before ingestion runs.","If you cannot add deps, use SimpleKeywordTableIndex (regex-based extraction) which needs neither package."],"exampleFix":"# before\nindex = RAKEKeywordTableIndex.from_documents(docs)  # ImportError: install nltk\n\n# after\npip install nltk rake_nltk\nindex = RAKEKeywordTableIndex.from_documents(docs)","handlingStrategy":"validation","validationCode":"def rake_available() -> bool:\n    try:\n        import nltk  # noqa: F401\n        import rake_nltk  # noqa: F401\n        return True\n    except ImportError:\n        return False\n\nassert rake_available(), \"RAKE keyword extraction requires nltk and rake_nltk\"","typeGuard":null,"tryCatchPattern":"try:\n    index = RAKEKeywordTableIndex.from_documents(docs)\nexcept ImportError as e:\n    if \"nltk\" in str(e):\n        index = SimpleKeywordTableIndex.from_documents(docs)  # no extra deps\n    else:\n        raise","preventionTips":["Install optional NLP deps (nltk, rake_nltk) in every environment that builds RAKE indices.","Exercise the RAKE path in CI so missing deps fail at build time, not in production.","Prefer SimpleKeywordTableIndex when you want zero extra dependencies."],"tags":["llama-index","rake","nltk","optional-dependency","import"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}