{"record":{"id":"4f753ed9b2ddaede","repo":"pola-rs/polars","slug":"source-must-implement-the-arrow-pycapsule-interfac","errorCode":null,"errorMessage":"source must implement the Arrow PyCapsule Interface (__arrow_c_stream__)","messagePattern":"source must implement the Arrow PyCapsule Interface \\(__arrow_c_stream__\\)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/arrow_c_stream.py","lineNumber":76,"sourceCode":"    ... ]\n    >>> reader = pa.RecordBatchReader.from_batches(schema, batches)\n    >>> pl.scan_arrow_c_stream(reader).collect()\n    shape: (5, 2)\n    ┌─────┬─────┐\n    │ a   ┆ b   │\n    │ --- ┆ --- │\n    │ i64 ┆ str │\n    ╞═════╪═════╡\n    │ 1   ┆ x   │\n    │ 2   ┆ y   │\n    │ 3   ┆ z   │\n    │ 4   ┆ a   │\n    │ 5   ┆ b   │\n    └─────┴─────┘\n    \"\"\"\n    if not hasattr(source, \"__arrow_c_stream__\"):\n        msg = \"source must implement the Arrow PyCapsule Interface (__arrow_c_stream__)\"\n        raise TypeError(msg)\n\n    import polars._plr as plr\n\n    reader = plr.PyArrowCStreamReader(source)\n\n    def io_source(\n        with_columns: list[str] | None,\n        predicate: Expr | None,\n        n_rows: int | None,\n        batch_size: int | None,  # noqa: ARG001\n    ) -> Iterator[DataFrame]:\n        remaining = n_rows\n        while (batch := reader.next_batch(with_columns)) is not None:\n            df = pl.DataFrame._from_pydf(batch)\n            if predicate is not None:\n                df = df.filter(predicate)\n            if remaining is not None:\n                df = df.head(remaining)","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/arrow_c_stream.py#L58-L94","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass an object implementing __arrow_c_stream__ (e.g. pyarrow.RecordBatchReader, or table.__arrow_c_stream__() explicitly).","Upgrade pyarrow to >= 14 (ideally latest) so its objects implement the PyCapsule Interface.","If you have a pyarrow.Table, call my_table.__arrow_c_stream__() and pass that.","Use the appropriate polars API instead (pl.from_arrow / pl.scan_pyarrow_dataset)."],"exampleFix":"// before\n pl.scan_arrow_c_stream(pa_table)  # old pyarrow without capsule support\n// after\n reader = pa_table.to_reader()\n lf = pl.scan_arrow_c_stream(reader)  # or upgrade pyarrow >= 14","handlingStrategy":"type-guard","validationCode":"if not hasattr(source, '__arrow_c_stream__'):\n    raise TypeError('pass an object implementing the Arrow PyCapsule Interface')","typeGuard":"from typing import Any, Protocol\n\nclass ArrowCapsuleStream(Protocol):\n    def __arrow_c_stream__(self, requested_schema: Any = None) -> Any: ...\n\ndef is_arrow_c_stream(obj: Any) -> bool:\n    return isinstance(obj, ArrowCapsuleStream) or callable(getattr(obj, '__arrow_c_stream__', None))","tryCatchPattern":"try:\n    lf = pl.scan_arrow_c_stream(source)\nexcept TypeError as e:\n    if '__arrow_c_stream__' in str(e):\n        source = source.to_reader() if hasattr(source, 'to_reader') else source.__arrow_c_stream__()\n        lf = pl.scan_arrow_c_stream(source)\n    else:\n        raise","preventionTips":["Require pyarrow >= 14 (PyCapsule Interface) in your dependency pins.","Check hasattr(obj, '__arrow_c_stream__') before calling scan_arrow_c_stream.","Use pl.from_arrow/pl.scan_pyarrow_dataset for non-capsule pyarrow objects."],"tags":["python","arrow","type-mismatch","lazyframe"],"backgroundTag":"incompatible-source-type","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}