{"record":{"id":"6cd14023ed243c70","repo":"headroomlabs-ai/headroom","slug":"openai-package-not-installed-install-with-pip-in","errorCode":null,"errorMessage":"openai package not installed. Install with: pip install openai","messagePattern":"openai package not installed\\. Install with: pip install openai","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"headroom/memory/backends/direct_mem0.py","lineNumber":177,"sourceCode":"        self._initialized = False\n\n        # Background task tracking\n        self._background_tasks: dict[str, asyncio.Task] = {}\n        self._task_results: dict[str, dict[str, Any]] = {}\n        self._close_lock = asyncio.Lock()\n\n    async def _ensure_initialized(self) -> None:\n        \"\"\"Ensure all clients are initialized.\"\"\"\n        if self._initialized:\n            return\n\n        # Initialize embedder (OpenAI)\n        try:\n            from openai import OpenAI\n\n            self._openai_client = OpenAI()\n        except ImportError:\n            raise ImportError(\n                \"openai package not installed. Install with: pip install openai\"\n            ) from None\n\n        # Initialize Qdrant client for direct writes\n        try:\n            from qdrant_client import QdrantClient\n\n            client_kwargs = qdrant_env.build_qdrant_client_kwargs(\n                url=self._config.qdrant_url,\n                host=self._config.qdrant_host,\n                port=self._config.qdrant_port,\n                api_key=self._config.qdrant_api_key,\n                https=self._config.qdrant_https,\n                prefer_grpc=self._config.qdrant_prefer_grpc,\n                grpc_port=self._config.qdrant_grpc_port,\n            )\n            self._qdrant_client = QdrantClient(**client_kwargs)\n        except ImportError:","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/headroomlabs-ai/headroom/blob/322425c43bffde1ed0b64fecf3cf5951565dd82b/headroom/memory/backends/direct_mem0.py#L159-L195","documentation":"Raised by DirectMem0Adapter._ensure_initialized() when the 'openai' Python package cannot be imported. The direct-mem0 backend embeds memories with OpenAI embeddings, so the openai SDK is a hard runtime dependency for this backend. Initialization is lazy: the ImportError surfaces on the first memory operation (save/search/update), not at construction time.","triggerScenarios":"Creating DirectMem0Adapter (or MemoryEasy with backend 'qdrant-neo4j') and calling any async memory operation such as save(), search(), or update_memory(); the first call runs _ensure_initialized(), which does 'from openai import OpenAI' and raises ImportError on failure.","commonSituations":"Installing headroom-ai without the [memory-stack] extra; running in a venv/CI image that only pinned core dependencies; openai installed in a different interpreter than the one running the app.","solutions":["pip install 'headroom-ai[memory-stack]' (installs openai, qdrant-client, and mem0 together)","Or install the single missing package: pip install openai","Or switch to a backend with no extra deps (e.g. the sqlite/local backend)","Verify the right interpreter: python -c 'import openai' in the same venv the app runs in"],"exampleFix":"# before\npip install headroom-ai\nadapter = DirectMem0Adapter(cfg)\nawait adapter.save('note', user_id='u1')  # ImportError: openai package not installed\n\n# after\npip install 'headroom-ai[memory-stack]'\nadapter = DirectMem0Adapter(cfg)\nawait adapter.save('note', user_id='u1')","handlingStrategy":"validation","validationCode":"import importlib.util\n\ndef openai_available() -> bool:\n    return importlib.util.find_spec('openai') is not None\n\n# before constructing the backend\nif not openai_available():\n    raise SystemExit('Install extras first: pip install \"headroom-ai[memory-stack]\"')","typeGuard":null,"tryCatchPattern":"try:\n    await adapter.save(content, user_id='u1')\nexcept ImportError as e:\n    if 'openai package not installed' in str(e):\n        logger.error('memory backend missing deps; run: pip install \"headroom-ai[memory-stack]\"')\n    raise","preventionTips":["Pin 'headroom-ai[memory-stack]' in requirements from day one if you use DirectMem0Adapter","Run a startup dependency probe (importlib.util.find_spec for openai/qdrant-client/mem0) before serving traffic","Fail fast at app boot with a clear message instead of letting lazy init throw mid-request"],"tags":["python","import","dependency","openai","mem0","memory-backend"],"backgroundTag":null,"analyzedSha":"322425c43bffde1ed0b64fecf3cf5951565dd82b","analyzedAt":"2026-08-15T01:03:05.481Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}