Skip to content

Commit abc3d82

Browse files
tom-dyarThomas Dyar
authored andcommitted
fix: use column expression in order_by instead of string literal
The order_by(asc("distance")) call fails because SQLAlchemy requires a column expression object, not a string literal. This fix extracts the distance expression to a variable and references it directly in the order_by clause. Changes: - Extract distance_expr before the query - Use distance_expr in session.query() and order_by() - Remove unused asc import - Add regression test for similarity_search_with_score_by_vector
1 parent 4a7b002 commit abc3d82

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

langchain_iris/vectorstores.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from sqlalchemy import (
2222
Connection,
2323
and_,
24-
asc,
2524
VARCHAR,
2625
TEXT,
2726
Column,
@@ -519,21 +518,24 @@ def similarity_search_with_score_by_vector(
519518

520519
embedding = [float(v) for v in embedding]
521520

521+
# Build the distance expression for ordering
522+
distance_expr = (
523+
self.distance_strategy(embedding).label("distance")
524+
if self.native_vector
525+
else self.table.c.embedding.func(
526+
self.distance_strategy, embedding
527+
).label("distance")
528+
)
529+
522530
# Execute the query and fetch the results
523531
with Session(self._conn) as session:
524532
results: Sequence[Row] = (
525533
session.query(
526534
self.table,
527-
(
528-
self.distance_strategy(embedding).label("distance")
529-
if self.native_vector
530-
else self.table.c.embedding.func(
531-
self.distance_strategy, embedding
532-
).label("distance")
533-
),
535+
distance_expr,
534536
)
535537
.filter(filter_by)
536-
.order_by(asc("distance"))
538+
.order_by(distance_expr)
537539
.limit(k)
538540
.all()
539541
)

tests/test_vectorstores.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,36 @@ def test_irisvector_retriever_search_threshold_custom_normalization_fn(
288288
)
289289
output = retriever.invoke("foo")
290290
assert output == []
291+
292+
293+
def test_irisvector_similarity_search_with_score_by_vector(
294+
collection_name, connection_string
295+
) -> None:
296+
"""Regression test: order_by must use column expression, not string.
297+
298+
This test verifies the fix for the order_by(asc("distance")) bug where
299+
SQLAlchemy requires a column expression object, not a string literal.
300+
"""
301+
texts = ["foo", "bar", "baz"]
302+
metadatas = [{"page": str(i)} for i in range(len(texts))]
303+
docsearch = IRISVector.from_texts(
304+
texts=texts,
305+
collection_name=collection_name,
306+
embedding=FakeEmbeddingsWithAdaDimension(),
307+
metadatas=metadatas,
308+
connection_string=connection_string,
309+
pre_delete_collection=True,
310+
)
311+
312+
# Get the embedding for "foo" and call similarity_search_with_score_by_vector directly
313+
embedding = FakeEmbeddingsWithAdaDimension().embed_query("foo")
314+
output = docsearch.similarity_search_with_score_by_vector(embedding=embedding, k=3)
315+
316+
# Verify we get results without errors and they are ordered by distance
317+
assert len(output) == 3
318+
# First result should be "foo" with distance 0.0 (exact match)
319+
assert output[0][0].page_content == "foo"
320+
assert output[0][1] == 0.0
321+
# Results should be ordered by increasing distance
322+
distances = [score for _, score in output]
323+
assert distances == sorted(distances)

0 commit comments

Comments
 (0)