Skip to content

Commit c9246ac

Browse files
committed
fix(langchain): sort similarity_search() results by distance + tighten test
- Sort vector_search results client-side by r.distance (ascending) to match the public contract promised in the docstring - Replace isinstance(results, list) with results == [] assertion to explicitly enforce the empty-vector guard contract
1 parent 7c4d4c0 commit c9246ac

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

langchain-coordinode/langchain_coordinode/graph.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,14 @@ def similarity_search(
219219
"""
220220
if not query_vector:
221221
return []
222-
results = self._client.vector_search(
223-
label=label,
224-
property=property,
225-
vector=query_vector,
226-
top_k=k,
222+
results = sorted(
223+
self._client.vector_search(
224+
label=label,
225+
property=property,
226+
vector=query_vector,
227+
top_k=k,
228+
),
229+
key=lambda r: r.distance,
227230
)
228231
return [{"id": r.node.id, "node": r.node.properties, "distance": r.distance} for r in results]
229232

tests/integration/adapters/test_langchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_similarity_search_returns_results(graph, unique_tag):
168168
def test_similarity_search_empty_vector_returns_empty(graph):
169169
"""similarity_search() with an empty vector list returns an empty list without error."""
170170
results = graph.similarity_search([], k=5)
171-
assert isinstance(results, list)
171+
assert results == []
172172

173173

174174
def test_schema_refreshes_after_add(graph, unique_tag):

0 commit comments

Comments
 (0)