Implementation:Eventual Inc Daft Daft Cls
| Knowledge Sources | |
|---|---|
| Domains | Data_Engineering, User_Defined_Functions |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for decorating Python classes as stateful user-defined function containers provided by the Daft library.
Description
The @daft.cls decorator converts a Python class into a Daft user-defined class. Daft classes allow you to initialize a class instance once and reuse it for multiple rows. Initialization is lazy: arguments are saved at instantiation time and only passed into __init__ when a query is executed. Class methods are automatically treated as Daft functions. The @daft.method decorator can override default arguments for individual methods, and @daft.method.batch creates batch-oriented methods.
Usage
Import via import daft and apply the @daft.cls decorator to a Python class. Use when you need stateful processing with expensive initialization such as ML model inference.
Code Reference
Source Location
- Repository: Daft
- File:
daft/udf/__init__.py - Lines: L339-432
Signature
@daft.cls(
*,
gpus: float = 0,
use_process: bool | None = None,
max_concurrency: int | None = None,
max_retries: int | None = None,
on_error: Literal["raise", "log", "ignore"] | None = None,
name_override: str | None = None,
)
Import
import daft
@daft.cls
class MyModel:
def __init__(self, model_path: str):
self.model = load_model(model_path)
def __call__(self, text: str) -> str:
return self.model(text)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| gpus | float | No | Number of GPUs each instance requires. Defaults to 0. Fractional values between 0 and 1.0 are supported. |
| use_process | None | No | Whether to run each instance in a separate process. Daft auto-selects if unset. |
| max_concurrency | None | No | Maximum number of concurrent instances of the class. |
| max_retries | None | No | Maximum number of retries on failure. |
| on_error | None | No | Error handling strategy. |
| name_override | None | No | Custom name for the UDF class in plan visualization and progress bars. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | Daft-wrapped class | A Daft-wrapped class that can be instantiated with init arguments and whose methods produce Expressions for DataFrame pipelines. |
Usage Examples
Basic Usage
import daft
@daft.cls
class MyModel:
def __init__(self, model_path: str):
self.model = load_model(model_path)
def __call__(self, prompt: str) -> str:
return self.model(prompt)
my_model = MyModel("path/to/model")
df = daft.from_pydict({"prompt": ["hello", "world", "daft"]})
df = df.with_columns({"generated": my_model(df["prompt"])})
GPU-Accelerated Usage
import daft
@daft.cls(gpus=1)
class GpuModel:
def __init__(self, path: str):
import torch
self.model = torch.load(path)
def __call__(self, data: str) -> str:
return self.model.predict(data)