AI Agent stuck in a loop of sending request to the Postgres tool

I am working on a simple workflow involving an AI agent that interacts with a PostgreSQL database to retrieve answers. The workflow operates as follows:

  1. The AI agent receives a question from the chat.
  2. It generates a query using the {{ $fromAI("query") }} template and sends it to the PostgreSQL tool.
  3. The PostgreSQL tool executes the query and returns the result.

Observed Issue

  • During the first iteration, everything works as expected:
    • The AI generates an appropriate query.
    • The PostgreSQL tool executes it correctly.
    • The answer is returned without any issues.
  • However, in many cases, the initial answer is insufficient, and the AI agent tries to refine its query and send additional iterations. Here’s where the issue arises:
    • The PostgreSQL tool appears to receive multiple inputs with different queries from the AI.
    • Despite receiving these inputs, the tool executes only the initial query repeatedly, returning the same answer every time.
    • The process continues until the maximum number of iterations is reached, after which the workflow shuts down.

Environment Details

  • Version: 1.67.1
  • Setup: Running inside a Docker container

Question

What could be causing the PostgreSQL tool to repeatedly execute the first query, even when receiving updated inputs from the AI agent?


It looks like your topic is missing some important information. Could you provide the following if applicable.

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

n8n version - 1.67.1
Database - Postgres
n8n ECECUTION_PROCESS - defaul
Running n8n via Docker.
Operation system: Rocky Linux.

To me it seems like the query the {{ $fromAI(“query”) }} which is supposed to represent the Query that will be executed agains the database only is generated once and then whatever inputs the ai agent tried to pass to the postgres tool it just keeps executin the first one over and over again

For additional context the workflow is triggered by the chat. I am ssking it some qustion related to the database. Since it does not yet know the schema it makes the first request to get the column names, tables etc. Then it receives a correct response, changes the query based on output and send an update query to get what it is asked for, for example the sum of values in a specific column. However the output does not change, the postgres tool keeps returning again and again the database schema whatever input is beeing sent.
Eventually it just reaches the maximum requests limit.

Thanks for the help!

Welcome to the community @Igor_Katsai !

Tip for sharing information

Pasting your n8n workflow


Ensure to copy your n8n workflow and paste it in the code block, that is in between the pairs of triple backticks, which also could be achieved by clicking </> (preformatted text) in the editor and pasting in your workflow.

```
<your workflow>
```

That implies to any JSON output you would like to share with us.

Make sure that you have removed any sensitive information from your workflow and include dummy or pinned data with it!


The behaviour you observe is by design. AI tools (or sub-nodes) are different from the other nodes in its behaviour:

You would need to adjust your workflow one way or another to ensure only one item (last when appropriate) is relayed to the tool.

This makes absolutely no sense!

The Tool use agent support the use of Postgres node, when opening the node we are recommended to use {{ $fromAI(“query”) }} to set the query, but when having multiple runs, we always only run the first query multiple times.
There seems to be no way to change this, and if multiple runs isn’t the whole point of using an Agent in the first place, I don’t know what is :slight_smile:

How can this be by design? Either there is a bug in the execution or in the docs?

2 Likes

I have the same issue with {{ $fromAI(“query”) }} and almost the exact same workflow like @Igor_Katsai

How should we handle this properly?

Welcome to the community @Thijs_Morlion .

I would rather use a SQL Agent for this task than generic Tools Agent.

Thank you!

Well, I saw there is an SQL agent indeed, but my knowledge does not reach far enough to know if I can use it with a BigQuery database. I’ll look into it. :grin:

fromai when used in a loop somehow uses the same query from the 1st loop into the 2nd loop for some reason even though the loop is parsing the new table to ai agent.

its a feature, not a bug!

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