{"record":{"id":"a259ee9df8ef3a49","repo":"langchain-ai/deepagents","slug":"storebackend-must-be-used-inside-a-langgraph-graph","errorCode":null,"errorMessage":"StoreBackend must be used inside a LangGraph graph execution (e.g. via create_deep_agent), or initialized with an explicit store and namespace: StoreBackend(store=my_store, namespace=lambda _rt: ('filesystem',))","messagePattern":"StoreBackend must be used inside a LangGraph graph execution \\(e\\.g\\. via create_deep_agent\\), or initialized with an explicit store and namespace: StoreBackend\\(store=my_store, namespace=lambda _rt: \\('filesystem',\\)\\)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"libs/deepagents/deepagents/backends/store.py","lineNumber":139,"sourceCode":"\n    def _get_store(self) -> BaseStore:\n        \"\"\"Return the store instance.\n\n        Uses the store passed at init if available, otherwise falls back to\n        `get_store()` which reads from the LangGraph execution context.\n        \"\"\"\n        if self._store is not None:\n            return self._store\n        try:\n            return get_store()\n        except (RuntimeError, KeyError):\n            msg = (\n                \"StoreBackend must be used inside a LangGraph graph execution \"\n                \"(e.g. via create_deep_agent), or initialized with an explicit \"\n                \"store and namespace: StoreBackend(store=my_store, \"\n                \"namespace=lambda _rt: ('filesystem',))\"\n            )\n            raise RuntimeError(msg) from None\n\n    def _get_namespace(self) -> tuple[str, ...]:\n        \"\"\"Get the namespace for store operations.\n\n        Resolves the `Runtime` from the graph execution context and passes it to\n        the namespace factory. When called outside a graph (e.g. direct backend\n        use), the runtime is unavailable and `None` is passed instead, so\n        factories that ignore their argument still work. A factory that reads\n        the runtime (e.g. `lambda rt: (rt.server_info.user.identity, ...)`)\n        raises a clear `RuntimeError` in that case rather than an opaque\n        `AttributeError` on `None`.\n        \"\"\"\n        try:\n            runtime: Runtime[Any] | None = get_runtime()\n        except (RuntimeError, KeyError):\n            runtime = None\n        try:\n            namespace = self._namespace(cast(\"Runtime[Any]\", runtime))","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/langchain-ai/deepagents/blob/a1af029e6e73cb17c36bff823d227747b28e91e1/libs/deepagents/deepagents/backends/store.py#L121-L157","documentation":"StoreBackend resolves its LangGraph BaseStore lazily from the running graph's runtime context (`_get_store`). If the backend is used outside a LangGraph graph execution and no explicit `store` was provided at construction, there is nothing to talk to, so a RuntimeError is raised. The library requires either graph execution or explicit construction-time `store` (and optionally `namespace`).","triggerScenarios":"Instantiating `StoreBackend()` with no arguments and calling `ls`, `read`, `write`, `edit`, `glob`, or `grep` in a plain script/test; using the backend after the agent graph has finished; calling backend methods from a thread without graph context.","commonSituations":"Unit-testing the backend standalone; calling read/write on a StoreBackend returned by a completed `create_deep_agent` run outside the run context; forgetting to pass `store=` when reusing the backend in a background job.","solutions":["Construct the backend with an explicit store: `StoreBackend(store=my_store, namespace=('filesystem',))`","Call backend methods only inside a LangGraph graph execution (e.g. via `create_deep_agent` tools)","Pass an InMemoryStore or a real BaseStore instance in tests instead of relying on runtime context","If a namespace factory reads the Runtime, keep it inside the graph or make it Runtime-independent"],"exampleFix":"// before\nbackend = StoreBackend()\nbackend.read('/foo.txt')  # outside graph -> RuntimeError\n// after\nfrom langgraph.store.memory import InMemoryStore\nbackend = StoreBackend(store=InMemoryStore(), namespace=('filesystem',))\nbackend.read('/foo.txt')","handlingStrategy":"try-catch","validationCode":"def backend_usable(backend) -> bool:\n    return getattr(backend, '_store', None) is not None\n# construct with explicit store when outside a graph:\nfrom langgraph.store.memory import InMemoryStore\nbackend = StoreBackend(store=InMemoryStore(), namespace=('filesystem',))","typeGuard":null,"tryCatchPattern":"try:\n    content = backend.read('/file.txt')\nexcept RuntimeError as exc:\n    if 'must be used inside a LangGraph graph execution' in str(exc):\n        backend = StoreBackend(store=get_or_create_store(), namespace=('filesystem',))\n        content = backend.read('/file.txt')\n    else:\n        raise","preventionTips":["Always pass an explicit `store=` when instantiating StoreBackend outside create_deep_agent","Use InMemoryStore in unit tests instead of relying on runtime context","Never keep references to graph-scoped backends for use after the run ends","Route file operations through agent tools when inside a graph"],"tags":["runtime-context","store","langgraph"],"backgroundTag":"missing-runtime-context","analyzedSha":"a1af029e6e73cb17c36bff823d227747b28e91e1","analyzedAt":"2026-08-29T11:43:24.718Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}