Splitting JSON Array of Objects to be fed into an AI Agent

Hey! I have a JSON array of objects that I’m trying to process through an AI Agent node. The issue is that the array is quite large (~30+ items) and adding it to the entire array is not feasible to the AI Agent. I’ve tried implementing the Loop Over Items node, but find that I cannot get the loop to work after the initial run of the AI agent.

Describe the problem/error/question

Trying to split up a JSON object array to be fed to an AI agent, and keep the workflow going with the next set of objects after the AI Agent completes its task.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

This is the truncated output from the first time the “Loop Over Product Batches” node is run after setting the batch amount to 6. However, I have 35+ items.

[
  {
    "State": "Packaged",
    "Base Product": "Bottle",
    "Format": "500mg",
    "Unit": "ea",
    "Brand": "VitaCore",
    "Product Line": "Berberine 500 mg Capsules",
    "GTIN": "10385729461028"
  },
  {
    "State": "Packaged",
    "Base Product": "Box",
    "Format": "100g",
    "Unit": "ea",
    "Brand": "HealthBloom",
    "Product Line": "Collagen Peptides Powder",
    "GTIN": "10385729462589"
  },
  {
    "State": "Packaged",
    "Base Product": "Bottle",
    "Format": "1000mg",
    "Unit": "ea",
    "Brand": "NutriLife",
    "Product Line": "Vitamin C 1000 mg Tablets",
    "GTIN": "10385729463214"
  },
  {
    "State": "Packaged",
    "Base Product": "Pouch",
    "Format": "250g",
    "Unit": "ea",
    "Brand": "PureEssentials",
    "Product Line": "Plant-Based Protein Powder",
    "GTIN": "10385729464793"
  },
  {
    "State": "Packaged",
    "Base Product": "Bottle",
    "Format": "250mg",
    "Unit": "ea",
    "Brand": "Nature's Harmony",
    "Product Line": "Omega-3 Fish Oil Softgels",
    "GTIN": "10385729465812"
  },
  {
    "State": "Packaged",
    "Base Product": "Bottle",
    "Format": "400mg",
    "Unit": "ea",
    "Brand": "ActiveForm",
    "Product Line": "Magnesium Glycinate 400 mg",
    "GTIN": "10385729466987"
  }
]

Information on your n8n setup

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

Hey, @elevatedivan hope all is well. Welcome to the community.

So you have a large array, then you split it into individual items and then you feed it to the AI Agent 6 at a time. What is not working again?

Hey @jabbson, what happens is that after my AI agent runs through the initial 6 objects it goes back to the Loop Over Items node and then the workflow stops. My assumption is that once the 6 objects are completed it will go back to the loop and get the next 6 objects fed to the AI agent. Please let me know if this makes sense.

Yeah, thanks for confirming that.

Just ran your workflow and it appears to have gone through the AI agent mulitple times.

Here is the repro workflow I tried:

As you can see I took your reduced example above and lowered the batch size to 2. This means that there were 3 loop cycles, and the loop shows 4, which is correct - 3 for each iteration and one final one to come out of the Done leg.

If your AI Agent is receiving all data in the first run, the workflow will stop after it finishes processing those 6 items.

Instead of using a Code node, try the Split Items node and check if it works.

Thanks for confirming that the workflow is behaving as intended on your end, @jabbson. The workflow I linked is a subset of a larger workflow. What I want to do is just test the loop to see it’s working. However, when I click 'Play" on just the loop, it works but then doesn’t automatically start the AI Agent. I have to manually click 'Play" on the AI Agent node for it to start. Once that occurs I then the AI Agent returns the items, but then doesn’t start the next batch. This is the issue I’m running into:

  1. Click Plan on the Loop after I’ve gotten my split JSON
  2. Loop runs, then I have to manually click on the AI Agent to run
  3. Then data is sent back to the loop, but doesn’t send the next batch of objects to be tested.

I’m currently on an older version of n8n cloud, 1.82.3, so I’ll have to contact my admin to see if updating the version will fix my issue.

Also, thank you @Mookie_Lian for the Split Items node. I tried that and achieved almost the exact same results from my code node, just need to play around with it some more!

1 Like

intended behaviour, when manually starting a node, the workflow will run up to that node (inclusive) and stop

intended behaviour, similar to above, when you manually start the AI agent the workflow runs this node and nodes before.

1 Like

If you want to test, you better introduce the Limit node after the results are split and only let X number of items through, and run the whole workflow.

1 Like

I figured out the issue: Instead of individually testing the node, I need to click on the Test Workflow button. Once I did that, the workflow executed as intended. Such a simple thing to do :person_facepalming:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.