UDF and Arrow
Pin your version and read the changelog before upgrading.
Data crosses the Python/Rust boundary as Arrow batches through the PyCapsule
interface (__arrow_c_array__ / __arrow_c_stream__). pyarrow is never
required.
Zero-copy interchange
Batches returned by the SDK implement both capsule methods, so Polars, DuckDB, and PyArrow consume them directly:
import polars, duckdb
df = polars.DataFrame(batch) # DataFrame
rows = duckdb.sql("SELECT * FROM batch").fetchall()
With pyarrow installed, pyarrow.record_batch(batch) works as well.
Dict ingestion
from_iter accepts dicts. Type inference on the first non-null value:
| Python type | Arrow type |
|---|---|
int | int64 |
float | float64 (int columns are promoted if a float appears) |
str | utf8 |
bool | bool |
dict / list | JSON-encoded utf8 |
None | nullable column (a column that is all None is rejected) |
All rows must share the same keys (ragged rows are rejected), and a column that
mixes incompatible types (e.g. int and str) is rejected with a ConfigError
naming the column.
Writing a UDF
map_batches(callable) is vectorized: the callable receives one batch and
returns one of:
- an Arrow PyCapsule batch (typically the input, possibly transformed),
None— the batch is dropped,- a list of dicts — re-ingested with the same inference rules,
- any object exposing the PyCapsule interface.
def drop_small(batch):
df = polars.DataFrame(batch)
return df.filter(df["v"] >= 5) # Polars DataFrames export Arrow capsules
flow = (
arkflow.Pipeline()
.from_iter([{"v": i} for i in range(10)])
.map_batches(drop_small)
.to_stdout()
)
Rules and boundaries:
- A raising UDF surfaces as
ExecutionErrorwith the traceback attached; the interpreter survives. - Returning
Nonefrommap_batchesdrops the batch from the output. - Per-row Python UDFs are the
pythonprocessor in YAML;map_batchesis the embedded equivalent and needs no plugin feature.
Metadata columns
Source metadata travels as __meta_-prefixed columns (__meta_source,
__meta_partition, __meta_offset, __meta_timestamp, …). iter_batches(include_meta=False)
projects them out for convenience; connector pipelines keep them in the schema,
and SQL sees them as ordinary columns.