Skip to main content
Most model training frameworks feed data into a model by sequencing through successive batches, a process often referred to as data loading. The simplest way to perform data loading is to read a LanceDB table in batches, in the order its rows are stored, through an identity Permutation. A table itself is not iterable.
In specific use cases, you may need the flexibility to shuffle, split, and select data without using the full StreamingDataset object. In these cases, use Permutation, the lower-level functionality StreamingDataset is built on. A Permutation defines a customizable row ordering, and supports map-style access through __getitem__() and batched access through __getitems__().

Persisting a permutation

By default a permutation table is stored in memory. Call persist before execute to write it to an embedded LanceDB connection backed by local files or object storage. The plan becomes an ordinary table, so another process with access to the same storage can open it and use Permutation.from_tables to read the same split. Only the plan destination must be embedded. The base table can be Embedded or Enterprise; an Enterprise base is pinned through the plan’s recorded base branch and version.
The table returned by execute keeps the destination connection and namespace context, so it can be pickled and reopened directly in a worker process. mode="create" is the default and fails when the destination table already exists. Pass mode="overwrite" to replace it explicitly. Overwriting writes a new table version; readers that already opened the previous version keep reading that snapshot. Use namespace_path to create the plan in a namespace. Persisting the plan itself to a LanceDB Enterprise connection is not supported yet, because reproducible Enterprise plan ordering requires deterministic remote row ordering. The plan records the base table version and branch in its schema metadata. Permutation.base_version and Permutation.base_branch read those values without querying the base table; both are None for older plans that did not record them.

Version Pinning

A Permutation is pinned to the version of its base table at the time it was constructed, and every read (including future reads from a StreamingDataset that wraps it, and every DataLoader worker after it starts) resolves against that pinned version. This makes iteration deterministic across compactions and worker restarts: the permutation only addresses rows that existed when it was built, and rows appended to the base table afterwards are not visible through the existing permutation. To include newly appended rows, build a new Permutation (and any StreamingDataset that depends on it) against the updated table. Permutations written before this behavior shipped continue to read as they did before.

Output formats

By default, a Table data loader emits Arrow data. collate_fn is PyTorch’s batching hook: PyTorch calls it to turn the fetched items into one batch. PyTorch’s default collate function only knows how to combine tensors, NumPy arrays, numbers, dicts, and lists, so it does not accept Arrow data directly. When using a Table directly, pass LanceDB’s lancedb.util.tbl_to_tensor helper as PyTorch’s collate_fn; it converts numeric Arrow columns into a column-major torch.Tensor with shape (columns, rows). Permutation works differently: its default output is a list of Python dicts, which PyTorch’s default collate function can batch into a dict of tensors. However, converting every value to a Python object has a significant performance penalty. To convert the Arrow data in different ways, call with_format() with one of the formats below. Here B is the batch size, C is the number of columns, and *S is the shape of one sample. torch_dict is usually the best choice for training because it keeps column names, handles columns with different sample shapes, and shares Arrow storage where the layout allows. torch_col and torch_row require every column to have the same sample shape. torch_row combines numeric dtypes with torch.promote_types, and torch_col promotes supported mixed numeric dtypes through torch.stack. If they see mixed scalar and vector columns, they name the mismatched fields and shapes and point you to torch_dict. torch_dict, torch, and torch_col reject null values. torch_row preserves the previous NaN behavior for nullable scalar integer and floating columns, but rejects nulls in nested fixed-size-list and tensor columns. Use the "python" or "arrow" format for other nullable columns, or drop/fill the nulls first. lance.bfloat16 values are supported when the leaf field is fixed_size_binary[2] with the ARROW:extension:name metadata set to lance.bfloat16; these become torch.bfloat16 tensors. An unmarked binary column is rejected rather than guessed to be bfloat16. The numpy format returns one ndarray built with batch.to_pandas().to_numpy(). The pandas conversion may copy data and may promote mixed numeric columns to a common numeric dtype, for example int64 and float32 to float64. Nested, list, or incompatible columns produce an object ndarray rather than a numeric array.

Using torch_dict with a torch data loader

torch_dict produces a dictionary of per-column tensors for each batch. Pass an identity function as collate_fn so PyTorch returns that dictionary unchanged. Scalar columns have shape (B,), a FixedSizeList<float, D> column has shape (B, D), and nested fixed-size lists contribute one dimension per level. Fixed-shape tensor columns use their logical shape, with the Arrow axis permutation applied as a view.
When num_workers > 0, replace the lambda with a picklable top-level function that returns its argument.

Using the torch column format with a torch data loader

The torch_col format converts the entire Arrow batch to one tensor with shape (C, B, *S). For a table of scalar columns this is the familiar (columns, rows) tensor. For a single column it is an unsqueeze(0) view of the per-column tensor, so no data is copied; stacking multiple columns does copy. PyTorch’s default collate does not accept a single multi-dimensional tensor. Pass an identity function as collate_fn to receive the tensor directly from the data loader.

Transforming rows with map

To change rows in Python as they are read, call map() with a function that takes a row as a dict and returns a dict. Calls can be chained, and the functions run in order. Pass batched=True to receive the whole batch as a dict of column lists instead; the function must then return one value per input row for every column.
The dict a function returns replaces the row. A column the function does not return is dropped, so return every column you want to keep. A mapped Permutation always produces a list of dicts, like the default format, whether or not batched is set, so PyTorch’s default collate function can batch it. select_columns() and schema still describe the columns read from the table, which are the input of the first function. map() cannot be combined with with_format() or with_transform(). Calling one after the other, in either order, raises a ValueError. To build tensors from mapped rows, convert them in the DataLoader’s collate_fn. When num_workers > 0, pass picklable top-level functions to map(), not lambdas.

Selecting columns

By default, the Table class will return all columns in the table when used as input to PyTorch. If you only need a subset of columns, you can significantly reduce your I/O requirements by selecting only the columns you need. The Permutation class provides a select_columns method that provides this functionality.
Each batch is a dict of the selected columns, the default Permutation format described above.