All skills
czlonkowski avatar

/n8n-code-python

@d6465af

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 detected", "Import of standard library module … is disallowed", "__build_class__ not found", "A 'json' property isn't a dictionary", or "Python runner unavailable". Covers the only two variables (_items/_item), dict-only access, imports blocked by default, the sandbox's denied builtins, accepted return shapes, and how errors interact with onError. JavaScript is the default for Code nodes — native Python has no n8n helpers and, by default, no imports. EXCEPTION — for Python in the AI-agent-callable Custom Code Tool (@n8n/n8n-nodes-langchain.toolCode), use the n8n-code-tool skill instead (input is _query, return must be a string).

Use this Skill: https://skilld.dev/gh/czlonkowski/n8n-skills/n8n-code-python

This session only. Nothing lands on disk.

README.md

≈856 tokens on demand. Your agent reads this file only when SKILL.md points to it.

n8n Code Python Skill

Guidance for writing native Python in n8n Code nodes (language: "pythonNative", n8n 2.x).


Why this skill exists

n8n 2.0 removed the Pyodide-based "Python (Beta)" and replaced it with native Python running in a task runner. A large share of the Python n8n code in circulation (templates, forum answers, older docs, model training data) targets the old runtime and fails on the new one:

  • _input, _json, _node, _now, _today, _jmespath → NameError (only _items / _item exist)
  • item.json.field → AttributeError (dict access only)
  • import json → Security violations detected (every import is blocked unless the instance allowlists it; n8n Cloud allows none)
  • class Foo: → __build_class__ not found; type(), getattr(), hasattr() → NameError

The skill teaches the runtime as it actually behaves. Every rule and pattern was verified by execution on n8n 2.38.5, and the skill keeps pushing agents toward JavaScript, expressions, and native nodes whenever the user hasn't explicitly asked for Python.


Skill activation

  • The user explicitly wants a Python Code node
  • Migrating legacy Pyodide Python (_input.all(), _json, _node[...])
  • A Python Code node fails with NameError, Security violations detected, Import of standard library module … is disallowed, A 'json' property isn't a dictionary, or Python runner unavailable

Not for the AI-agent Custom Code Tool (toolCode) — that's n8n-code-tool.

Example queries:

  • "Write a Python Code node that groups these items by country."
  • "My Python Code node says name '_input' is not defined."
  • "Why can't I import json in the Python Code node?"

File structure

SKILL.md

JavaScript-first rule; _items / _item and dict-only access; legacy→native migration table; imports blocked by default (Cloud vs self-hosted allowlist); sandbox limits (denied builtins, no classes, no dunders, nonlocal instead of global); verified return shapes per mode and output value conversion; how runtime vs static vs return-shape errors behave with onError; performance; checklist.

COMMON_PATTERNS.md

12 import-free patterns, each run verbatim on a live instance with the observed output: filter and reshape, totals, group by, dedupe, top N, flatten nested arrays, validate and flag, drop items in each-item mode, text report, safe nested access, running totals with nonlocal, ISO timestamps without datetime.


Related skills

  • n8n-code-javascript — the default for Code nodes
  • n8n-expression-syntax — $jmespath, Luxon, and the transform gatekeeper
  • n8n-code-tool — Python in the AI-agent Custom Code Tool
  • n8n-error-handling — error outputs and the continueRegularOutput passthrough trap
  • n8n-self-hosting — enabling the Python task runner (TASK_RUNNERS.md)

Version

Version: 2.0.0 — rewritten for native Python (n8n 2.x); replaces the Pyodide-era 1.x content (STANDARD_LIBRARY.md, DATA_ACCESS.md and ERROR_PATTERNS.md were removed because they described a runtime that no longer exists). Compatibility: n8n ≥ 2.0 Code node, language: "pythonNative".


Credits

Part of the n8n-skills project.

Conceived by Romuald Członkowski

Source: SKILL.md on GitHub

1 warning14d5 checks · Risk SAFE
  • Gen Agent Trust Hub14d

    This skill provides safe guidance and code patterns for writing Python within n8n's native task-runner environment. It correctly identifies and describes the platform's sandbox limitations, including disabled imports and restricted built-in functions, ensuring developers work within established security boundaries.

  • Socket14d

    No alerts

  • Snyk14d

    Risk: LOW · No issues

  • Runlayer7mo

    6/6 files flagged

  • ZeroLeaks5mo

    2 findings · Score: 80/100

Signed by skilld at d6465af. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 weeks ago.

Activeupdated 2 weeks ago
  • Python
  • n8n
  • code-node
  • workflow
  • data-transformation
  • standard-library
  • beta

README badge

README badge for czlonkowski/n8n-skills/n8n-code-python

Writes Python code in n8n Code nodes using the _input/_json/_node helper syntax, with access to standard library functions only (json, datetime, re, base64, hashlib, urllib.parse, math, random, statistics). Use this skill when a user explicitly requests Python for n8n workflows; JavaScript is recommended for most cases since it has fuller n8n helper support and no external library restrictions.

Generated from the current SKILL.md.

When should I use Python instead of JavaScript in n8n?
Only when you need specific Python standard library functions (regex, hashlib, statistics) or are significantly more comfortable with Python syntax. JavaScript is recommended for 95% of use cases because it has access to all n8n helper functions and better documentation.
What external libraries can I import in n8n Python Code nodes?
No external libraries are available by default — only the Python standard library (json, datetime, re, base64, hashlib, urllib.parse, math, random, statistics). Self-hosted instances may have additional packages depending on their Python runner configuration.
What is the correct return format for a Python Code node?
Always return a list of dictionaries with a 'json' key: `[{"json": {...}}]`. Returning a plain dictionary, list without the json wrapper, or other formats will cause the workflow to fail.
Why does my webhook data return undefined when I access it directly?
Webhook data is nested under the 'body' property. Access it via `_json.get('body', {})` rather than directly from `_json`, or use `.get()` for safe access to nested fields.
What's the difference between Python (Beta) and Python (Native) modes?
Python (Beta) uses `_input`, `_json`, `_node` helper syntax and built-in helpers like `_now`, making it better for n8n integration. Python (Native) uses only `_items` and `_item` variables with no helpers — use Python (Beta) for most cases.

Generated from the current SKILL.md. These answers refresh after source changes.