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:
- LLM generates an SQL query (using the RAG document as context) to fetch user-requested data, executed via the Execute Query node..
- The result is passed to a LLM (OpenAI) node, where the user can ask for specific aggregations or data transformations.
- The LLM generates a Python script using
pandas
to apply those transformations (e.g., group by, sum, mean, etc.). - I want to take the script generated by the LLM and automatically run it in a Python Code node without manually pasting it.
- 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