{"record":{"id":"8592cf5a52d02a2e","repo":"run-llama/llama_index","slug":"treelib-package-is-missing-please-install-it-by","errorCode":null,"errorMessage":"`treelib` package is missing. Please install it by using `pip install treelib`.","messagePattern":"`treelib` package is missing\\. Please install it by using `pip install treelib`\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"llama-index-instrumentation/src/llama_index_instrumentation/span_handlers/simple.py","lineNumber":100,"sourceCode":"        if not children:\n            return acc\n        updated_spans = [s for s in spans if s not in children]\n\n        children_trees = [\n            self._build_tree_by_parent(\n                parent=c, acc=[c], spans=[s for s in updated_spans if c != s]\n            )\n            for c in children\n        ]\n\n        return acc + reduce(lambda x, y: x + y, children_trees)\n\n    def _get_trace_trees(self) -> List[\"Tree\"]:\n        \"\"\"Method for getting trace trees.\"\"\"\n        try:\n            from treelib import Tree\n        except ImportError as e:\n            raise ImportError(\n                \"`treelib` package is missing. Please install it by using \"\n                \"`pip install treelib`.\"\n            )\n\n        all_spans = self.completed_spans + self.dropped_spans\n        for s in all_spans:\n            if s.parent_id is None:\n                continue\n            if not any(ns.id_ == s.parent_id for ns in all_spans):\n                warnings.warn(f\"Parent with id {s.parent_id} missing from spans\")\n                s.parent_id += \"-MISSING\"\n                all_spans.append(SimpleSpan(id_=s.parent_id, parent_id=None))\n\n        parents = self._get_parents()\n        span_groups = []\n        for p in parents:\n            this_span_group = self._build_tree_by_parent(\n                parent=p, acc=[p], spans=[s for s in all_spans if s != p]","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/run-llama/llama_index/blob/afd0fef371831f9bda13e5af7167cf4e981278ab/llama-index-instrumentation/src/llama_index_instrumentation/span_handlers/simple.py#L82-L118","documentation":"Raised by llama-index-instrumentation's SimpleSpanHandler._get_trace_trees when the optional `treelib` dependency cannot be imported. The handler builds hierarchical trace trees from completed/dropped spans and uses treelib's Tree structure to render them; without the package installed the ImportError is re-raised with an install hint. Note the raise also discards the original ImportError context.","triggerScenarios":"Calling span-handler APIs that render traces (e.g. printing/exporting trace trees via SimpleSpanHandler) in an environment where `pip install treelib` was never run. The try/except ImportError around `from treelib import Tree` converts the missing module into this actionable ImportError.","commonSituations":"Installing llama-index-instrumentation without its visualization extras; running in a slim Docker/CI image where optional deps are pruned; upgrading environments and losing manually installed treelib.","solutions":["pip install treelib in the same environment/venv that runs the instrumentation code","Add treelib to your requirements.txt/pyproject optional extras if you rely on trace trees","If you don't need trace rendering, avoid the code path that calls _get_trace_trees"],"exampleFix":"# before\nhandler = SimpleSpanHandler()\n# ... later, rendering traces raises ImportError\n\n# after\npip install treelib\nhandler = SimpleSpanHandler()","handlingStrategy":"validation","validationCode":"def treelib_available() -> bool:\n    try:\n        import treelib  # noqa: F401\n        return True\n    except ImportError:\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    trees = handler._get_trace_trees()\nexcept ImportError as e:\n    if \"treelib\" in str(e):\n        logger.warning(\"treelib missing; trace rendering disabled\")\n    else:\n        raise","preventionTips":["Declare treelib in your project's dependencies when using trace-tree rendering","Include optional visualization deps in Docker images, not just the base package","Check import availability before calling span-tree rendering in shared tooling"],"tags":["missing-dependency","instrumentation","observability","python"],"backgroundTag":null,"analyzedSha":"afd0fef371831f9bda13e5af7167cf4e981278ab","analyzedAt":"2026-08-15T05:42:58.429Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}