diff --git a/python/python/lancedb/table.py b/python/python/lancedb/table.py index 3d950ce7b..9b65565d6 100644 --- a/python/python/lancedb/table.py +++ b/python/python/lancedb/table.py @@ -7447,7 +7447,10 @@ def _infer_target_schema( if _name_suggests_vector_column(field.name) and is_list_type: if peeked is None: - peeked, reader = peek_reader(reader) + try: + peeked, reader = peek_reader(reader) + except StopIteration: + return schema, reader # Use the most common length of the list as the dimensions dim = _modal_list_size(peeked.column(i)) diff --git a/python/python/tests/test_db.py b/python/python/tests/test_db.py index 78f68fb80..09ee719db 100644 --- a/python/python/tests/test_db.py +++ b/python/python/tests/test_db.py @@ -2,6 +2,7 @@ # SPDX-FileCopyrightText: Copyright The LanceDB Authors +import asyncio import contextlib import inspect import pickle @@ -436,6 +437,27 @@ async def test_create_table_from_iterator_async(mem_db_async: lancedb.AsyncConne assert await table.count_rows() == 10 +@pytest.mark.asyncio +async def test_create_table_empty_vector_column_async( + mem_db_async: lancedb.AsyncConnection, +): + # A zero-row list column named like a vector leaves nothing to infer the + # dimension from; create_table used to hang instead of returning. + data = pa.table( + { + "id": pa.array([], type=pa.int64()), + "vector": pa.array([], type=pa.list_(pa.float32())), + } + ) + + table = await asyncio.wait_for( + mem_db_async.create_table("test", data=data), timeout=10 + ) + + assert await table.count_rows() == 0 + assert await table.schema() == data.schema + + @pytest.mark.parametrize( "batches", [ diff --git a/python/python/tests/test_util.py b/python/python/tests/test_util.py index 17758c41e..15ad167c3 100644 --- a/python/python/tests/test_util.py +++ b/python/python/tests/test_util.py @@ -917,6 +917,23 @@ def test_infer_target_schema(): assert output == expected +@pytest.mark.parametrize( + ("name", "data_type"), + [ + ("vector", pa.list_(pa.float32())), + ("embedding", pa.large_list(pa.float64())), + ("vector", pa.list_(pa.int32())), + ], +) +def test_infer_target_schema_empty_reader(name, data_type): + data = pa.table({name: pa.array([], type=data_type)}) + + schema, reader = _infer_target_schema(data.to_reader()) + + assert schema == data.schema + assert sum(batch.num_rows for batch in reader) == 0 + + def test_infer_target_schema_with_vector_embedding_names(): """Test that _infer_target_schema detects vector columns with 'vector'/'embedding'.