Overview
The Python Code Node lets you execute custom Python code within a workflow. It provides a sandboxed runtime environment where you can perform data transformations, calculations, string manipulation, and any custom logic that cannot be expressed with the built-in node types. Code runs in an isolated environment with restricted imports and resource limits to ensure security and prevent runaway executions.Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
code | string | — | Python code to execute (required) |
inputs | object | {} | Map of variable names available in the code to workflow context references |
outputs | object | {} | Map of output context variable names to local variable names in the code |
timeout_seconds | number | 10 | Maximum execution time before the code is terminated |
Input and Output
Inputs
Theinputs configuration maps workflow context variables into the Python runtime as local variables. These variables are available directly in your code.
Outputs
Theoutputs configuration maps local variables from the code back to the workflow context. Only variables listed in outputs are written to the context — all other local variables are discarded after execution.
{{greeting_message}}, {{best_match}}, and {{confident}}.
Sandboxed Environment
The Python Code Node runs in a restricted sandbox to prevent security issues and resource abuse.Allowed Imports
The following standard library modules are available:| Category | Modules |
|---|---|
| Data Processing | json, csv, re, collections, itertools |
| Math & Science | math, statistics, decimal, fractions |
| Text | string, textwrap, unicodedata |
| Date & Time | datetime, time, calendar |
| Data Structures | dataclasses, enum, typing |
| Utilities | copy, functools, operator, hashlib, base64, uuid |
| URL Handling | urllib.parse |
Resource Limits
| Resource | Limit | Description |
|---|---|---|
| Execution time | Configurable (timeout_seconds) | Default 10 seconds, max 60 seconds |
| Memory | 128 MB | Maximum heap allocation |
| Output size | 1 MB | Maximum total size of output variables |
| Recursion depth | 100 | Maximum call stack depth |
Examples
Data Transformation
Transform search results into a formatted context string for an AI Agent Node:context_string, result_count
Calculation
Compute statistics from query results:stats, summary
JSON Parsing and Restructuring
Parse and restructure LLM output:categorized, category_count
Date Calculations
days_between, is_overdue, status
Error Handling
If the Python code raises an exception, the node transitions to theFAILED status and emits a node_failed SSE event. The error message includes:
- The exception type and message
- The line number where the error occurred
- A truncated traceback
error variable and branch accordingly.
Best Practices
Keep code focused and short
Keep code focused and short
Each Python Code Node should do one thing. If your code is longer than 30-40 lines, consider splitting it into multiple nodes or extracting logic into a Python-based plugin.
Always define explicit inputs and outputs
Always define explicit inputs and outputs
Even though inputs could be accessed through other mechanisms, always declare them in the
inputs configuration. This makes the node self-documenting and ensures the visual editor shows the data dependencies.Handle missing or unexpected data
Handle missing or unexpected data
Workflow variables may be
None or have unexpected types. Always validate inputs at the top of your code and provide sensible defaults.Use the timeout wisely
Use the timeout wisely
The default 10-second timeout is generous for most data transformations. If your code needs more time, it may be doing too much work — consider breaking it into smaller steps or moving heavy processing to a background task.