Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/graphrag-vectors/graphrag_vectors/lancedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def load_documents(self, documents: list[VectorStoreDocument]) -> None:
if document.vector is None:
continue

actual_vector_size = len(document.vector)
if actual_vector_size != self.vector_size:
msg = (
f"Vector for document '{document.id}' has dimension "
f"{actual_vector_size}, but index '{self.index_name}' is "
f"configured with vector_size {self.vector_size}."
)
raise ValueError(msg)

ids.append(str(document.id))
vectors.append(np.array(document.vector, dtype=np.float32))
create_dates.append(document.create_date)
Expand Down
26 changes: 26 additions & 0 deletions tests/integration/vector_stores/test_lancedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,32 @@ def test_load_documents(self, store_with_fields, sample_documents_with_metadata)
store.load_documents(sample_documents_with_metadata)
assert store.count() == 3

def test_load_documents_rejects_mismatched_vector_size(self):
"""Test loading a batch with a wrong-sized vector raises a clear error."""
temp_dir = tempfile.mkdtemp()
try:
vector_store = LanceDBVectorStore(
db_uri=temp_dir, index_name="dim_mismatch", vector_size=5
)
vector_store.connect()
vector_store.create_index()

documents = [
VectorStoreDocument(id="good", vector=[0.1, 0.2, 0.3, 0.4, 0.5]),
VectorStoreDocument(id="bad", vector=[0.1, 0.2, 0.3]),
]

with pytest.raises(
ValueError,
match=(
"Vector for document 'bad' has dimension 3, "
"but index 'dim_mismatch' is configured with vector_size 5"
),
):
vector_store.load_documents(documents)
finally:
shutil.rmtree(temp_dir)

def test_search_by_id(self, store_with_fields, sample_documents_with_metadata):
"""Test searching for a document by id returns all fields."""
store = store_with_fields
Expand Down