/MainProcess] 22208184909825/agent-e80a0 agent invoke chat to retry again llm er...

2026年06月22日 21:57 27.34 秒 success

错误信息

[2025-09-03 11:10:25,969: ERROR/MainProcess] 22208184909825/agent-e80a0 agent invoke chat to retry again llm error: Error code: 400 - {'error': {'message': "Invalid schema for function 'self_intent_recognition_0': None is not of type 'array'. (request id: 20250903110948261917640BBDOPt2p)", 'type': 'openai_error', 'param': '', 'code': 'invalid_function_parameters'}}

AI 解决方案

🔍 分错分析

1. 🌐 错误翻译


原文:Invalid schema for function 'self_intent_recognition_0': None is not of type 'array'.
译文:函数‘self_intent_recognition_0’的模式无效:某个应为数组(array)类型的值不能为None。

2. 🎯 原因分析


  • 根本原因:调用LLM API(如OpenAI)时,传递的函数(function/tools)定义格式不正确。

  • 具体问题:在函数self_intent_recognition_0parameters定义中,某个期望是数组(Array)类型的属性(例如enum, items, requiredtype本身)的值被设置为了None(或null),而非一个有效的数组。

  • 常见场景:这通常发生在动态构建函数定义的代码中,某个列表字段未被正确初始化或赋值。
  • 3. 💡 解决方案


    检查并修正传递给LLM的函数定义结构。确保所有应为数组的字段都正确定义。

    错误示例(可能出错的代码片段):

    tools = [{
        "type": "function",
        "function": {
            "name": "self_intent_recognition_0",
            "description": "识别用户意图",
            "parameters": {
                "type": "object",
                "properties": {
                    "intent": {
                        "type": "string",
                        "enum": None,  # ❌ 错误:enum 应为数组,不能是 None
                        "description": "意图类型"
                    }
                },
                "required": ["intent"]  # ✅ 正确:这是一个数组
            }
        }
    }]
    

    修复示例(正确的代码结构):

    tools = [{
        "type": "function",
        "function": {
            "name": "self_intent_recognition_0",
            "description": "识别用户意图",
            "parameters": {
                "type": "object",
                "properties": {
                    "intent": {
                        "type": "string",
                        "enum": ["greeting", "farewell", "query"],  # ✅ 修复:提供一个有效的字符串数组
                        "description": "意图类型"
                    }
                },
                "required": ["intent"]
            }
        }
    }]
    
    # 然后使用正确的 tools 定义调用 API
    response = client.chat.completions.create(
        model="your-model",
        messages=messages,
        tools=tools,  # 使用修正后的定义
        tool_choice="auto"
    )
    

    4. 🛡️ 预防措施


  • 构建后验证:在将函数定义发送给API之前,使用 json.dumps() 将其序列化为字符串,确保没有 None 值,并使用在线JSON校验工具检查其结构。

  • 使用类型检查:在代码中为函数定义添加简单的检查逻辑。

  •    # 简单检查示例
       for tool in tools:
           params = tool['function'].get('parameters', {})
           for prop_name, prop_def in params.get('properties', {}).items():
               for key in ['enum', 'items']:
                   if key in prop_def and prop_def[key] is None:
                       raise ValueError(f"参数'{prop_name}'的'{key}'字段不能为None,请检查。")