You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenAI Chat Completions API请求报错:functions参数格式问题求助

解决OpenAI Chat Completions API的functions参数报错问题

问题根源

OpenAI Chat Completions API对functions和function_call参数有明确规则:

  • 不需要使用函数调用功能时,直接省略这两个参数,而非设置为null或空数组[]。
  • 若传入functions参数,必须是包含至少一个有效函数定义的数组;搭配function_call时,需与合法的functions配置匹配。

正确请求示例

无需函数调用时,移除functions和function_call字段即可:

{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "system",
            "content": "etc"
        },
        {
            "role": "user",
            "content": "etc"
        }
    ],
    "temperature": null,
    "top_p": null,
    "n": null,
    "stream": null,
    "stop": null,
    "max_tokens": null,
    "user": null
}

过往请求报错原因

  1. 第一个请求functions: null:API要求functions必须是数组类型,不接受null值。
  2. 第二个请求functions: []且function_call: "none":传入空数组会被判定为启用函数功能但未提供有效定义,因此触发“数组过短”报错。

函数调用场景的正确示例

若确实需要使用函数调用,functions数组需包含至少一个符合格式的函数定义:

{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "system",
            "content": "etc"
        },
        {
            "role": "user",
            "content": "etc"
        }
    ],
    "functions": [
        {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, e.g. San Francisco, CA"
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"]
                    }
                },
                "required": ["location"]
            }
        }
    ],
    "function_call": "auto"
}

内容的提问来源于stack exchange,提问作者Luke Puplett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 06:34:50