N8n code python

From czlonkowski/n8n-skills · View interactive version

N8n code python is an AI agent skill from the GitHub repository czlonkowski/n8n-skills. Write Python in n8n Code nodes (native Python, `language` pythonNative, n8n 2.x). Use when the user explicitly wants Python in a Code node, when migrating old Pyodide/"Python (Beta)" code that used _input/_json/_node/_now, or when a Python Code node fails with NameError, "Security violations detecte

How to use this skill

Skill file

Python Code Node (native)

Since n8n 2.0 the Code node's Python runs as native Python in a task runner (`language:

"pythonNative"`). The old Pyodide "Python (Beta)" is gone, and with it every n8n helper it had:

_input, _json, _node, _now, _today and _jmespath all raise NameError now. Code copied

from old templates, forum posts or older docs usually breaks on this.


JavaScript first — stronger than before

Native Python gives you two variables and plain Python, and by default no imports: no

json, datetime or re. Everything n8n-specific ($('Node'), $jmespath, Luxon,

this.helpers.httpRequest, static data) exists only in JavaScript. Use Python only when the user

explicitly asks for it. Even then, first check whether an expression, Edit Fields, or a native

node (Crypto, Date & Time, HTML, XML) does the job. See n8n-code-javascript and the transform

gatekeeper in n8n-expression-syntax.


Quick start

# Run Once for All Items (default mode)
return [
    {"json": {"name": it["json"]["name"], "revenue": it["json"]["revenue"]}}
    for it in _items
    if it["json"].get("active")
]
# Run Once for Each Item
row = _item["json"]
return {"json": {**row, "name_upper": (row.get("name") or "").upper()}}

Node config: `{"language": "pythonNative", "mode": "runOnceForAllItems" | "runOnceForEachItem",

"pythonCode": "..."} on n8n-nodes-base.code` typeVersion 2.


The only inputs: `_items` and `_item`

| Mode | Variable | Shape |

|---|---|---|

| Run Once for All Items | _items | list of plain dicts {"json": {...}, "pairedItem": {...}} |

| Run Once for Each Item | _item | one plain dict {"json": {...}, "pairedItem": {...}} |

all-items mode) raises NameError.

AttributeError: 'dict' object has no attribute 'json'.

branch, bring it in with Merge first, or read it in JavaScript.

uses $('Node').item, add "pairedItem": {"item": i} to each returned item. That isn't

verified on native Python yet, so test-run before relying on it.

Migration table for legacy code:

| Legacy (Pyodide) | Native |

|---|---|

| _input.all() | _items |

| _input.first()["json"] | _items[0]["json"] (guard if _items) |

| _input.item / _json | _item / _item["json"] |

| _node["X"] | not available: Merge upstream, or use JS |

| _now, _today | not available: pass {{ $now.toISO() }} in via Edit Fields, or use JS |

| _jmespath(data, q) | not available: $jmespath in an expression, or a comprehension |

| item.json.field | item["json"]["field"] |


Imports: blocked by default

Every import (standard library and third-party) is checked against an allowlist **before the

code runs**. The default allowlist is empty, so even import json rejects the whole node:

Security violations detected
Line 1: Import of standard library module 'json' is disallowed. Allowed stdlib modules: none

n8n-self-hosting → TASK_RUNNERS.md). Some instances therefore allow json, datetime

and re, and most don't.

one-line test node import json + return [{"json": {"ok": True}}], or ask the user. Never

assume.

Do date math with the Date & Time node or Luxon in an expression. ISO-8601 strings still compare and sort correctly as

plain strings. Use the Crypto node for hashing.

image. Use the HTTP Request node for HTTP.


Sandbox limits (these fail even without imports)

| You write | What happens | Use instead |

|---|---|---|

| eval, exec, compile, open, input, type, getattr, setattr, hasattr, vars, dir, globals, locals, object, memoryview, breakpoint | NameError: name 'type' is not defined (runtime) | isinstance(x, dict); key in d / d.get(key) |

| class Foo: ... | __build_class__ not found (runtime) | dicts + functions |

| x.__class__, "{0.__class__}".format(x), __import__("json") | Security violations detected (whole node rejected before running) | — |

| global counter inside a function | NameError: name 'counter' is not defined, because your code runs inside a wrapper function | nonlocal counter |

Everything else in plain Python works (verified): comprehensions, generators, lambdas, closures,

recursion, try/except, f-strings / .format() / %, sorted/min/max/sum/any/all/

enumerate/zip/round, sets, isinstance, print() (output goes to the browser console).


Return shapes (verified)

Observed on n8n 2.38.5. The auto-wrapping and passthrough behaviours below are undocumented and could

change in a later release. Re-check with a test run after upgrading n8n.

Run Once for All Items

| Return | Result |

|---|---|

| [{"json": {...}}, ...] | canonical, N items |

| [{...}, ...] (plain dicts) | auto-wrapped under json, N items |

| {"json": {...}} or a single plain dict | 1 item |

| _items (mutated in place) | passthrough with your changes |

| None / no return | error Cannot read properties of null (reading 'json') |

Run Once for Each Item

| Return | Result |

|---|---|

| {"json": {...}}, a plain dict, or _item | 1 item |

| None | the item is dropped (a built-in filter) |

| a list | error A 'json' property isn't a dictionary [item 0] |

Value conversion on output: tuple becomes a list, set becomes the string "{1, 2}",

and a datetime becomes str(dt) ("2026-09-16 10:18:00.025792", not ISO). Convert explicitly

(sorted(s), dt.isoformat()).

Prefer the explicit [{"json": ...}] in all-items mode and {"json": ...} in each-item mode.

The auto-wrapping works, but the explicit shape makes the intent obvious to the next reader.


Errors and `onError`

A plain raise ValueError("bad row") fails the node with that message. When the node has an

onError continue mode, the three failure kinds behave differently (verified):

| Failure | continueErrorOutput | continueRegularOutput |

|---|---|---|

| Runtime exception (raise, KeyError, NameError, denied builtin) | {"error": "<message>"} on the error output (main[1]) ✅ | {"error": "<message>"} on the main output |

| Static rejection (Security violations detected: import, dunder) | node marked failed, but the input items, unchanged, go out the success output; main[1] stays empty | input items, unchanged, on the main output |

| Bad return shape (list in each-item mode, None in all-items mode) | same: unchanged input on the success output | same |

The last two are silent-data traps: downstream nodes receive unprocessed input as if the code had

run, and the execution still shows success. No error branch catches them. Prevent them (no

imports unless confirmed, correct return shape) and confirm with a real test run, checking the

Code node's status and output (see n8n-error-handling).

Other messages:

no Python task runner (the stock image ships none). It's an infrastructure problem, not a code

problem. See n8n-self-hosting → TASK_RUNNERS.md.

return, return None). They do not catch _input/_json, dot access, blocked stdlib

imports, dunder access or classes. A test execution is the only reliable check.


Performance

Each Python Code node costs roughly 0.4 s (about 1 s when the runner is cold), noticeably more

than a JS Code node or an expression. Process lists in Run Once for All Items mode rather than

per item, and don't chain several small Python nodes where one would do.


Checklist


Reference

n8n instance (filter, aggregate, group, dedupe, top N, flatten, validate, drop items, text

report, safe nested access, running totals, ISO timestamps).

Related skills

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.