pola-rs/polars · error · TypeError

source must implement the Arrow PyCapsule Interface…

Error message

source must implement the Arrow PyCapsule Interface (__arrow_c_stream__)

What it means

scan_arrow_c_stream only accepts objects exporting the Arrow PyCapsule Interface, i.e. exposing a __arrow_c_stream__ method. Passing any other object (e.g. a pyarrow.Table without capsules, a pandas DataFrame, a plain list) raises this TypeError.

Solutions

  1. Pass an object implementing __arrow_c_stream__ (e.g. pyarrow.RecordBatchReader, or table.__arrow_c_stream__() explicitly).
  2. Upgrade pyarrow to >= 14 (ideally latest) so its objects implement the PyCapsule Interface.
  3. If you have a pyarrow.Table, call my_table.__arrow_c_stream__() and pass that.
  4. Use the appropriate polars API instead (pl.from_arrow / pl.scan_pyarrow_dataset).

Example fix

// before
 pl.scan_arrow_c_stream(pa_table)  # old pyarrow without capsule support
// after
 reader = pa_table.to_reader()
 lf = pl.scan_arrow_c_stream(reader)  # or upgrade pyarrow >= 14
Defensive patterns

Strategy: type-guard

Validate before calling

if not hasattr(source, '__arrow_c_stream__'):
    raise TypeError('pass an object implementing the Arrow PyCapsule Interface')

Type guard

from typing import Any, Protocol

class ArrowCapsuleStream(Protocol):
    def __arrow_c_stream__(self, requested_schema: Any = None) -> Any: ...

def is_arrow_c_stream(obj: Any) -> bool:
    return isinstance(obj, ArrowCapsuleStream) or callable(getattr(obj, '__arrow_c_stream__', None))

Try / catch

try:
    lf = pl.scan_arrow_c_stream(source)
except TypeError as e:
    if '__arrow_c_stream__' in str(e):
        source = source.to_reader() if hasattr(source, 'to_reader') else source.__arrow_c_stream__()
        lf = pl.scan_arrow_c_stream(source)
    else:
        raise

Prevention

When it happens

Trigger: Calling pl.scan_arrow_c_stream(source) with an object lacking __arrow_c_stream__ — e.g. an old pyarrow version (<14), a pandas DataFrame, a list/dict, or a materialized result instead of a stream-capable object.

Common situations: Running with pyarrow older than the PyCapsule Interface adoption; confusing pl.scan_pyarrow_dataset with scan_arrow_c_stream; passing a table that must first produce a record-batch reader/stream.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/4f753ed9b2ddaede. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/io/arrow_c_stream.py:76

    ... ]
    >>> reader = pa.RecordBatchReader.from_batches(schema, batches)
    >>> pl.scan_arrow_c_stream(reader).collect()
    shape: (5, 2)
    ┌─────┬─────┐
    │ a   ┆ b   │
    │ --- ┆ --- │
    │ i64 ┆ str │
    ╞═════╪═════╡
    │ 1   ┆ x   │
    │ 2   ┆ y   │
    │ 3   ┆ z   │
    │ 4   ┆ a   │
    │ 5   ┆ b   │
    └─────┴─────┘
    """
    if not hasattr(source, "__arrow_c_stream__"):
        msg = "source must implement the Arrow PyCapsule Interface (__arrow_c_stream__)"
        raise TypeError(msg)

    import polars._plr as plr

    reader = plr.PyArrowCStreamReader(source)

    def io_source(
        with_columns: list[str] | None,
        predicate: Expr | None,
        n_rows: int | None,
        batch_size: int | None,  # noqa: ARG001
    ) -> Iterator[DataFrame]:
        remaining = n_rows
        while (batch := reader.next_batch(with_columns)) is not None:
            df = pl.DataFrame._from_pydf(batch)
            if predicate is not None:
                df = df.filter(predicate)
            if remaining is not None:
                df = df.head(remaining)

View on GitHub (pinned to fe841f959e)