{"record":{"id":"53b8d47b9c12c49e","repo":"microsoft/graphrag","slug":"container-not-initialized","errorCode":null,"errorMessage":"Container not initialized.","messagePattern":"Container not initialized\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/graphrag-storage/graphrag_storage/azure_cosmos_storage.py","lineNumber":187,"sourceCode":"        except CosmosResourceNotFoundError:\n            return None\n        except Exception:\n            logger.exception(\"Error reading item %s\", namespaced)\n            return None\n        else:\n            if as_bytes:\n                return result.encode(encoding or self._encoding)\n            return result\n\n    async def set(self, key: str, value: Any, encoding: str | None = None) -> None:\n        \"\"\"Store *value* under *key*.\n\n        *value* should be a JSON string. It is parsed and stored under the\n        ``body`` field so that Cosmos indexes it as structured data.\n        \"\"\"\n        if not self._container_client:\n            msg = \"Container not initialized.\"\n            raise ValueError(msg)\n        namespaced = self._namespaced_key(key)\n        try:\n            # Parse JSON strings so the body is queryable; store others as-is.\n            if isinstance(value, str):\n                try:\n                    parsed = json.loads(value)\n                except (json.JSONDecodeError, ValueError):\n                    parsed = value\n            elif isinstance(value, bytes):\n                parsed = value.decode(encoding or self._encoding)\n            else:\n                parsed = value\n            cosmosdb_item = {\"id\": namespaced, \"body\": parsed}\n            self._container_client.upsert_item(body=cosmosdb_item)\n        except Exception:\n            logger.exception(\"Error writing item %s\", namespaced)\n\n    async def has(self, key: str) -> bool:","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/microsoft/graphrag/blob/f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704/packages/graphrag-storage/graphrag_storage/azure_cosmos_storage.py#L169-L205","documentation":"AzureCosmosStorage.set() writes through an internal _container_client that is only created during the storage's connect/setup phase. Calling set() before (or after a failed) initialization leaves _container_client None, so the method refuses to operate on an uninitialized client.","triggerScenarios":"Calling await store.set(...) before calling the storage's initialize/connect method, or reusing a storage instance after it has been closed/failed to connect.","commonSituations":"Using AzureCosmosStorage standalone instead of via the pipeline (which wires initialization); exceptions during container creation swallowed earlier; accessing a closed store in tests.","solutions":["Await the storage's initialize/connect call before any set/get operations","Check that container_name and database_name are valid; a failed container creation leaves the client unset","If reusing a long-lived instance, re-initialize after close()"],"exampleFix":"# before\nawait store.set(\"k\", \"v\")\n# after\nawait store.initialize()\nawait store.set(\"k\", \"v\")","handlingStrategy":"validation","validationCode":"if store._container_client is None:\n    await store.initialize()","typeGuard":null,"tryCatchPattern":"try:\n    await store.set(k, v)\nexcept ValueError as e:\n    if \"not initialized\" in str(e):\n        await store.initialize()\n        await store.set(k, v)\n    else:\n        raise","preventionTips":["Always pair construction with initialize() in a context manager","Don't reuse stores after close()"],"tags":["cosmosdb","lifecycle","initialization"],"backgroundTag":"uninitialized-client","analyzedSha":"f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704","analyzedAt":"2026-08-27T11:16:29.677Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}