Skip to content

Commit d5022f5

Browse files
ehz0ahktz03
andauthored
fix(langchain): honor nested filters and index field selectors (#6404)
* fix(langchain): match nested JSON store filters (cherry picked from commit f6471b8) Signed-off-by: Hao Zhe <[email protected]> * fix(langchain): index LangGraph store field selectors (cherry picked from commit 3c7bcae) Signed-off-by: Hao Zhe <[email protected]> * test(langgraph): cover filtered recall of selected fields Exercise selected index fields and nested filters together through sync, async and batch search. Check caller values remain unchanged and document projection refresh behavior in both guides. Signed-off-by: Hao Zhe <[email protected]> * docs(langchain): clarify reserved nested filter operators Signed-off-by: Hao Zhe <[email protected]> --------- Signed-off-by: Hao Zhe <[email protected]> Co-authored-by: ktz03 <[email protected]>
1 parent 0d54685 commit d5022f5

4 files changed

Lines changed: 157 additions & 7 deletions

File tree

‎docs/en/agent-integrations/07-langchain-langgraph.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,11 @@ errors propagate, and `put()` does not overwrite a record after a failed read.
271271
`list_namespaces(prefix=("users", "*"))` matches one namespace label per `*`, including
272272
when `root_uri` uses a peer home alias such as `viking://~/peers/support/memories/langgraph_store`.
273273

274+
`search()` accepts nested JSON filters such as `filter={"metadata": {"color": "blue"}}`.
275+
Filter keys `eq`, `ne`, `gt`, `gte`, `lt`, `lte` and `in` are reserved operator aliases at every nesting level.
276+
Use `index=["items[*].text"]` to index selected array fields while retaining the complete
277+
stored value. Existing index projections are updated when the item is written again.
278+
274279
### LangGraph middleware
275280

276281
```python

‎docs/zh/agent-integrations/07-langchain-langgraph.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ store.put(("users", "ada"), "preferences", None) # 值为 None 表示删除该
258258
`list_namespaces(prefix=("users", "*"))` 中的每个 `*` 匹配一层命名空间,
259259
也适用于 `viking://~/peers/support/memories/langgraph_store` 这样的 peer 主目录别名。
260260

261+
`search()` 支持 `filter={"metadata": {"color": "blue"}}` 这样的嵌套 JSON 筛选。
262+
筛选中的 `eq`、`ne`、`gt`、`gte`、`lt`、`lte` 和 `in` 在所有嵌套层级均为保留的运算符别名。
263+
使用 `index=["items[*].text"]` 可以只索引数组中的指定字段,同时保留完整的存储值。
264+
已有条目的索引会在再次写入该条目时更新。
265+
261266
### LangGraph middleware
262267

263268
```python

‎integrations/frameworks/langchain/src/langchain_openviking/store.py‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
PutOp,
3636
SearchItem,
3737
SearchOp,
38+
get_text_at_path,
3839
)
3940
else:
4041
try:
@@ -47,6 +48,7 @@
4748
PutOp,
4849
SearchItem,
4950
SearchOp,
51+
get_text_at_path,
5052
)
5153
except ImportError: # pragma: no cover - exercised by optional import path
5254
_LANGGRAPH_IMPORT_ERROR = missing_dependency("langgraph", "langgraph")
@@ -559,7 +561,11 @@ def _extract_uris(value: Any) -> list[str]:
559561

560562
def _project_value(value: dict[str, Any], index: bool | list[str] | None) -> dict[str, Any]:
561563
if isinstance(index, list):
562-
return {field: _nested_value(value, field) for field in index}
564+
projected = {field: _nested_value(value, field) for field in index}
565+
for field in index:
566+
if projected[field] is None:
567+
projected[field] = get_text_at_path(value, field) or None
568+
return projected
563569
return value
564570

565571

@@ -577,15 +583,32 @@ def _matches_filter(value: dict[str, Any], filter: dict[str, Any] | None) -> boo
577583
return True
578584
for path, expected in filter.items():
579585
actual = _nested_value(value, path)
580-
if isinstance(expected, dict):
581-
for op, target in expected.items():
582-
if not _compare(actual, op, target):
583-
return False
584-
elif actual != expected:
586+
if not _matches_value(actual, expected):
585587
return False
586588
return True
587589

588590

591+
def _matches_value(actual: Any, expected: Any) -> bool:
592+
if isinstance(expected, dict):
593+
if any(
594+
key.startswith("$") or key in {"eq", "ne", "gt", "gte", "lt", "lte", "in"}
595+
for key in expected
596+
):
597+
return all(_compare(actual, op, target) for op, target in expected.items())
598+
return isinstance(actual, dict) and all(
599+
_matches_value(actual.get(key), target) for key, target in expected.items()
600+
)
601+
if isinstance(expected, (list, tuple)):
602+
return (
603+
isinstance(actual, (list, tuple))
604+
and len(actual) == len(expected)
605+
and all(
606+
_matches_value(left, right) for left, right in zip(actual, expected, strict=False)
607+
)
608+
)
609+
return actual == expected
610+
611+
589612
def _compare(actual: Any, op: str, target: Any) -> bool:
590613
if op in {"$eq", "eq"}:
591614
return actual == target

‎integrations/frameworks/langchain/tests/unit/test_langchain_integration.py‎

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
convert_to_openai_messages,
2525
)
2626
from langchain_core.runnables import RunnableLambda
27-
from langgraph.store.base import InvalidNamespaceError, PutOp
27+
from langgraph.store.base import InvalidNamespaceError, PutOp, SearchOp
2828
from openviking_sdk.errors import (
2929
InvalidArgumentError,
3030
NotFoundError,
@@ -1894,6 +1894,58 @@ def rm(self, uri, recursive=False):
18941894
assert store.search(namespace, query="azure") == []
18951895

18961896

1897+
@pytest.mark.asyncio
1898+
@pytest.mark.parametrize("query", [None, "doc"])
1899+
@pytest.mark.parametrize(
1900+
"filter, expected",
1901+
[
1902+
({"metadata": {"color": "blue"}}, ["alpha"]),
1903+
({"metadata": {"nested": {"owner": "ada"}}}, ["alpha"]),
1904+
({"metadata": {"nested": {"rank": {"$gte": 2}}}}, ["alpha"]),
1905+
({"metadata": {"detail.region": "eu"}}, ["alpha"]),
1906+
({"tags": [{"role": "reviewer"}, "memory"]}, ["alpha"]),
1907+
({"tags": [{"role": "reviewer"}]}, []),
1908+
({"metadata": {}}, ["alpha", "beta"]),
1909+
({"metadata": {"color": "black"}}, []),
1910+
({"metadata.nested.rank": {"gte": 2}}, ["alpha"]),
1911+
({"metadata.nested": {"$eq": {"rank": 3, "owner": "ada"}}}, ["alpha"]),
1912+
({"metadata": {"eq": "blue", "label": "blue"}}, ["scalar"]),
1913+
({"metadata": {"$unknown": "blue"}}, ["scalar"]),
1914+
({"tags": ["reviewer", "memory"]}, ["scalar"]),
1915+
({"kind": "doc"}, ["alpha", "beta", "scalar"]),
1916+
],
1917+
)
1918+
async def test_langgraph_store_structured_filters(filter, expected, query):
1919+
store = OpenVikingStore(client=InMemoryOpenVikingClient())
1920+
for key, value in {
1921+
"alpha": {
1922+
"kind": "doc",
1923+
"metadata": {
1924+
"color": "blue",
1925+
"nested": {"rank": 3, "owner": "ada"},
1926+
"detail.region": "eu",
1927+
},
1928+
"tags": [{"role": "reviewer", "active": True}, "memory"],
1929+
},
1930+
"beta": {
1931+
"kind": "doc",
1932+
"metadata": {"color": "red", "nested": {"rank": 1, "owner": "bob"}},
1933+
"tags": [{"role": "reader", "active": True}, "memory"],
1934+
},
1935+
"scalar": {"kind": "doc", "metadata": "blue", "tags": ["reviewer", "memory"]},
1936+
}.items():
1937+
store.put(("users", "ada"), key, value)
1938+
1939+
op = SearchOp(("users",), filter=filter, query=query)
1940+
results = [
1941+
store.search(("users",), filter=filter, query=query),
1942+
await store.asearch(("users",), filter=filter, query=query),
1943+
store.batch([op])[0],
1944+
(await store.abatch([op]))[0],
1945+
]
1946+
assert all(sorted(item.key for item in items) == expected for items in results)
1947+
1948+
18971949
def test_langgraph_store_semantic_search_keeps_peer_id_out_of_retrieval():
18981950
client = InMemoryOpenVikingClient()
18991951
store = OpenVikingStore(client=client, actor_peer_id="peer-store")
@@ -1911,6 +1963,71 @@ def test_langgraph_store_rejects_ttl_writes():
19111963
store.put(("users", "ada"), "temporary", {"note": "expires"}, ttl=60)
19121964

19131965

1966+
@pytest.mark.parametrize("use_default", [False, True])
1967+
@pytest.mark.parametrize(
1968+
("field", "query", "expected_match"),
1969+
[
1970+
("$", "solstice", True),
1971+
("", "solstice", True),
1972+
("items[0].text", "pulsar", True),
1973+
("items[-1].text", "galaxy", True),
1974+
("items[*].text", "galaxy", True),
1975+
("nested[*].parts[*].text", "aurora", True),
1976+
("metadata.*", "quasar", True),
1977+
("{text,metadata.title}", "nebula", True),
1978+
("text", "nebula", True),
1979+
("metadata.title", "quasar", True),
1980+
("absent", "solstice", False),
1981+
("literal[0]", "nova", True),
1982+
],
1983+
)
1984+
@pytest.mark.asyncio
1985+
async def test_langgraph_store_indexes_selected_field_paths(
1986+
field, query, expected_match, use_default
1987+
):
1988+
value = {
1989+
"text": "nebula",
1990+
"metadata": {"title": "quasar"},
1991+
"items": [{"text": "pulsar"}, {"text": "galaxy"}],
1992+
"nested": [{"parts": [{"text": "aurora"}]}],
1993+
"literal[0]": "nova",
1994+
"ignored": "solstice",
1995+
}
1996+
original = deepcopy(value)
1997+
for mode in ("put", "aput", "batch", "abatch"):
1998+
client = InMemoryOpenVikingClient()
1999+
store = OpenVikingStore(client=client, index=[field] if use_default else None)
2000+
index = None if use_default else [field]
2001+
if mode == "put":
2002+
store.put(("documents",), "note", value, index=index)
2003+
elif mode == "aput":
2004+
await store.aput(("documents",), "note", value, index=index)
2005+
elif mode == "batch":
2006+
store.batch([PutOp(("documents",), "note", value, index=index)])
2007+
else:
2008+
await store.abatch([PutOp(("documents",), "note", value, index=index)])
2009+
2010+
assert store.get(("documents",), "note").value == value
2011+
assert value == original
2012+
expected_keys = ["note"] if expected_match else []
2013+
assert [item.key for item in store.search(("documents",), query=query)] == expected_keys
2014+
filter = {"metadata": {"title": "quasar"}}
2015+
op = SearchOp(("documents",), query=query, filter=filter)
2016+
results = [
2017+
store.search(("documents",), query=query, filter=filter),
2018+
await store.asearch(("documents",), query=query, filter=filter),
2019+
store.batch([op])[0],
2020+
(await store.abatch([op]))[0],
2021+
]
2022+
assert all([item.key for item in items] == expected_keys for items in results)
2023+
assert (
2024+
store.search(("documents",), query=query, filter={"metadata": {"title": "absent"}})
2025+
== []
2026+
)
2027+
if field not in {"$", ""}:
2028+
assert store.search(("documents",), query="solstice") == []
2029+
2030+
19142031
def test_langgraph_store_batch_rejects_ttl_writes():
19152032
store = OpenVikingStore(client=InMemoryOpenVikingClient())
19162033

0 commit comments

Comments
 (0)