{"record":{"id":"e71d7f32c0af036d","repo":"headroomlabs-ai/headroom","slug":"tree-sitter-is-not-installed-install-with-pip-in","errorCode":null,"errorMessage":"tree-sitter is not installed. Install with: pip install headroom-ai[code]\\nThis adds ~50MB for tree-sitter grammars.","messagePattern":"tree-sitter is not installed\\. Install with: pip install headroom-ai\\[code\\]\\\\nThis adds ~50MB for tree-sitter grammars\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"headroom/transforms/code_compressor.py","lineNumber":142,"sourceCode":"    contract with negligible extra memory.\n\n    Args:\n        language: Language name (e.g., 'python', 'javascript').\n\n    Returns:\n        Configured ``tree_sitter.Parser`` bound to the current thread.\n\n    Raises:\n        ImportError: If tree-sitter is not installed.\n        ValueError: If language is not supported.\n    \"\"\"\n    if language in _UNSAFE_TREE_SITTER_LANGUAGES:\n        raise ValueError(f\"Language '{language}' is quarantined for code-aware compression.\")\n    # NOTE: guard on importability (not _check_tree_sitter_available), because\n    # _check_tree_sitter_available now performs a real end-to-end parse via\n    # _get_parser; guarding on it here would recurse.\n    if not _tree_sitter_importable():\n        raise ImportError(\n            \"tree-sitter is not installed. Install with: pip install headroom-ai[code]\\n\"\n            \"This adds ~50MB for tree-sitter grammars.\"\n        )\n\n    parsers: dict[str, Any] | None = getattr(_tree_sitter_local, \"parsers\", None)\n    if parsers is None:\n        parsers = {}\n        _tree_sitter_local.parsers = parsers\n\n    if language not in parsers:\n        try:\n            from tree_sitter import Parser\n            from tree_sitter_language_pack import get_language\n\n            parser = Parser()\n            # `language` is a validated runtime str; get_language types its arg\n            # as a Literal of supported names, which a dynamic str can't satisfy.\n            parser.language = get_language(language)  # type: ignore[arg-type]","sourceCodeStart":124,"sourceCodeEnd":160,"githubUrl":"https://github.com/headroomlabs-ai/headroom/blob/322425c43bffde1ed0b64fecf3cf5951565dd82b/headroom/transforms/code_compressor.py#L124-L160","documentation":"Raised by _get_parser when _tree_sitter_importable() returns False, i.e. the optional tree-sitter dependency (plus its grammar wheels) is not installed in the current environment. Headroom keeps tree-sitter behind the headroom-ai[code] extra because the grammars add ~50MB, so the import is lazy and this ImportError is the explicit failure when the extra is missing.","triggerScenarios":"Calling _get_parser(any_language) — or any higher-level code-aware compression API that reaches it — in an environment where 'pip install headroom-ai[code]' (tree-sitter + language packs) was never run, or the venv changed.","commonSituations":"Installing only the base headroom-ai package and then enabling code-aware compression; a CI image or slim Docker container that strips optional deps; upgrading headroom into a fresh virtualenv without re-adding extras.","solutions":["pip install headroom-ai[code] (or pip install \"headroom-ai[code]@your-version\") to add tree-sitter and its grammars.","Verify with: python -c \"import tree_sitter, tree_sitter_language_pack\" before enabling code-aware compression.","If the ~50MB is unacceptable, disable code-aware compression so the parser path is never entered."],"exampleFix":"# before: ImportError from _get_parser\nsession.compress(code, mode=\"code_aware\")\n\n# after\ntry:\n    from headroom.transforms.code_compressor import is_tree_sitter_available  # guarded probe\nexcept ImportError:\n    pass\n# shell: pip install headroom-ai[code]","handlingStrategy":"type-guard","validationCode":"from headroom.transforms.code_compressor import is_tree_sitter_available\nif not is_tree_sitter_available():\n    disable_code_aware_compression()","typeGuard":"def treesitter_ready() -> bool:\n    try:\n        import tree_sitter  # noqa: F401\n        import tree_sitter_language_pack  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    parser = _get_parser(language)\nexcept ImportError as e:\n    if \"tree-sitter is not installed\" in str(e):\n        logger.error(\"Install headroom-ai[code] to enable code-aware compression\")\n        raise\n    raise","preventionTips":["Declare headroom-ai[code] in requirements whenever code-aware compression is enabled.","Probe is_tree_sitter_available() at startup, not per-request.","Pin the extra in Docker/CI images so environments are reproducible."],"tags":["dependency","import-error","tree-sitter","installation"],"backgroundTag":null,"analyzedSha":"322425c43bffde1ed0b64fecf3cf5951565dd82b","analyzedAt":"2026-08-15T01:03:05.481Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}