Agent executing the sql code he has created

Describe the problem/error/question

Hello! New user here. I have been trying to create a multiagent environment where firstly, the user writes a question about a database (named superheroes) as an input. The first agent is going to take that input and rewrite it for the better (for example: how many dudes are there → How many heroes are in total). This agent is going to connect to the database to know how really is the database and how to upgrade the input (knowing the names of the columns or the formant). This seems to work from what I have seen. However, the problem comes with the second agent. This agent will take the output from the first agent (no code, only a well made string telling what to do) and write a SQL code for it so it can be executed on the database. However, the problem lays on the fact that in the second mysql node, what’s the query I should tell him to use? Or, is it possible to make it authomatic (the agent does it by its own?). I have tried to browse information but no luck with it.

What is the error message (if any)?

Everything works except that second mysql node of Agent 2. Which tells me (using the input “How many dudes are there”.

You have an error in your SQL syntax on line 1 near ‘The total number of superheroes is 6895.’

Any idea what can it be? I put the workflow here too. I believe it must be something about variables / queries.

sql: The total number of superheroes is 6895., code: ER_PARSE_ERROR

Information of n8n setup

  • n8n version: latest version
  • Database: mySQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): own
  • Running n8n via (Docker, npm, n8n cloud, desktop app): npm locally
  • Operating system: Windows 11

hey @MaskedLlama04
I think you should rewrite your prompt for 2nd agent.
you can try prompt like:
You are an expert SQL assistant. Based on the following question, write only a valid MySQL query to run on the superheroestable. Do not explain anything. Return ONLY the SQL query. Question: {{ $json.output }}

I hope this helps :slight_smile:

Hey @MaskedLlama04 hope all is well, welcome to the community.

Please see if this workflow helps:

Here is the output of the chat:

And here is the intermediate results:
Initial query: “how many dudes are there”
SQL interrogation to yield enhanced query: “DESCRIBE superheroes;”
Enhanced query: “How many superheroes are there in the superheroes table?”
SQL interrogation based on the enhanced query: “SELECT COUNT(*) AS superhero_count FROM superheroes;”
SQL interrogation result: “superhero_count:10”
User response: “There are 10 superheroes in the superheroes table.”

Hi @MaskedLlama04 instead of defining the sql query for the AI to understand, you can use {{ $fromAI("query", "mysql query to run") }} to simplifying the query that will used by the AI.
the query means “what query that AI need to insert”, and mysql ... run is the description to make the AI understand what query provider that AI need to execute.
Also, some AI model cannot understand perfectly without specific instructions, you can use OpenAI, or describe the instructions more detailed

Hello everyone! I have been trying your several solutions, and now at least it compiles. I have tried to do what @jabbson did and the results were upgraded. However, I see its sql code does not really take into account the structure of the database, which is supossed to be learnt at the first agent. Anyone knows how?

What @withabdul and @moosa said about the query and prompt is correct too, thank you. The kind of error it gives me now tho is

Table 'mysql.dc_comics_characters' doesn't exist
sql: SELECT id FROM dc_comics_characters GROUP BY eye_color HAVING COUNT(*) > 10, code: ER_NO_SUCH_TABLE

When for example the database is named superheroes, there is no “dc_comics_characters” field and things like that. Forgot to say I really need to work with ollama, so I dont know how much that limits the idea.

Then you should give structure of tables in system prompt too.

It all comes down to prompt fiddling. If you really need the agent to learn the db structure, teach it how to do it, enforce certain actions.

  • Run SHOW DATABASES; to …
  • Run SHOW TABLES; to…
  • Run DESCRIBE table_name; or SHOW COLUMNS FROM table_name; to…
  • Run SHOW CREATE TABLE table_name; to…

triggers…
indexes…
procedures…
table data samples…
data from INFORMATION_SCHEMA

etc… There are so many things you can propose the agent does in order to understand the structure, just give it a little push.