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 <rishi02102017@users.noreply.github.com>
This commit is contained in:
Jyotishman Das
2026-11-30 19:14:15 +08:00
committed by GitHub
co-authored by rishi02102017
parent f639e178a1
commit 163177f6a5
2 changed files with 38 additions and 2 deletions
+2 -2
View File
@@ -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()
+36
View File
@@ -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<item: int64>[2]" in expected
assert "v: fixed_size_list<item: int64>[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]}]