---
title: Write logic for Lakeflow Designer custom operators
description: "Write logic for Lakeflow Designer custom operators: python-run-function run() functions, Unity Catalog UDFs and UDTFs, previews, and API access."
canonical_url: https://docs.databricks.com/aws/en/designer/write-operator-logic
---

# Write logic for Lakeflow Designer custom operators

The *operator logic* is the code that a custom operator runs. For a `python-run-function` operator, it's an inline Python `run()` function. For a `uc-udf` or `uc-udtf` operator, it's a Unity Catalog function written in SQL or Python.

Writing the logic is one step in [building a shared custom operator](https://docs.databricks.com/aws/en/designer/user-operators.md#build-steps). After you write and test the logic, you [describe the operator in YAML](https://docs.databricks.com/aws/en/designer/operators-yaml-ref.md) so Lakeflow Designer knows how to show it in the UI, then [register it](https://docs.databricks.com/aws/en/designer/user-operators.md#register-operator) so it appears in the **Operators** pane. To see all of the steps together for one operator, follow a tutorial such as [Tutorial: Compound interest calculator operator](https://docs.databricks.com/aws/en/designer/tutorial-compound-interest-udf.md).

## <a id="python-run-function" />Write a python-run-function operator

Every `python-run-function` operator must define a `run()` function:

```python
def run(config: Dict[str, Any], inputs: Dict[str, Any], spark) -> Dict[str, Any]:
```

* **`config`**: User-configured values from the UI, keyed by property name.
* **`inputs`**: Input DataFrames, keyed by input port `name`.
* **`spark`**: The active SparkSession.
* **Returns**: A dictionary mapping output port `name` values to DataFrames.

The following example filters rows from an input DataFrame:

```python
def run(config, inputs, spark):
    df = inputs["in"]
    filtered = df.filter(config["filter_expression"])
    return {"out": filtered}
```

To test the function before you register it, call it in a notebook with sample data:

```python
test_df = spark.createDataFrame(
    [("Alice", 100), ("Bob", 200)],
    ["name", "amount"]
)

result = run(
    config={"filter_expression": "amount > 100"},
    inputs={"in": test_df},
    spark=spark
)

result["out"].show()
```

If your operator requires external pip packages, add the `environment` field to the YAML:

```yaml
environment:
  environment_version: '4'
  dependencies:
    - requests==2.31.0
    - beautifulsoup4==4.12.0
```

## <a id="uc-logic" />Write a UDF or UDTF operator

You can write Unity Catalog functions in SQL or Python. Python functions are wrapped in a SQL `CREATE FUNCTION` statement:

**SQL function:**

```sql
CREATE OR REPLACE FUNCTION my_catalog.my_schema.calculate_bmi(weight_kg DOUBLE, height_m DOUBLE)
RETURNS DOUBLE
LANGUAGE SQL
RETURN
  SELECT weight_kg / (height_m * height_m);
```

**Python function (wrapped in SQL):**

```sql
CREATE OR REPLACE FUNCTION my_catalog.my_schema.calculate_bmi(weight_kg DOUBLE, height_m DOUBLE)
RETURNS DOUBLE
LANGUAGE PYTHON
AS $$
  return weight_kg / (height_m ** 2)
$$;
```

UDFs process a single value at a time and return a calculated value. UDTFs process tables row by row and can maintain state across all rows. Use `uc-udf` for column-level transforms and `uc-udtf` for operations like ML clustering or aggregation.

A UDTF handler class defines `eval()`, which runs for each input row. Optionally, it also defines `__init__()` to set up state and `terminate()` to yield final results after all rows. See [Python user-defined table functions (UDTFs) in Unity Catalog](https://docs.databricks.com/aws/en/udf/udtf-unity-catalog.md).

```python
class MyOperator:
    def __init__(self):
        # Optional. Called before processing - initialize any values needed.
        ...

    def eval(self, row):
        # Required. Called one time per input row - process or accumulate data here.
        ...

    def terminate(self):
        # Optional. Called after all rows - perform final calculations and yield results.
        ...
```

> [!NOTE]
>
> UDTF return tables must have fixed, explicit types. You can't reference input column types in the return configuration.

## <a id="preview-mode" />Skip side effects during previews

Lakeflow Designer supports previews while in design mode. For operators that call external APIs or write to external systems, add an `is_preview` config property so you can skip side effects during preview. Users click **Run** to run the operator with its side effects. The built-in Python operator doesn't need this property, because it reads `config["is_preview"]` automatically. See [Python preview mode](https://docs.databricks.com/aws/en/designer/built-in-operators.md#python-preview-mode).

```yaml
config:
  type: object
  properties:
    is_preview:
      type: boolean
      format: is_preview
      default: false
```

Lakeflow Designer automatically sets this value to `true` during preview. Check it in your logic to skip side effects:

```python
# In a python-run-function
if config.get("is_preview"):
    return {"out": inputs["in"]}
```

```sql
-- In a UC function (SQL)
CASE WHEN is_preview THEN 'preview' ELSE /* actual work */ END
```

## <a id="connections" />Call external APIs with Unity Catalog HTTP connections

For custom operators based on SQL Unity Catalog functions that call external APIs, use Unity Catalog HTTP connections to securely store credentials:

```sql
CREATE CONNECTION my_api_connection TYPE HTTP OPTIONS (
  host 'https://api.example.com',
  port '443',
  base_path '/v1/',
  bearer_token 'your-token-here'
);
```

Then use the connection in your SQL UDF with the `http_request()` function. For details, see [Connect to external HTTP services](https://docs.databricks.com/aws/en/query-federation/http.md).

## <a id="workspace-client" />Access workspace resources with WorkspaceClient

For `python-run-function` operators, you can use the Databricks `WorkspaceClient` to access workspace resources and external APIs:

```python
def run(config, inputs, spark):
    from databricks.sdk import WorkspaceClient
    w = WorkspaceClient()
    # Use w to access workspace resources
```

## <a id="next-steps" />Additional resources

* [Custom operator YAML reference](https://docs.databricks.com/aws/en/designer/operators-yaml-ref.md)
* [Register and deploy your operator](https://docs.databricks.com/aws/en/designer/user-operators.md#register-operator)
* [Tutorial: Gmail email sender operator](https://docs.databricks.com/aws/en/designer/tutorial-gmail-email-sender.md)
* [Tutorial: K-means clustering](https://docs.databricks.com/aws/en/designer/tutorial-kmeans-clustering-udtf.md)
