問題:
在使用 PostgreSQL 工具時,我在 n8n AI Agent 工作流程中遇到了一個與架構相關的問題。
設定:
- AI Agent (LangChain)
- PostgreSQL 工具已連接 (
Execute_a_SQL_query_in_Postgres) - 工具應執行 AI 生成的 SQL 查詢
- 使用輸入映射:
{{ $fromAI('query') }}
問題:
我收到以下錯誤:
- 「invalid schema」
- 「no schema found」
- 工具無法正確接收輸入
因此,AI Agent 無法執行 PostgreSQL 工具,也無法傳回資料庫回應。
相反地,我只看到系統訊息 / AI 推理輸出。
預期行為:
AI 應生成 SQL 查詢並以此格式傳遞給工具:
{ "query": "SELECT * FROM products;" }
我想知道的事項:
- 在 n8n AI Agent 中為 PostgreSQL 工具定義輸入架構的正確方法是什麼?
- 我需要手動為「query」參數定義 JSON 架構嗎?
- 為什麼當架構設定不正確時,
$fromAI('query')會失敗?
json 程式碼:
{
“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”: “對 PostgreSQL 資料庫執行 SQL 查詢。\n\n可用的表:\n\nproducts\n- id\n- product_name\n- price\n- tracking_id\n- status\n\n在使用者提出下列問題時使用此工具:\n\n- 產品清單\n- 產品價格\n- 追蹤狀態\n- 產品可用性\n- 產品資訊\n\n執行 PostgreSQL 查詢。\n\n輸入:\nquery = SQL 查詢字串\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”
}
}










