Problem:
I’m facing a schema-related issue in my n8n AI Agent workflow while using the PostgreSQL tool.
Setup:
- AI Agent (LangChain)
- PostgreSQL tool connected (
Execute_a_SQL_query_in_Postgres) - Tool is supposed to execute SQL queries generated by the AI
- Using input mapping:
{{ $fromAI('query') }}
Problem:
I’m getting errors like:
- “invalid schema”
- “no schema found”
- tool is not receiving input properly
Because of this, the AI Agent is not executing the PostgreSQL tool and no database response is returned.
Instead, I only see system messages / AI reasoning output.
Expected behavior:
AI should generate a SQL query and pass it to the tool in this format:
{ "query": "SELECT * FROM products;" }
What I want to know:
- What is the correct way to define the input schema for the PostgreSQL tool in n8n AI Agent?
- Do I need to manually define the JSON schema for the “query” parameter?
- Why does
$fromAI('query')fail when schema is not properly set?
json code :
{
“nodes”: [
{
“parameters”: {
“model”: {
“__rl”: true,
“value”: “gpt-4o”,
“mode”: “list”,
“cachedResultName”: “gpt-4o”
},
“responsesApiEnabled”: false,
“options”: {}
},
“type”: “@n8n/n8n-nodes-langchain.lmChatOpenAi”,
“typeVersion”: 1.3,
“position”: [
272,
112
],
“id”: “80240fb0-0b9f-4736-a2f9-f05874a62e88”,
“name”: “OpenAI Chat Model”,
“credentials”: {
“openAiApi”: {
“id”: “ga3qqF8DxuYDoPMz”,
“name”: “OpenAI account”
}
}
},
{
“parameters”: {
“promptType”: “define”,
“text”: “={{ $json.chatInput }}”,
“options”: {
“systemMessage”: “You are an AI Database Assistant.\n\nYou MUST use the tool “Execute_a_SQL_query_in_Postgres” to answer all queries.\n\n\nYour job:\n- Understand user question\n- Generate SQL for the “products” table only\n- Execute SQL using the provided tool\n- Return ONLY final result from the tool\n\nRules:\n- Use only table: products\n- Use only columns: id, product_name, price, tracking_id, status\n- Never invent tables or columns\n- If no data found, return “Not Found”\n\nDo NOT return SQL as text.\nYou MUST use the PostgreSQL tool for every query.”
}
},
“type”: “@n8n/n8n-nodes-langchain.agent”,
“typeVersion”: 3.1,
“position”: [
480,
-80
],
“id”: “f904ab0b-8af4-42b7-b143-35dbfd290ddb”,
“name”: “AI Agent”
},
{
“parameters”: {
“options”: {}
},
“type”: “@n8n/n8n-nodes-langchain.chatTrigger”,
“typeVersion”: 1.4,
“position”: [
144,
-80
],
“id”: “54066fdd-b86f-49f8-a126-d1df3a608f15”,
“name”: “When chat message received”,
“webhookId”: “8154f63c-2a41-43b4-b919-512dcca80946”
},
{
“parameters”: {
“descriptionType”: “manual”,
“toolDescription”: “Execute SQL queries against the PostgreSQL database.\n\nAvailable table:\n\nproducts\n- id\n- product_name\n- price\n- tracking_id\n- status\n\nUse this tool when the user asks:\n\n- product list\n- product price\n- tracking status\n- product availability\n- product information\n\nExecute PostgreSQL queries.\n\nInput:\nquery = SQL query string\n”,
“operation”: “executeQuery”,
“query”: “{{ $fromAI(‘query’) }}”,
“options”: {}
},
“type”: “n8n-nodes-base.postgresTool”,
“typeVersion”: 2.6,
“position”: [
640,
128
],
“id”: “e1958ef6-e736-4ea3-b80a-0dbe69dd9ff1”,
“name”: “Execute a SQL query in Postgres1”,
“credentials”: {
“postgres”: {
“id”: “ipP4rrIMdw1yQjM4”,
“name”: “Postgres account”
}
}
}
],
“connections”: {
“OpenAI Chat Model”: {
“ai_languageModel”: [
[
{
“node”: “AI Agent”,
“type”: “ai_languageModel”,
“index”: 0
}
]
]
},
“When chat message received”: {
“main”: [
[
{
“node”: “AI Agent”,
“type”: “main”,
“index”: 0
}
]
]
},
“Execute a SQL query in Postgres1”: {
“ai_tool”: [
[
{
“node”: “AI Agent”,
“type”: “ai_tool”,
“index”: 0
}
]
]
}
},
“pinData”: {},
“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “126c751a1f4f9bb134cc288b6b2abddd2d54dcebc1b2f7a5031e97b151f2eaef”
}
}










