Describe the problem/error/question
I have a valid python code that works outside n8n but in here I constantly get told output is invalid.
Please share your workflow
import json
from collections import defaultdict
data = _input.all()
def count(data):
os_version_counts = defaultdict(int)
for item in data:
if "osversion" in item:
os_version = str(item["osversion"])
os_version_counts[os_version] += 1
formatted_counts = []
for version, count in os_version_counts.items():
if '.' in version:
major_version = version.rsplit('.', 1)[0]
formatted_version = f"{major_version}.x"
formatted_counts.append({formatted_version: count})
else:
formatted_counts.append({version: count})
return formatted_counts
formatted_counts = count(data)
Example of json it takes
[
{
"row_number": 2,
"osversion": "13.4.1",
"needosupdate": "13.5.1",
},
{
"row_number": 3,
"osversion": "13.4.1",
"needosupdate": "13.5.1",
},
{
"row_number": 4,
"osversion": 13.4,
"needosupdate": "13.5.1",
}
]
Share the output returned by the last node
I’ve tested many versions, latest one outputs
[
{
'13.4.x': 2
},
{
'13.x': 1
}
]
There is really a need for better feedback loop for troubleshooting those issue
I’ve tested bunch of other inputs but all get same error.
Information on your n8n setup
**n8n version:1.1.1
Database (default: SQLite):
n8n EXECUTIONS_PROCESS setting (default: own, main):
Running n8n via n8n cloud:
Operating system:
Hi @Mateusz_Bijakowski
I’m admittedly not as familiar with python as I am with javascript, but you mentioning that your python script works outside n8n made me think of an important caveat that you may be running into here - n8n provides python support with pyodide, so you’d be limited to packages included with pyodide which you can find here . Might that be an issue you’re running into?
I would hope to get error both on import and on code failing. This is not the case.
I “think” collections is part of core python and should work but I’m happy to re-make it without it.
I will try and report back.
That would still be a big issue of very poor error handling/reporting.
No change with new code
def count(data):
os_version_counts = {}
for item in data:
if "osversion" in item:
os_version = str(item["osversion"])
os_version_counts[os_version] = os_version_counts.get(os_version, 0) + 1
formatted_counts = []
for version, count in os_version_counts.items():
if '.' in version:
major_version = version.rsplit('.', 1)[0]
formatted_version = f"{major_version}.x"
formatted_counts.append({formatted_version: count})
else:
formatted_counts.append({version: count})
return formatted_counts
formatted_counts = count(data)
locally returns this, fails in n8n
[{'13.4.x': 2}, {'13.x': 1}]
netroy
August 25, 2023, 4:12pm
6
I think the issue is that you are using item["osversion"]
instead of item.json.osversion
This code works for me without any issues:
from collections import defaultdict
data = items
os_version_counts = defaultdict(int)
for item in data:
os_version = str(item.json.osversion)
os_version_counts[os_version] += 1
formatted_counts = []
for version, count in os_version_counts.items():
if '.' in version:
major_version = version.rsplit('.', 1)[0]
formatted_version = f"{major_version}.x"
formatted_counts.append({formatted_version: count})
else:
formatted_counts.append({version: count})
return formatted_counts
4 Likes
k, so this works, thank you for that.
But I would like to be very clear that this just highlights the issue.
Errors provided by the system are useless for troubleshooting and our outside knowledge is not helping because of custom data references needed.
So either we need better error to troubleshoot things or we will have to keep asking questions that require someone with more experience to help.
I don’t mind asking for help if I’m stuck with something very custom and advanced but this is honestly very basic mistake that normally would be caught.
2 Likes
system
Closed
September 5, 2023, 11:34am
8
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.