microsoft/graphrag · error · ValueError

Container not initialized.

Error message

Container not initialized.

What it means

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.

Source

Thrown at packages/graphrag-storage/graphrag_storage/azure_cosmos_storage.py:187

        except CosmosResourceNotFoundError:
            return None
        except Exception:
            logger.exception("Error reading item %s", namespaced)
            return None
        else:
            if as_bytes:
                return result.encode(encoding or self._encoding)
            return result

    async def set(self, key: str, value: Any, encoding: str | None = None) -> None:
        """Store *value* under *key*.

        *value* should be a JSON string. It is parsed and stored under the
        ``body`` field so that Cosmos indexes it as structured data.
        """
        if not self._container_client:
            msg = "Container not initialized."
            raise ValueError(msg)
        namespaced = self._namespaced_key(key)
        try:
            # Parse JSON strings so the body is queryable; store others as-is.
            if isinstance(value, str):
                try:
                    parsed = json.loads(value)
                except (json.JSONDecodeError, ValueError):
                    parsed = value
            elif isinstance(value, bytes):
                parsed = value.decode(encoding or self._encoding)
            else:
                parsed = value
            cosmosdb_item = {"id": namespaced, "body": parsed}
            self._container_client.upsert_item(body=cosmosdb_item)
        except Exception:
            logger.exception("Error writing item %s", namespaced)

    async def has(self, key: str) -> bool:

View on GitHub (pinned to f40e9a26ce)

Solutions

  1. Await the storage's initialize/connect call before any set/get operations
  2. Check that container_name and database_name are valid; a failed container creation leaves the client unset
  3. If reusing a long-lived instance, re-initialize after close()

Example fix

# before
await store.set("k", "v")
# after
await store.initialize()
await store.set("k", "v")
Defensive patterns

Strategy: validation

Validate before calling

if store._container_client is None:
    await store.initialize()

Try / catch

try:
    await store.set(k, v)
except ValueError as e:
    if "not initialized" in str(e):
        await store.initialize()
        await store.set(k, v)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Using AzureCosmosStorage standalone instead of via the pipeline (which wires initialization); exceptions during container creation swallowed earlier; accessing a closed store in tests.

Related errors


AI-assisted analysis of microsoft/graphrag@f40e9a26ce (2026-08-27). Data as JSON: /api/errors/53b8d47b9c12c49e. Report an issue: GitHub.