From 163177f6a5ba5a44b2b1ac68748aa35089478bc7 Mon Sep 17 00:00:00 2001 From: Jyotishman Das <107503099+rishi02102017@users.noreply.github.com> Date: Mon, 30 Nov 2026 16:44:15 +0530 Subject: [PATCH] fix(python): report iterator schema mismatches in create_table (#4921) When `create_table()` is given an iterator and a later item can't be cast to the schema of the first one, the user gets an `AttributeError` instead of the intended schema-mismatch error: ```python def batches(): yield [{"id": 1, "text": "a"}] yield [{"id": "two", "text": "b"}] db.create_table("t", batches()) # RuntimeError: ... Unknown error: 'list' object has no attribute 'schema' ``` The same happens with pandas DataFrames, and with `table.add(..., on_bad_vectors="drop" | "fill" | "null")`, which goes through the same helper. A plain `table.add(batches())` already reports the mismatch correctly. The cause is in `_iterator_to_reader`: the error message reads `.schema` from the raw item the iterator yielded rather than from the Arrow table it was converted to. This PR uses the converted table's schema, as `scannable._from_iterable` already does, and adds a regression test in `test_db.py` that fails without the change. With the fix, the error is: ``` Input iterator yielded a batch with schema that does not match the schema of other batches. Expected: id: int64 text: string Got: id: string text: string ``` Tested with `pytest python/tests/test_db.py python/tests/test_table.py -m "not slow and not s3_test"` (574 passed, 17 skipped) and ruff on the changed files. --------- Co-authored-by: rishi02102017 --- python/python/lancedb/table.py | 4 ++-- python/python/tests/test_db.py | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/python/python/lancedb/table.py b/python/python/lancedb/table.py index 5ab32d81c..d6f26de8b 100644 --- a/python/python/lancedb/table.py +++ b/python/python/lancedb/table.py @@ -669,11 +669,11 @@ def _iterator_to_reader( if table.schema != schema: try: table = table.cast(schema) - except pa.lib.ArrowInvalid: + except (ValueError, pa.ArrowNotImplementedError, pa.ArrowTypeError): raise ValueError( f"Input iterator yielded a batch with schema that " f"does not match the schema of other batches.\n" - f"Expected:\n{schema}\nGot:\n{batch.schema}" + f"Expected:\n{schema}\nGot:\n{table.schema}" ) yield from table.to_batches() diff --git a/python/python/tests/test_db.py b/python/python/tests/test_db.py index 5656f5203..78f68fb80 100644 --- a/python/python/tests/test_db.py +++ b/python/python/tests/test_db.py @@ -436,6 +436,42 @@ async def test_create_table_from_iterator_async(mem_db_async: lancedb.AsyncConne assert await table.count_rows() == 10 +@pytest.mark.parametrize( + "batches", + [ + [[{"id": 1, "text": "foo"}], [{"id": "two", "text": "bar"}]], + [[{"id": 1, "text": "foo"}], [{"id": [2], "text": "bar"}]], + [[{"id": 1, "text": "foo"}], [{"id": 2, "text": "bar", "extra": 0}]], + [ + pd.DataFrame({"id": [1], "text": ["foo"]}), + pd.DataFrame({"id": ["two"], "text": ["bar"]}), + ], + ], + ids=["invalid_cast", "unsupported_cast", "extra_column", "dataframes"], +) +def test_create_table_from_iterator_with_mismatched_batches( + mem_db: lancedb.DBConnection, batches +): + with pytest.raises( + RuntimeError, match="does not match the schema of other batches" + ): + mem_db.create_table("test", data=iter(batches)) + + +def test_create_table_from_iterator_with_mismatched_list_width( + mem_db: lancedb.DBConnection, +): + batches = [ + pa.table({"v": pa.array([[1, 2]], type=pa.list_(pa.int64(), 2))}), + pa.table({"v": pa.array([[1, 2, 3]], type=pa.list_(pa.int64(), 3))}), + ] + with pytest.raises(RuntimeError) as exc_info: + mem_db.create_table("test", data=iter(batches)) + expected, _, actual = str(exc_info.value).partition("Got:") + assert "v: fixed_size_list[2]" in expected + assert "v: fixed_size_list[3]" in actual + + def test_create_exist_ok_mode(tmp_db: lancedb.DBConnection): db = tmp_db data = [{"vector": [1.1, 1.2]}, {"vector": [0.2, 1.8]}]