Skill

Generate Python Code for n8n Nodes

Writes Python code in n8n Code nodes: data access patterns, return format rules, and standard library limitations.

Works with n8n

79
Spark score
out of 100
Updated today
Version 15.7.0

Add to Favorites

Why it matters

Leverage Python within n8n workflows to process and transform data. This skill provides expert guidance for writing Python code in n8n Code nodes, focusing on standard library usage and proper data handling.

Outcomes

What it gets done

01

Write Python code snippets for n8n Code nodes.

02

Understand data access patterns like _input.all(), _input.first(), and _input.item.

03

Ensure correct return formats for n8n node outputs.

04

Adhere to limitations, such as the absence of external libraries.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/ag-n8n-code-python | bash

Overview

Python Code Node (Beta)

Provides guidance for writing Python in n8n's Code node, covering data access patterns, the required return format, and the standard-library-only limitation. Use when a Code node genuinely needs Python-specific syntax or standard library functions - JavaScript is preferred for most n8n use cases.

What it does

Provides expert guidance for writing Python code in n8n's Code node (Beta) - covering data access patterns, required return formats, the standard-library-only limitation, and common error prevention, while noting JavaScript is preferred for most use cases.

When to use - and when NOT to

Use Python in an n8n Code node when you need the statistics module, are significantly more comfortable with Python syntax, or your logic maps naturally to list comprehensions. Prefer JavaScript instead for the other 95% of cases, especially anything needing HTTP requests ($helpers.httpRequest()) or advanced date/time handling (Luxon) - and prefer a Set, Filter, or IF/Switch node over any Code node for simple field mapping, filtering, or conditionals.

Inputs and outputs

Covers two Python execution modes: Python (Beta), recommended, which exposes _input, _json, _node, and helpers like _now/_today/_jmespath(); and Python (Native), which only exposes _items/_item with no n8n helpers. Covers "Run Once for All Items" (default, recommended for 95% of cases - aggregation, filtering, batch transforms via _input.all()) versus "Run Once for Each Item" (per-item logic via _input.item, slower for large datasets).

Documents four data access patterns: _input.all() for batch operations, _input.first() for single-object/API-response handling, _input.item for Each Item mode, and _node["NodeName"]["json"] for referencing other workflow nodes' output. Flags the most common mistake: webhook data is nested under _json["body"], not _json directly.

States the critical return format rule - code must return a list of dictionaries each wrapped in a "json" key (e.g. [{"json": {...}}]), with common incorrect formats (bare dict, unwrapped list, plain string) called out as workflow-breaking. States the critical limitation that no external libraries are available (no requests, pandas, numpy, BeautifulSoup) - only the standard library (json, datetime, re, base64, hashlib, urllib.parse, math, random, statistics) - with workarounds like using an HTTP Request node for network calls or switching to JavaScript for pandas-like operations.

Provides five common patterns (data transformation via list comprehension, filtering/aggregation with sum/len, regex-based string extraction, data validation, and statistical analysis via the statistics module) and lists the top five mistakes: importing external libraries, missing return statements, incorrect return format, unguarded dictionary key access (KeyError), and webhook body nesting errors. Best practices recommend always using .get() for safe dictionary access, explicitly handling None values, preferring list comprehensions, keeping return structure consistent, and debugging with print() statements visible in the browser console.

Integrations

Works within n8n's Code node alongside n8n expression syntax, the n8n MCP tools for node search/validation, and workflow patterns for choosing between Python and JavaScript in the transformation step.

Who it's for

n8n workflow builders who occasionally need Python-specific standard library functions or syntax inside a Code node and need to avoid its return-format and external-library pitfalls - not a general Python tutorial, and not the default choice for most n8n data transformation.

items = _input.all()
return [{"json": {**item["json"], "processed": True}} for item in items]

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.