langchain-ai/deepagents · error · RuntimeError
StoreBackend must be used inside a LangGraph graph execution
Error message
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',)) What it means
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`).
Source
Thrown at libs/deepagents/deepagents/backends/store.py:139
def _get_store(self) -> BaseStore:
"""Return the store instance.
Uses the store passed at init if available, otherwise falls back to
`get_store()` which reads from the LangGraph execution context.
"""
if self._store is not None:
return self._store
try:
return get_store()
except (RuntimeError, KeyError):
msg = (
"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',))"
)
raise RuntimeError(msg) from None
def _get_namespace(self) -> tuple[str, ...]:
"""Get the namespace for store operations.
Resolves the `Runtime` from the graph execution context and passes it to
the namespace factory. When called outside a graph (e.g. direct backend
use), the runtime is unavailable and `None` is passed instead, so
factories that ignore their argument still work. A factory that reads
the runtime (e.g. `lambda rt: (rt.server_info.user.identity, ...)`)
raises a clear `RuntimeError` in that case rather than an opaque
`AttributeError` on `None`.
"""
try:
runtime: Runtime[Any] | None = get_runtime()
except (RuntimeError, KeyError):
runtime = None
try:
namespace = self._namespace(cast("Runtime[Any]", runtime))View on GitHub (pinned to a1af029e6e)
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
Example fix
// before
backend = StoreBackend()
backend.read('/foo.txt') # outside graph -> RuntimeError
// after
from langgraph.store.memory import InMemoryStore
backend = StoreBackend(store=InMemoryStore(), namespace=('filesystem',))
backend.read('/foo.txt') Defensive patterns
Strategy: try-catch
Validate before calling
def backend_usable(backend) -> bool:
return getattr(backend, '_store', None) is not None
# construct with explicit store when outside a graph:
from langgraph.store.memory import InMemoryStore
backend = StoreBackend(store=InMemoryStore(), namespace=('filesystem',)) Try / catch
try:
content = backend.read('/file.txt')
except RuntimeError as exc:
if 'must be used inside a LangGraph graph execution' in str(exc):
backend = StoreBackend(store=get_or_create_store(), namespace=('filesystem',))
content = backend.read('/file.txt')
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The namespace factory tried to read the Runtime, but it is u
- Server process exited with code {self._process.returncode}
- Server graph '{graph_name}' failed readiness check (status:
- thread_id is required in config.configurable
- Pending graph work remained on thread {thread_id} after clea
AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29).
Data as JSON: /api/errors/a259ee9df8ef3a49.
Report an issue: GitHub.