N8n AI Agent PostgreSQL 工具顯示無效/遺失的結構描述錯誤(查詢輸入無法運作)

問題:
在使用 PostgreSQL 工具時,我在 n8n AI Agent 工作流程中遇到了一個與架構相關的問題。

:gear: 設定:

  • AI Agent (LangChain)
  • PostgreSQL 工具已連接 (Execute_a_SQL_query_in_Postgres)
  • 工具應執行 AI 生成的 SQL 查詢
  • 使用輸入映射:{{ $fromAI('query') }}

:red_exclamation_mark: 問題:

我收到以下錯誤:

  • 「invalid schema」
  • 「no schema found」
  • 工具無法正確接收輸入

因此,AI Agent 無法執行 PostgreSQL 工具,也無法傳回資料庫回應。

相反地,我只看到系統訊息 / AI 推理輸出。


:magnifying_glass_tilted_left: 預期行為:

AI 應生成 SQL 查詢並以此格式傳遞給工具:


{ "query": "SELECT * FROM products;" }

:red_question_mark: 我想知道的事項:

  • 在 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”
}
}

嗨 @Hassan_Zakria

你的 $fromAI() 呼叫遺漏了 description 和 type 參數。

將你的 PostgreSQL 工具的查詢欄位從:

{{ $fromAI('query') }}

改為:

{{ $fromAI('query', 'A valid PostgreSQL SELECT query to execute against the products table', 'string') }}

description 告訴 AI Agent 輸入應該是什麼格式,這就是它生成工具輸入架構的方式。沒有它的話,架構生成就會失敗。

請讓我知道這是否有幫助 :crossed_fingers:

我試過了但同樣的錯誤仍然發生

錯誤的請求 - 請檢查您的參數

函數 ‘Execute_a_SQL_query_in_Postgres1’ 的無效結構描述:結構描述必須是類型為 “object” 的 JSON Schema,但收到的是 ‘type: “None”’。

嗨 @Hassan_Zakria,houda_ben 上面的修復是正確的第一步。如果你新增描述和類型但仍然拋出「got None」,還有兩件事要做:保持直引號,不要用花引號,$fromAI 中的花引號會無聲地中斷解析,所以沒有參數會被註冊。並且使用 ‘string’ 類型,而不是 ‘json’(json 類型會建立一個格式不正確的架構,issue #18588)。

如果正確的表達式仍然失敗,這是一個已知的迴歸(#29353),其中代理工具在較新的 2.x 版本上會發出確切的「got None」,但在 ~2.15 上可以工作。你使用的是哪個 n8n 版本?

版本:
2.26.4(自我託管)

@Hassan_Zakria 那確認了。在 2.26.4 版本上,即使表達式正確仍然拋出「got None」,這是開放的回歸問題 #29353,不是你的配置。在這些 2.x 版本上,Postgres 工具就是無法發出有效的參數 schema,所以沒有任何 $fromAI 調整能修復它。

兩個解決方向:

  1. 最乾淨的做法,無需降級:將 SQL 移到一個小的子工作流(一個「query」輸入,然後是你的 Postgres 節點),然後將其作為 Call n8n Workflow Tool 附加到代理,而不是直接使用 Postgres 工具。那個節點讓你明確定義輸入 schema,所以它會發送有效的物件 schema 並繞過這個 bug。

  2. 或者將 n8n 固定在 v2.15,即問題報告者確認可用的版本。

值得訂閱 #29353 以取得真正的修復。

好的,我試試子節點方法


問題:

agent 現在寫入查詢,detect tool 已發送,但之後產生錯誤
可能是 2 個節點之間的整合問題
執行「n8n subworkflow」節點
和「When Executed by Another Workflow」

{“query”: “SELECT * FROM products;”}

然後 n8n 會推斷出真正的 “query” 欄位,Postgres 節點中的 {{ $json.query }} 就能正確填充。(或者改用「使用下方欄位定義」的輸入數據模式,並新增一個欄位 query,類型為 string。)你之前的架構應該放在 Call Workflow Tool 的輸入端,而不是觸發器。

ok i try this further

我注意到當我在 JSON 範例輸入欄位中沒有新增屬性時
就會出現這個無效的結構描述訊息
可能是因為我沒有定義 type: object 和 required 查詢?

是的,那個截圖說清楚了,Call Workflow Tool 也會拋出完全相同的「must be type object, got None」錯誤,所以改用它其實也沒躲過什麼,我那邊說得不準。這個 bug 是 n8n 在 2.26.x 版本上為模型建置任何工具 schema 的方式有問題,不只是 postgres 工具。不過你的子工作流程佈線本身沒問題,一旦那個 bug 修復了就能運作。

現在有兩個解決方案。簡單的方法是完全跳過代理工具,讓 Message a Model 節點直接寫 SQL(把表格規則放在系統提示中),然後直接餵給普通的 Postgres 節點。沒有工具就沒有破損的 schema,所以在 2.26.4 上運作正常,你只是失去代理迴圈,不過對於純粹的問題轉 SQL,你其實不太需要。要不然就降級到 2.15,那邊還能運作,保留代理和工具。