SqliteCache: a comma in a node name breaks cache lookup with KeyError in match_cached_writes
Checked other resources
- This is a bug, not a usage question.
- I added a clear and descriptive title that summarizes this issue.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangGraph rather than my code.
- The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package).
- This is not related to the langchain-community package.
- I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.
Related Issues / PRs
None found. I searched the tracker for SqliteCache, cache namespace, match_cached_writes and CACHE_NS_WRITES. #6247 touches RedisCache but is about async client support, which is a different problem.
Reproduction Steps / Example Code (Python)
import os, tempfile
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import CachePolicy
from langgraph.cache.sqlite import SqliteCache
class State(TypedDict):
n: int
def node(state: State) -> State:
return {"n": state["n"] + 1}
builder = StateGraph(State)
builder.add_node("foo,bar", node, cache_policy=CachePolicy(ttl=120))
builder.add_edge(START, "foo,bar")
builder.add_edge("foo,bar", END)
graph = builder.compile(
cache=SqliteCache(path=os.path.join(tempfile.mkdtemp(), "cache.db"))
)
graph.invoke({"n": 0}) # populates the cache
graph.invoke({"n": 0}) # KeyErrorSwapping SqliteCache for InMemoryCache makes the same graph work, and the node is served from cache on the second call.
Error Message and Stack Trace (if applicable)
Traceback (most recent call last):
File "minimal.py", line 26, in <module>
graph.invoke({"n": 0}) # KeyError
File ".../langgraph/pregel/main.py", line 3913, in invoke
for chunk in self.stream(
File ".../langgraph/pregel/main.py", line 2965, in stream
for task in loop.match_cached_writes():
File ".../langgraph/pregel/_loop.py", line 1559, in match_cached_writes
task = cached[key]
~~~~~~^^^^^
KeyError: (('__pregel_ns_writes', '__main__.node', 'foo', 'bar'), 'd5ee48e566d7630668734697e48c78f6')Description
I am caching a node with CachePolicy and a SqliteCache backend. The node name happens to contain a comma. I expect the node name to have no bearing on whether the cache works. Instead the second invocation raises KeyError inside LangGraph's own loop, so the graph cannot run at all.
The cause is the namespace encoding in SqliteCache. A cache namespace is a tuple[str, ...], and the SQLite backend flattens it into a single TEXT column by joining on a comma:
libs/checkpoint-sqlite/langgraph/cache/sqlite/__init__.py:55builds the lookup parameter with",".join(ns_tuple)libs/checkpoint-sqlite/langgraph/cache/sqlite/__init__.py:91writes the row with",".join(key[0])libs/checkpoint-sqlite/langgraph/cache/sqlite/__init__.py:69reads it back withtuple(ns.split(","))
set() and clear() only ever encode, so they agree with each other and the SELECT matches the right row. The problem is confined to the decode on line 69: get() returns a dict keyed by the re-split namespace rather than by the key the caller passed in. When any namespace segment contains a comma, the re-split tuple has more elements than the original.
For a node named foo,bar, the namespace built at libs/langgraph/langgraph/pregel/_algo.py:673 is
('__pregel_ns_writes', '<module>.<qualname>', 'foo,bar') # 3 elements, what the caller holdsand what get() hands back is
('__pregel_ns_writes', '<module>.<qualname>', 'foo', 'bar') # 4 elements, after the round triplibs/langgraph/langgraph/pregel/_loop.py:1559 then looks up cached[key] with the original 3-element namespace and raises. The async path has the same line at libs/langgraph/langgraph/pregel/_loop.py:1814 and fails the same way with ainvoke.
Two other consequences of the same encoding, which I verified by calling SqliteCache directly rather than through a graph:
- An empty namespace
()is stored as""and comes back as('',), so the lookup misses. ('a', 'b')and('a,b',)both encode to the stringa,b, so they collide on the(ns, key)primary key. After writing to both, askingSqliteCachefor('a', 'b')returned the value that was written for('a,b',).InMemoryCachekeeps them separate.
The practical effect is that swapping the cache backend changes behaviour, which defeats the point of them sharing BaseCache.
One note I want to flag as unverified: RedisCache uses the same flatten-and-split approach with a colon (":".join(ns) at libs/checkpoint/langgraph/cache/redis/__init__.py:33, remaining.split(":") at :45), so I would expect the same class of failure for a namespace segment containing a colon. I did not run that one because it needs a Redis server, so please treat it as a code reading rather than a reproduction.
As for a direction for the fix, the SQLite SELECT already matches the correct row, so get() could key its result with the caller's original FullKey instead of reconstructing the namespace from the stored string. That would leave the on-disk format alone. Making the stored namespace unambiguous, by encoding it as JSON or by escaping the separator, would additionally fix the collision in point 2, at the cost of a format change. I am happy to open a PR for whichever you prefer, once someone has picked a direction and assigned the issue.
System Info
System Information
------------------
> OS: Linux
> OS Version: #29~24.04.2-Ubuntu SMP PREEMPT_DYNAMIC Wed Aug 12 17:25:56 UTC 2
> Python Version: 3.12.3 (main, Jun 19 2026, 12:46:00) [GCC 13.3.0]
Package Information
-------------------
> langchain_core: 1.6.0
> langsmith: 0.11.1
> langchain_protocol: 0.0.18
> langgraph_sdk: 0.4.3
langgraph: 1.2.11
langgraph-checkpoint: 4.2.0
langgraph-checkpoint-sqlite: 3.1.1
Other Dependencies
------------------
> anyio: 4.14.2
> httpx: 0.28.1
> orjson: 3.12.0
> pydantic: 2.13.4Source: langchain-ai/langgraph