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 @@ -102,6 +102,15 @@ def load_documents(self, documents: list[VectorStoreDocument]) -> None:
if not ids:
return

actual_vector_size = len(vectors[0])
if actual_vector_size != self.vector_size:
msg = (
f"Embedding dimension {actual_vector_size} does not match "
f"configured vector_size {self.vector_size} for index '{self.index_name}'. "
f"Please update vector_size in your vector_store configuration."
)
raise ValueError(msg)

flat_vector = np.concatenate(vectors).astype(np.float32)
flat_array = pa.array(flat_vector, type=pa.float32())
vector_column = pa.FixedSizeListArray.from_arrays(flat_array, self.vector_size)
Expand Down
23 changes: 23 additions & 0 deletions tests/integration/vector_stores/test_lancedb.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,29 @@ def test_user_defined_date_field_exploded(self):
finally:
shutil.rmtree(temp_dir)

def test_load_documents_vector_dimension_mismatch(self):
"""Test that load_documents raises ValueError when vector dimension does not match vector_size."""
temp_dir = tempfile.mkdtemp()
try:
vector_store = LanceDBVectorStore(
db_uri=temp_dir, index_name="dim_mismatch", vector_size=10
)
vector_store.connect()
vector_store.create_index()

documents = [
VectorStoreDocument(id="1", vector=[0.1, 0.2, 0.3, 0.4, 0.5]),
VectorStoreDocument(id="2", vector=[0.2, 0.3, 0.4, 0.5, 0.6]),
]

with pytest.raises(
ValueError,
match="Embedding dimension 5 does not match configured vector_size 10",
):
vector_store.load_documents(documents)
finally:
shutil.rmtree(temp_dir)

def test_vector_store_customization(self, sample_documents):
"""Test vector store customization with LanceDB."""
temp_dir = tempfile.mkdtemp()
Expand Down