AI Agent + Postgres Tool: workflow created via API doesn't work, same workflow created manually does — what am I missing?

Hey everyone, hoping someone can shed some light on this because I’m completely stumped.
I have two workflows that are supposed to be identical. One works perfectly, the other doesn’t. The only difference is how they were created.
Setup:

  • n8n self-hosted (latest, Docker on VPS)
  • AI Agent with Google Gemini (gemini-2.5-flash)
  • Postgres Tool (postgresTool v2.6, operation: Select) as a tool
  • Simple chat interface
    The working workflow was built manually in the n8n UI. When I ask “what are the critical CVEs?”, the ReAct loop works as expected: Gemini → Postgres Tool → Gemini and the agent correctly reads and uses the data from the db.
    The broken workflow was created programatically via the n8n API. It has the exact same nodes, same credentials, same system prompt, same Postgres config. But the execution order is always: Gemini → Gemini → Postgres Tool — Gemini fires twice before the tool is ever called, so by the time the DB returns data, there’s no second Gemini call left to actually read it. The agent then responds with “no results found” even though the Postgres node clearly shows the correct data in the output panel.
    What I’ve tried and ruled out:
  • System prompt — tried everything from detailed instructions to “ABSOLUTE RULE: call the tool first” to a minimal one-liner. Makes no difference.
  • Model — tested Gemini 2.5-flash, 2.5-flash-lite, OpenAI GPT-4. Same broken behaviour on the API-created workflow.
  • $fromAI() — tool does get called but returns an empty string “” to the LLM, not sure why
  • returnAll: true — data shows in the n8n output panel but LLM still receives “”
  • schema mode “list” vs “name” in the Postgres node — no change
    promptType: define with explicit {{ $(‘When chat message received’).item.json.chatInput }} — no effect
    I’ve compared the JSON of both workflows and they look identical to me. Same node types, same typeVersions, same parameters, same connections. Yet one works and the other doesn’t.
    My suspicion is that there’s some internal metadata or cached value that gets set when you build a workflow through the UI but is missing when created via API. Something like cachedResultName or some other field that’s not obvious in the JSON.
    Has anyone ran into this? Is there something the n8n UI sets behind the scenes that the API doesn’t? Would really appreciate any pointers, been stuck on this for a while now.
    Thanks!

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version: latest

  • Database (default: SQLite): postgre SQL

  • n8n EXECUTIONS_PROCESS setting (default: own, main):

  • Running n8n via (Docker, npm, n8n cloud, desktop app): docker on hostinger

  • Operating system: MAC OS

Left : working workflow created through GUI/
Right : exact same workflow created through API/Claude Code (not working)

Hi Welcome! this is almost certainly a connections issue in your JSON, when you create via API the AI sub-node connections need to use types like ai_tool, ai_languageModel, ai_memory not main, and they go from the sub-node to the Agent not the other way around. Export the working one and diff the connections object against the API one, i bet thats where the difference is.

Thanks for the suggestion! I checked the connections and they’re already using the correct types — ai_tool for the Postgres node and ai_languageModel for Gemini, both pointing from the sub-node to the Agent. That part looks fine in the JSON. The issue seems to be somewhere else, still digging :frowning:

1 Like

hi @Alexis_Tran

can you open the workflow settings on both and compare the execution logic value ?
Let me know what both values are !

1 Like

That was the problem, thank you!

Execution Logic for workflow via API was on V0! I didn’t know about that option! Thank you, I’ve been working on it since this morning.

1 Like

@Alexis_Tran glad it helped !!
If your issue is resolved, could you mark the answer as the solution? It helps others with the same problem find the fix faster. Thanks!

1 Like

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