Skip to main content

Run Python Code

Controlled node

Overview​

Execute Python code in a serverless container environment. This node allows you to run custom Python scripts with support for package installation, prebuild scripts, and structured input/output handling. The code executes in an isolated environment with access to a filesystem where inputs are provided via inputs.json and results are captured from output.json.

Execution Environment​

  • Language: Python 3.x
  • Timeout: 10 minutes (600 seconds) maximum execution time
  • Package Manager: pip (packages installed fresh per execution)
  • Prebuild: Shell commands supported for environment setup
  • Storage: Ephemeral filesystem with inputs.json and output.json access
Input/Output Pattern

The node follows a file-based IO pattern:

  • Read workflow inputs from inputs.json (automatically populated from the inputs socket)
  • Write results to output.json (automatically captured to the result output)
  • Use print() for logging (captured in stdout)

Inputs​

InputTypeDescriptionDefault
RunEventTriggers the Python code execution-
CodeTextPython code to execute. Must read from inputs.json and write to output.json.See default template below
PackagesTextComma-separated list of pip packages to install (e.g., requests==2.28.1, pandas). Can also accept an array of strings via input connection.-
Prebuild ScriptTextShell commands to run before code execution (e.g., apt-get update). Useful for system dependencies.-
InputsDataDictionary/object passed to the script via inputs.json{}

Default Code Template​

# Read inputs from inputs.json
import json

with open('inputs.json') as f:
inputs = json.load(f)

# Your code here
result = {
'message': 'Hello from Python!',
'data': inputs
}

# Write result to output.json (this becomes the node's output)
with open('output.json', 'w') as f:
json.dump(result, f)

# stdout is for logging only (visible in logs, not returned as output)
print('Processing completed successfully')

Outputs​

OutputTypeDescription
DoneEventFires when execution completes successfully or with error
ResultDataContains the JSON data written to output.json, or an error object if execution failed. Error format: { error: { message, stdout, stderr }, buildLogs?: string }

Error Handling​

When execution fails, the result output contains:

  • error.message: The error message or exception details
  • error.stdout: Standard output from the execution
  • error.stderr: Standard error output
  • buildLogs: Build logs if package installation failed (only present on build failures)

Runtime Behavior and Defaults​

  • Package Installation: Packages are installed via pip before code execution. Specify versions using standard pip syntax (package==version). If the input is provided as an array via connection, each element is treated as a package name.
  • Prebuild Phase: Runs before package installation. Use for system-level setup (e.g., installing system libraries required by Python packages).
  • Input Serialization: The inputs data socket is automatically serialized to inputs.json in the execution environment. Complex objects, arrays, and primitives are supported.
  • Output Capture: The contents of output.json are parsed as JSON and emitted from the result output. Ensure your code writes valid JSON.
  • Execution Limits:
    • Maximum 10 minutes total execution time (includes prebuild, package installation, and code execution)
    • Maximum 600 polling attempts with 1-2 second intervals
    • Memory and CPU limits apply based on the serverless runtime configuration

Example Usage​

Basic Data Processing​

Process incoming data and return a transformed result:

import json

# Read inputs
with open('inputs.json') as f:
data = json.load(f)

# Process data (e.g., calculate statistics)
numbers = data.get('numbers', [])
average = sum(numbers) / len(numbers) if numbers else 0

# Write result
with open('output.json', 'w') as f:
json.dump({
'average': average,
'count': len(numbers),
'sum': sum(numbers)
}, f)

Using External Packages​

Install and use external libraries like requests or pandas:

Packages input: requests==2.31.0, pandas

import json
import requests
import pandas as pd

with open('inputs.json') as f:
config = json.load(f)

# Fetch data
response = requests.get(config['url'])
data = response.json()

# Process with pandas
df = pd.DataFrame(data['results'])
summary = df.describe().to_dict()

with open('output.json', 'w') as f:
json.dump(summary, f)

With Prebuild Script​

Install system dependencies before Python packages:

Prebuild Script:

apt-get update && apt-get install -y libxml2-dev libxslt1-dev

Packages: lxml, beautifulsoup4

from bs4 import BeautifulSoup
import json

with open('inputs.json') as f:
html_content = json.load(f)['html']

soup = BeautifulSoup(html_content, 'lxml')
text = soup.get_text()

with open('output.json', 'w') as f:
json.dump({'clean_text': text}, f)