opensi·si

Lance is an open multimodal lakehouse table format for AI. You can use Hugging Face paths (hf://) to access Lance datasets on the Hub. This lets you scan and search large…

Lance

Lance is an open multimodal lakehouse table format for AI. You can use Hugging Face paths (hf://) to access Lance datasets on the Hub. This lets you scan and search large datasets on the Hugging Face Hub without having to copy the entire dataset locally.

Getting Started

To get started, pip install pylance and pyarrow:

pip install pylance pyarrow

Why Lance?

  • Optimized for ML/AI workloads: Lance is a modern columnar format designed for fast random access without compromising scan performance, making it useful for search, analytics, training, feature engineering and many more use cases.
  • Multimodal assets are stored as bytes, or binary objects ("blobs as files") in Lance alongside embeddings, and traditional scalar data -- this makes it easier to govern, share, and distribute your large datasets via the Hub.
  • Indexing is a first-class citizen (native to the format itself): Lance comes with fast, on-disk, scalable vector and FTS indexes that sit right alongside the dataset on the Hub, so you can share not only your data but also your embeddings and indexes without your users needing to recompute them.
  • Flexible schema and data evolution let you incrementally add new features/columns (moderation tags, embeddings, etc.) without needing to rewrite the entire table.

Store all your data in one place

In Lance, your multimodal data assets (images, audio, video) are stored as raw bytes alongside your scalar metadata and embeddings. This makes it easy to scan and filter your dataset in one place without needing to stitch together multiple storage systems.

Stream from the Hub with datasets

Use load_dataset(..., streaming=True) to scan and iterate through the data without downloading it locally.

from datasets import load_dataset

# Return as a Hugging Face dataset
ds = load_dataset(
    "lance-format/laion-1m",
    split="train",
    streaming=True
)
# Take first three rows
for row in ds.take(3):
    print(row["caption"])

Streaming is great for sampling metadata to understand what you have. For vector search or working with large binary blobs, you can use the Lance dataset API, explained below.

Warning Streaming is fast for sampling simple scalar metadata but not as quick for embeddings or large multimodal assets. To work with large datasets, it's recommended to scan the metadata, identify subsets of what you need, and download that portion of the dataset locally to avoid facing Hub rate limits: hf download lance-format/laion-1m --repo-type dataset --local-dir ./laion

Stream from the Hub with lance.dataset

You can also scan a Lance dataset that's stored on the Hugging Face Hub using the hf:// path specifier. This scans the remote dataset without requiring that you download it locally. Using the Lance dataset API, it's very simple to set limits, filters and projections to only fetch the data you need.

import lance

# Return as a Lance dataset
ds = lance.dataset("hf://datasets/lance-format/laion-1m/data/train.lance")

scanner = ds.scanner(
    columns=["caption", "url", "similarity"],
    limit=5
)

rows = scanner.to_table().to_pylist()
for row in rows:
    print(row)

Work with binary assets

The example below shows how images are retrieved from a Lance dataset as raw JPEG bytes in the image column, and used downstream. Use ds.take to fetch the bytes and write them to disk so you can use them elsewhere.

import lance
from pathlib import Path

ds = lance.dataset("hf://datasets/lance-format/laion-1m/data/train.lance")

dir_name = "laion_samples"
Path(dir_name).mkdir(exist_ok=True)

rows = ds.take([0, 1], columns=["image", "caption"]).to_pylist()
for idx, row in enumerate(rows):
    with open(f"{dir_name}/{idx}.jpg", "wb") as f:
        f.write(row["image"])
        print(f"Wrote image with caption: {row['caption']}")

Write a subset to a new Lance dataset

Working with large datasets? It's simple to run a filtered scan to select a subset of rows from the Hub and materialize them into a local Lance dataset.

import lance

ds = lance.dataset("hf://datasets/lance-format/laion-1m/data/train.lance")
scanner = ds.scanner(
    columns=["image", "caption", "width", "height"],
    filter="width >= 200 AND height >= 100",
    limit=10,
)
subset = scanner.to_table()

lance.write_dataset(subset, "./laion_subset")