From 7509718f0292dcaff8561b412468e342a2fffac9 Mon Sep 17 00:00:00 2001 From: Lenin Mookiah Date: Mon, 30 Nov 2026 07:34:27 -0800 Subject: [PATCH] fix(python): create an empty table when a vector column has no rows to infer from (#5205) This PR fixes [issue #5204](https://github.com/lancedb/lancedb/issues/5204). Fixes #5204 `create_table(name, data=...)` hung forever when `data` was an Arrow table with zero rows and a variable-length list column named like a vector (`vector`, `embedding`). With no `schema=`, `_infer_target_schema` peeks the first batch to infer the vector dimension. On a reader with no batches, `peek_reader` raises `StopIteration`. That happens inside `asyncio.to_thread`, where a `StopIteration` can't be set on the Future, so `create_table` never returned. With no rows the dimension can't be inferred, the same case as an all-null vector column, which already keeps the variable-length list. So `_infer_target_schema` now returns the input schema unchanged when the reader has no batches. ## Testing `test_infer_target_schema_empty_reader` covers `list` `vector`, `large_list` `embedding` and `list` `vector`. All three fail on main with `StopIteration` and pass with the fix. ``` pytest python/tests/test_util.py # 142 passed, 1 skipped pytest python/tests/test_table.py python/tests/test_db.py -m "not slow and not s3_test" # 636 passed, 17 skipped pre-commit run --files # ruff, typos, whitespace: passed ``` I ran these with main's `python/` over the extension from the 0.42.0b26 wheel, since I don't have a Rust toolchain here, and with pylance 14.0.3 because the pinned 15.0.0b35 wasn't on the indexes I could reach. With the fix, the reproduction from the issue creates the table: 0 rows, `vector: list`. The Node.js SDK has no equivalent peek on empty input (its empty-data path requires a schema), so no change there. --------- Co-authored-by: Lenin Mookiah <2639584+leninworld@users.noreply.github.com> --- python/python/lancedb/table.py | 5 ++++- python/python/tests/test_db.py | 22 ++++++++++++++++++++++ python/python/tests/test_util.py | 17 +++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) 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'.