python_function
Execute custom Python code to transform events with full programming flexibility.
Overview
Execute custom Python code to transform events with full programming flexibility. This step runs user-provided Python code in a secure Pyodide sandbox, allowing you to implement any custom transformation logic that built-in steps don't cover. Your code is loaded once at workflow startup, and the specified handler function is called for each event. The function receives the event as a dictionary and must return the modified event. You can use Python's standard library to manipulate data, perform calculations, call external functions, or implement complex business logic. The sandbox ensures isolation and security while giving you the full power of Python for data transformation.
Quick Start
steps:
- type: python_function
code: <string>
handler: <string>Configuration
| Parameter | Type | Required | Description |
|---|---|---|---|
code | string | Yes | Python source code that defines your handler. The code runs once inside the sandboxed module namespace. |
handler | string | Yes | Name of the function declared in 'code'. It must accept a dict event and return the transformed event (or None). |
Examples
Combine and format fields
Create derived fields from existing data
type: python_function
code: |
def process(event):
event['full_name'] = f"{event['first_name']} {event['last_name']}"
event['display_name'] = event['full_name'].title()
return event
handler: process
Calculate totals and taxes
Perform financial calculations on order data
type: python_function
code: |
def process(event):
subtotal = event['quantity'] * event['price']
tax_rate = 0.08
tax = subtotal * tax_rate
event['subtotal'] = round(subtotal, 2)
event['tax'] = round(tax, 2)
event['total'] = round(subtotal + tax, 2)
return event
handler: process
Clean and normalize data
Remove sensitive fields and normalize formats
type: python_function
code: |
def process(event):
# Remove sensitive fields
for field in ['password', 'ssn', 'credit_card']:
event.pop(field, None)
# Normalize email and phone
if 'email' in event:
event['email'] = event['email'].lower().strip()
if 'phone' in event:
event['phone'] = ''.join(filter(str.isdigit, event['phone']))
return event
handler: process
Parse and transform dates
Convert date strings to different formats
type: python_function
code: |
from datetime import datetime
def process(event):
if 'date_string' in event:
dt = datetime.strptime(event['date_string'], '%Y-%m-%d')
event['formatted_date'] = dt.strftime('%B %d, %Y')
event['day_of_week'] = dt.strftime('%A')
event['timestamp'] = int(dt.timestamp())
return event
handler: process
Conditional logic and categorization
Apply business rules to categorize data
type: python_function
code: |
def process(event):
score = event.get('score', 0)
if score >= 90:
event['grade'] = 'A'
event['category'] = 'excellent'
elif score >= 80:
event['grade'] = 'B'
event['category'] = 'good'
elif score >= 70:
event['grade'] = 'C'
event['category'] = 'average'
else:
event['grade'] = 'F'
event['category'] = 'needs_improvement'
event['passed'] = score >= 70
return event
handler: process
Array and string manipulation
Process arrays and strings with Python methods
type: python_function
code: |
def process(event):
# Split comma-separated tags into list
if 'tags' in event and isinstance(event['tags'], str):
event['tags'] = [tag.strip() for tag in event['tags'].split(',')]
# Create acronym from title
if 'title' in event:
words = event['title'].split()
event['acronym'] = ''.join(w[0].upper() for w in words if w)
# Count unique values
if 'items' in event:
event['unique_items'] = len(set(event['items']))
return event
handler: process
Advanced Options
These options are available on all steps for error handling and retry logic:
| Parameter | Type | Default | Description |
|---|---|---|---|
retries | integer | 0 | Number of retry attempts (0-10) |
backoff_seconds | number | 0 | Backoff (seconds) applied between retry attempts |
retry_propagate | boolean | false | If True, raise last exception after exhausting retries; otherwise swallow. |