#9097·langgraph

SqliteStore vector search with distance_type="l2" returns the farthest match first

Author: aalquwayfiliCreated Sep 27, 2026Updated Sep 29, 2026
Labelsexternal

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.

Reproduction Steps / Example Code (Python)

python
import json

from langgraph.store.sqlite import SqliteStore

VECTORS = {"a": [1.0, 0.0, 0.0], "b": [0.9, 0.1, 0.0], "c": [0.0, 0.0, 1.0]}


def embed(texts: list[str]) -> list[list[float]]:
    # stored values are embedded as JSON, queries arrive as plain text
    return [VECTORS[json.loads(t)["text"] if t.startswith("{") else t] for t in texts]


index = {"dims": 3, "embed": embed, "distance_type": "l2"}
with SqliteStore.from_conn_string(":memory:", index=index) as store:
    store.setup()
    for key in "abc":
        store.put(("docs",), key, {"text": key})
    for r in store.search(("docs",), query="a"):
        print(r.key, round(r.score, 3))

Error Message and Stack Trace (if applicable)

bash
c 1.414
b 0.141
a 0.0

With "distance_type": "cosine" the same script prints a, b, c.

Description

With distance_type="l2", SqliteStore uses the raw L2 distance as the score (vec_distance_L2(...) in BaseSqliteStore) and sorts by score descending, so the farthest match comes first. PostgresStore returns the negated distance for l2 (-scored.neg_score), so it ranks closest first.

Related: the inner_product branch uses -1 * vec_distance_L1(...), which is Manhattan distance rather than an inner product.

The SQLite tests note that SQLite only supports cosine, so the fix could either negate the l2 distance (and fix or drop inner_product), or reject non-cosine distance types in the index config. I have the l2 fix with a test ready. Could you assign this issue to me? I'll open a PR for whichever option you prefer.

System Info

bash
System Information
------------------
> OS:  Linux
> Python Version:  3.11.14

Package Information
-------------------
> langchain_core: 1.5.2
> langgraph-checkpoint: 4.2.0
> langgraph-checkpoint-sqlite: 3.1.1
> sqlite-vec: 0.1.9

Reproduced on langgraph main at 7daa3ab.