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]}]