Invalid JSON payload received. Unknown name "ref" at 'tools[0].function_declarat...

2026年01月13日 00:26 状态: processing

🚨 错误信息

Invalid JSON payload received. Unknown name "ref" at 'tools[0].function_declarations[0].***.properties[0].***.***.properties[2].***.items': Cannot find field. Invalid JSON payload received. Unknown name "ref" at 'tools[0].function_declarations[0].***.properties[0].***.items': Cannot find field.

🤖 AI解决方案

错误分析报告

1. 错误翻译

原文: Invalid JSON payload received. Unknown name "ref" at 'tools[0].function_declarations[0]..properties[0]...properties[2]..items': Cannot find field.
译文: 接收到了无效的JSON负载。在路径 'tools[0].function_declarations[0]..properties[0]...properties[2]..items' 处发现了未知的名称 "ref":无法找到该字段。

原文: Invalid JSON payload received. Unknown name "ref" at 'tools[0].function_declarations[0]..properties[0]..items': Cannot find field.
译文: 接收到了无效的JSON负载。在路径 'tools[0].function_declarations[0]..properties[0]..items' 处发现了未知的名称 "ref":无法找到该字段。

2. 原因分析

这是一个典型的JSON Schema或API请求结构验证错误。您正在使用的API(很可能是Google AI或Gemini API)期望接收一个严格遵循其函数声明(Function Declarations)结构的JSON对象。

核心问题:

  • 您在 items 属性中使用了 "ref" 字段来引用另一个模式(Schema),但目标API不支持这种引用方式。

  • 该API的JSON Schema验证器无法识别 "ref" 作为 items 属性的有效子字段。
  • 上下文定位: 错误发生在您的 tools 数组的第一个元素的 function_declarations 中,具体涉及到参数或返回值的嵌套对象属性(properties)里的数组项(items)定义。

    3. 解决方案

    您需要修改JSON Schema定义,使用API支持的方式来定义嵌套或引用的数据类型。通常这意味着需要将引用的结构内联展开,而不是使用 $ref

    假设原始有问题的JSON片段如下:

    json
    {
    "tools": [{
    "function_declarations": [{
    "parameters": {
    "type": "OBJECT",
    "properties": {
    "myArray": {
    "type": "ARRAY",
    "items": {
    "$ref": "#/definitions/MyItemType" // 这是无效的
    }
    }
    }
    }
    }]
    }]
    }

    修复后的代码:

    json
    {
    "tools": [{
    "function_declarations": [{
    "parameters": {
    "type": "OBJECT",
    "properties": {
    "myArray": {
    "type": "ARRAY",
    "items": {
    // 直接将 MyItemType 的结构内联定义在这里
    "type": "OBJECT",
    "properties": {
    "id": {
    "type": "STRING",
    "description": "项目的唯一标识符"
    },
    "name": {
    "type": "STRING",
    "description": "项目名称"
    }
    // ... 其他 MyItemType 应有的属性
    },
    "required": ["id"]
    }
    }
    }
    }
    }]
    }]
    }

    4. ️ 预防措施

  • 仔细查阅官方文档:

  • - 在使用特定API(如Gemini Function Calling)时,务必查阅其最新的官方文档中关于如何定义函数参数Schema的部分。
    - 确认其支持的JSON Schema子集,特别注意其对 $refdefinitions 等高级特性的支持情况。许多API的Schema实现是受限的。

  • ️ 使用Schema验证工具:

  • - 在发送请求前,可以使用JSON Schema验证库(如 ajv for Node.js)或在线验证器,先用API提供的官方Schema来验证您构建的Schema是否正确。
    - 这可以提前发现结构错误,避免向API发送无效请求。