Multi-option actions

Describe the problem/error/question

This is… going to be difficult to explain, but I really need help and so I will do my best.
I am trying to create a search agent that starts with a form that takes a query (textfield) and search engines/platforms/apis as checkboxes. For example, it (currently) has Google, Exa, Perplexity, Reddit as the four options (to start with).

I may want to search something using one or more of the above. For example, I may select only one of the options or some of the options or all of the options.

And the next thing is for the workflow to do its search and output the results (which I may merge and do whatever actions I want to later).

So, in the case where I selected, let’s say, Google, Exa and Reddit (without Perplexity), it should do a Google Search, then an Exa Search and then a Reddit Search.

I tried using SWITCH, but it kept stopping after Google Search, presumably, because Google was the first option which triggered the SWITCH node.

I tried using Loop Over Items (Split in Batches) node, but I don’t think I figured how to use it.

I could use if/else, but that would be a really complicated web for 4 options.

I really don’t know how to implement it and need some guidance.

TL;DR:
Options selected → Conduct Search using Option → Exhaust all options selected

What is the error message (if any)?

No error message. It’s just that either the flow stops after executing search for the first option or it doesn’t loop back (my implementation is definitely to be blamed here)

Please share your workflow

This is not complete, because none of the approaches I tried made any logical sense.

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 1.66.0
  • Database (default: SQLite): Default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): No clue what this means
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm on RPI
  • Operating system: Debian 12

hello @fpcteam

you can do it via Loop node or sub workflow node (preferable way).

Last node (Aggregate) is needed for proper Looping in case you have multiple outputs from your search engines

Btw, don’t use tokens directly in the header section of the HTTP node, always use a credential. For your case with Exa it would be a Header Auth type

Hi Barn4K, thank you for responding. will try out what you suggested in a while. :slight_smile:
Thanks for pointing out the token vs credentials thing. I edited the token (removed some chars) before pasting it here. I had to use tokens since there’s no direct integration with some APIs and had to use HTTP request.

Thanks again. Will respond back once I have tried it.

Have a wonderful Monday.

W.

Hi @barn4k, I implemented your workflow logic and am running into the same problem I had earlier.

I changed the loop’s batch size to: {{ $(‘n8n Form Trigger’).item.json[‘Where do you want to search?’].length }} (which yields the count of 3) so that it would run multiple times.

But as you can see in the image, only the Google flow runs and not the rest of them.

My guess is that this is what happens:

  1. Workflow enters loop
  2. Switch checks “engine” value. Finds it to be “Google”. Proceed with Google route.
  3. Loops back
  4. Switch checks “engine” value. And it doesn’t advance to the next item. The figurative ‘i’ value for the loop - like i++.
  5. And hence executes Google again and again until the counter runs out.

I don’t know how to make switch consider engine[i] instead of just engine. and i don’t know how to do i++ at the end of the loop.

To be honest, I really don’t understand this node and hence don’t know if it automatically moves the counter (i) ahead.

here’s the code again (with my change to the loop’s batch size):

Leave it as 1, it will be a completely different scenario if you set it to the value more than 1

You are right! It did run Google and Reddit. :exploding_head:

So, it does the i++ thing on its own. but I don’t understand why leaving it at 1 worked.

if you have the time, i would love to learn that.

thanks again buddy. :slight_smile:

Okay. Sorry to trouble you so much. But I unpinned the output and ran the workflow and it didn’t work.

I selected Reddit and Exa but it only ran Reddit.
image

the batch size is 1 as you suggested.

Any ideas on why it didn’t work?

Again. I am really sorry to trouble you so much.

Also:

  1. I selected only EXA to test: but I can’t pass on the query string to the API call:

How do I pass the search query from the form to switch to the exa node?

Seems your Reddit node doesn’t return anything and the Loop ended.
To prevent such cases, open the node settings and turn on the switch Always output data
image

As for the Exa, the search string will be available with the {{ $json.search }} syntax, but as the loop exited on the Reddit node, the Exa one didn’t fired

by Default (without Loop node) n8n will process items as follow:

Nodes Items
Node 1 Item 1
Node 1 Item 2
Node 1 Item N
Node 2 Item 1
Node 2 Item N
Node N Item 1
Node N Item N

So it will act as all items of the node 1, then all items of the nod 2 and so on

But the Loop node will change this behavior. E.g. with the batch size of 1

Nodes Items
Node 1 Item 1
Node 2 Item 1
Node N Item 1
Node 1 Item 2
Node N Item 2
Node 1 Item N
Node N Item N

So it will act as one item of the Node 1, then one item of the node 2 and so on

With the batch size of 2

Nodes Items
Node 1 Item 1
Node 1 Item 2
Node 2 Item 1
Node 2 Item 2
Node N Item 1
Node N Item 2
Node 1 Item N-1
Node 1 Item N
Node N Item N-1
Node N Item N

In more general way it means all batched items for the node 1, then all batched items of the node 2 and so on

Batch is acting as chunks mechanism if you need to reduce the amount of items which should be processed at a time and if you need to process an item with multiple nodes sequentially

Hi M,
thank you for the explanation and more importantly for your patience. :slight_smile:

I removed Reddit, but I implemented the other search apis (exa, perplexity and tavily) so that the workflow doesn’t fail and after doing so, it executes - almost properly.

The reason I say, ‘almost properly’ is because the EXA node runs thrice every time as seen in the above image.

There were no errors during the run, so technically, each of the three nodes should have run just once and the COMBINE_DATA node should have run 3 times instead of 5.

The outputs come out fine which means the nodes work fine.

Can you help me figure out why the EXA node runs thrice?

Here is the workflow (I have removed the api keys for all three of them):

Thanks again. Have a beautiful evening.

That’s strange. Can you show the output of the Exa node for the first and second times? Do they the same?

They more or less have the same results, but if you compare the requestID of the runs, you’ll see that they are all the same. But run three times.

Also, I just tested with EXA (just chose EXA) and in this case it ran only once as expected.

Output of Run 1

[
  {
    "requestId": "5fa048f4c70d6d8eaf29de6f5d21c66a",
    "resolvedSearchType": "neural",
    "results": [
      {
        "score": 0.21583065390586853,
        "title": "Emerging Startups 2020: Top Legal Tech",
        "id": "https://tracxn.com/d/emerging-startups/legal-tech-startups-2020",
        "url": "https://tracxn.com/d/emerging-startups/legal-tech-startups-2020",
        "publishedDate": "2020-11-20T00:00:00.000Z",
        "author": "Tracxn"
      },
      {
        "score": 0.21490398049354553,
        "title": "Emerging Startups 2022: Top Legal Tech Startups",
        "id": "https://tracxn.com/d/emerging-startups/top-legal-tech-startups-2022",
        "url": "https://tracxn.com/d/emerging-startups/top-legal-tech-startups-2022",
        "publishedDate": "2022-01-06T00:00:00.000Z",
        "author": "Tracxn"
      },
      {
        "score": 0.2144208550453186,
        "title": "Home - DreamLegal",
        "id": "https://dreamlegal.in/",
        "url": "https://dreamlegal.in/",
        "publishedDate": "2021-04-11T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.2089901715517044,
        "title": "LegalMind - Building a synergy between law and technology.",
        "id": "https://legalmind.tech/",
        "url": "https://legalmind.tech/",
        "author": "Arpit Jain"
      },
      {
        "score": 0.2037971466779709,
        "title": "India's nascent legaltech startups pique investor interest",
        "id": "https://www.dealstreetasia.com/stories/legal-tech-startups-india-320478",
        "url": "https://www.dealstreetasia.com/stories/legal-tech-startups-india-320478",
        "publishedDate": "2022-12-13T00:00:00.000Z",
        "author": "Vibhuti Sharma"
      },
      {
        "score": 0.2035125344991684,
        "title": "LegalTech Artificial Intelligence Market Research Report: Market size, Industry outlook, Market Forecast, Demand Analysis,Market Share, Market Report 2021-2026",
        "id": "https://www.industryarc.com/Report/18534/legaltech-artificial-intelligence-market-research-report.html",
        "url": "https://www.industryarc.com/Report/18534/legaltech-artificial-intelligence-market-research-report.html",
        "publishedDate": "2023-02-06T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.2020741105079651,
        "title": "Welcome to Legal Startups",
        "id": "https://legalstartups.info/",
        "url": "https://legalstartups.info/",
        "publishedDate": "2022-12-31T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.20146119594573975,
        "title": "Legaltech Startups: An Upcoming Business Model In Town | Entrepreneur",
        "id": "https://www.entrepreneur.com/en-in/news-and-trends/legaltech-startups-an-upcoming-business-model-in-town/447930",
        "url": "https://www.entrepreneur.com/en-in/news-and-trends/legaltech-startups-an-upcoming-business-model-in-town/447930",
        "publishedDate": "2023-03-20T00:00:00.000Z",
        "author": "S Shanthi"
      },
      {
        "score": 0.201410710811615,
        "title": "Revolutionizing India's Legal Landscape with Legaltech",
        "id": "https://www.killerstartups.com/startups/revolutionizing-indias-legal-landscape-with-legaltech",
        "url": "https://www.killerstartups.com/startups/revolutionizing-indias-legal-landscape-with-legaltech",
        "publishedDate": "2023-08-30T00:00:00.000Z",
        "author": "Miranda Ross"
      },
      {
        "score": 0.2004965990781784,
        "title": "legal tech: Latest News & Videos, Photos about legal tech | The Economic Times - Page 1",
        "id": "https://economictimes.indiatimes.com/topic/legal-tech",
        "url": "https://economictimes.indiatimes.com/topic/legal-tech",
        "publishedDate": "2023-02-23T00:00:00.000Z",
        "author": "Economic Times"
      }
    ]
  }
]

Output of Run 2

[
  {
    "requestId": "5fa048f4c70d6d8eaf29de6f5d21c66a",
    "resolvedSearchType": "neural",
    "results": [
      {
        "score": 0.21583065390586853,
        "title": "Emerging Startups 2020: Top Legal Tech",
        "id": "https://tracxn.com/d/emerging-startups/legal-tech-startups-2020",
        "url": "https://tracxn.com/d/emerging-startups/legal-tech-startups-2020",
        "publishedDate": "2020-11-20T00:00:00.000Z",
        "author": "Tracxn"
      },
      {
        "score": 0.21490398049354553,
        "title": "Emerging Startups 2022: Top Legal Tech Startups",
        "id": "https://tracxn.com/d/emerging-startups/top-legal-tech-startups-2022",
        "url": "https://tracxn.com/d/emerging-startups/top-legal-tech-startups-2022",
        "publishedDate": "2022-01-06T00:00:00.000Z",
        "author": "Tracxn"
      },
      {
        "score": 0.2144208550453186,
        "title": "Home - DreamLegal",
        "id": "https://dreamlegal.in/",
        "url": "https://dreamlegal.in/",
        "publishedDate": "2021-04-11T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.2089901715517044,
        "title": "LegalMind - Building a synergy between law and technology.",
        "id": "https://legalmind.tech/",
        "url": "https://legalmind.tech/",
        "author": "Arpit Jain"
      },
      {
        "score": 0.2037971466779709,
        "title": "India's nascent legaltech startups pique investor interest",
        "id": "https://www.dealstreetasia.com/stories/legal-tech-startups-india-320478",
        "url": "https://www.dealstreetasia.com/stories/legal-tech-startups-india-320478",
        "publishedDate": "2022-12-13T00:00:00.000Z",
        "author": "Vibhuti Sharma"
      },
      {
        "score": 0.2035125344991684,
        "title": "LegalTech Artificial Intelligence Market Research Report: Market size, Industry outlook, Market Forecast, Demand Analysis,Market Share, Market Report 2021-2026",
        "id": "https://www.industryarc.com/Report/18534/legaltech-artificial-intelligence-market-research-report.html",
        "url": "https://www.industryarc.com/Report/18534/legaltech-artificial-intelligence-market-research-report.html",
        "publishedDate": "2023-02-06T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.2020741105079651,
        "title": "Welcome to Legal Startups",
        "id": "https://legalstartups.info/",
        "url": "https://legalstartups.info/",
        "publishedDate": "2022-12-31T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.20146119594573975,
        "title": "Legaltech Startups: An Upcoming Business Model In Town | Entrepreneur",
        "id": "https://www.entrepreneur.com/en-in/news-and-trends/legaltech-startups-an-upcoming-business-model-in-town/447930",
        "url": "https://www.entrepreneur.com/en-in/news-and-trends/legaltech-startups-an-upcoming-business-model-in-town/447930",
        "publishedDate": "2023-03-20T00:00:00.000Z",
        "author": "S Shanthi"
      },
      {
        "score": 0.201410710811615,
        "title": "Revolutionizing India's Legal Landscape with Legaltech",
        "id": "https://www.killerstartups.com/startups/revolutionizing-indias-legal-landscape-with-legaltech",
        "url": "https://www.killerstartups.com/startups/revolutionizing-indias-legal-landscape-with-legaltech",
        "publishedDate": "2023-08-30T00:00:00.000Z",
        "author": "Miranda Ross"
      },
      {
        "score": 0.2004965990781784,
        "title": "legal tech: Latest News & Videos, Photos about legal tech | The Economic Times - Page 1",
        "id": "https://economictimes.indiatimes.com/topic/legal-tech",
        "url": "https://economictimes.indiatimes.com/topic/legal-tech",
        "publishedDate": "2023-02-23T00:00:00.000Z",
        "author": "Economic Times"
      }
    ]
  }
]

Output of Run 3

[
  {
    "requestId": "5fa048f4c70d6d8eaf29de6f5d21c66a",
    "resolvedSearchType": "neural",
    "results": [
      {
        "score": 0.21583065390586853,
        "title": "Emerging Startups 2020: Top Legal Tech",
        "id": "https://tracxn.com/d/emerging-startups/legal-tech-startups-2020",
        "url": "https://tracxn.com/d/emerging-startups/legal-tech-startups-2020",
        "publishedDate": "2020-11-20T00:00:00.000Z",
        "author": "Tracxn"
      },
      {
        "score": 0.21490398049354553,
        "title": "Emerging Startups 2022: Top Legal Tech Startups",
        "id": "https://tracxn.com/d/emerging-startups/top-legal-tech-startups-2022",
        "url": "https://tracxn.com/d/emerging-startups/top-legal-tech-startups-2022",
        "publishedDate": "2022-01-06T00:00:00.000Z",
        "author": "Tracxn"
      },
      {
        "score": 0.2144208550453186,
        "title": "Home - DreamLegal",
        "id": "https://dreamlegal.in/",
        "url": "https://dreamlegal.in/",
        "publishedDate": "2021-04-11T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.2089901715517044,
        "title": "LegalMind - Building a synergy between law and technology.",
        "id": "https://legalmind.tech/",
        "url": "https://legalmind.tech/",
        "author": "Arpit Jain"
      },
      {
        "score": 0.2037971466779709,
        "title": "India's nascent legaltech startups pique investor interest",
        "id": "https://www.dealstreetasia.com/stories/legal-tech-startups-india-320478",
        "url": "https://www.dealstreetasia.com/stories/legal-tech-startups-india-320478",
        "publishedDate": "2022-12-13T00:00:00.000Z",
        "author": "Vibhuti Sharma"
      },
      {
        "score": 0.2035125344991684,
        "title": "LegalTech Artificial Intelligence Market Research Report: Market size, Industry outlook, Market Forecast, Demand Analysis,Market Share, Market Report 2021-2026",
        "id": "https://www.industryarc.com/Report/18534/legaltech-artificial-intelligence-market-research-report.html",
        "url": "https://www.industryarc.com/Report/18534/legaltech-artificial-intelligence-market-research-report.html",
        "publishedDate": "2023-02-06T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.2020741105079651,
        "title": "Welcome to Legal Startups",
        "id": "https://legalstartups.info/",
        "url": "https://legalstartups.info/",
        "publishedDate": "2022-12-31T00:00:00.000Z",
        "author": null
      },
      {
        "score": 0.20146119594573975,
        "title": "Legaltech Startups: An Upcoming Business Model In Town | Entrepreneur",
        "id": "https://www.entrepreneur.com/en-in/news-and-trends/legaltech-startups-an-upcoming-business-model-in-town/447930",
        "url": "https://www.entrepreneur.com/en-in/news-and-trends/legaltech-startups-an-upcoming-business-model-in-town/447930",
        "publishedDate": "2023-03-20T00:00:00.000Z",
        "author": "S Shanthi"
      },
      {
        "score": 0.201410710811615,
        "title": "Revolutionizing India's Legal Landscape with Legaltech",
        "id": "https://www.killerstartups.com/startups/revolutionizing-indias-legal-landscape-with-legaltech",
        "url": "https://www.killerstartups.com/startups/revolutionizing-indias-legal-landscape-with-legaltech",
        "publishedDate": "2023-08-30T00:00:00.000Z",
        "author": "Miranda Ross"
      },
      {
        "score": 0.2004965990781784,
        "title": "legal tech: Latest News & Videos, Photos about legal tech | The Economic Times - Page 1",
        "id": "https://economictimes.indiatimes.com/topic/legal-tech",
        "url": "https://economictimes.indiatimes.com/topic/legal-tech",
        "publishedDate": "2023-02-23T00:00:00.000Z",
        "author": "Economic Times"
      }
    ]
  }
]

There’s one more observation I just made.

I selected only Tavily for my search

[
{
"search": "Ravi Warrier",
"engine": "Tavily"
}
]

and yet it ran the EXA node:

Here’s the workflow again:

Oh, and I just figured out what you meant by setting credentials for API/HTTP Request calls. I did it for Exa and Perplexity. Tavily unfornately needs the key in the body (don’t worry, I edited the key in the flow/code above).

Thanks for that tip. :slight_smile:

1 Like

Please, disable the option Always Output Data for the Switch node.

Seems it has unpredicted behavior to output empty object to the first branch. @Jon @netroy @ria FYI

1 Like

Hey. Morning.

Thanks for the fix. :slight_smile:

W.

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