How can I dynamically pass and execute a Python script generated in one node inside an n8n Code (Python) node without manually copying it?

Describe the problem/error/question

I’m trying to automate the execution of a Python script inside a Code (Python) node in n8n, but I can’t find a way to dynamically inject or pass the script generated from a previous node. I currently have to manually copy/paste the generated Python code into the node, which breaks the automation flow.

My goal is to allow a user prompt to generate the script and have it automatically executed in a downstream Python node, without manual intervention.

What is the error message (if any)?

There is no specific error, but the issue is that the Code (Python) node does not allow dynamic assignment of the script to be executed. I cannot use an expression like {{ $json["script"] }} as the actual code to be executed — it doesn’t dynamically evaluate or replace the code block of the node.

What are you trying to achieve?

Here’s my use case:

  1. LLM generates an SQL query (using the RAG document as context) to fetch user-requested data, executed via the Execute Query node..
  2. The result is passed to a LLM (OpenAI) node, where the user can ask for specific aggregations or data transformations.
  3. The LLM generates a Python script using pandas to apply those transformations (e.g., group by, sum, mean, etc.).
  4. I want to take the script generated by the LLM and automatically run it in a Python Code node without manually pasting it.
  5. Ideally, I also want to later visualize the data via bar plots, scatter plots, sunburst, etc., based on user prompts — but I’m stuck at the step where the Python script needs to be executed dynamically.

Workflow

Information on your n8n setup

  • n8n version: 1.101.1
  • Database (default: SQLite): MySQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm
  • Operating system: Windows
2 Likes

Seems not available currently.

My another thought will be use LLM to determine the visualization type first and draw based on the type and the data.

But yes it’s more hard coded but still can generate different type of charts when the request change.

The main issue lies in how to pass dynamically generated Python code to the Code node in n8n without having to write it manually. From what I’ve seen, this is a current limitation of the tool: the Code node does not allow receiving and executing Python code directly as dynamic input from previous nodes. If anyone knows of any alternative approaches to automate this, I’d appreciate your suggestions.