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

Langchain调用OpenAI函数返回结果不一致问题咨询

Langchain调用OpenAI函数时响应格式不一致问题

使用Langchain调用OpenAI函数过程中,出现了响应格式不一致的情况:

  • 第一种场景:函数调用信息存在于additional_kwargs.function_call属性中,对应用户输入I want to order pizza,响应示例:
AIMessage {
  lc_serializable: true,
  lc_kwargs: {
    content: '',
    additional_kwargs: { function_call: [Object], tool_calls: undefined }
  },
  lc_namespace: [ 'langchain', 'schema' ],
  content: '',
  name: undefined,
  additional_kwargs: {
    function_call: {
      name: 'orderPizza',
      arguments: '{
  "pizzaSize": "NA",
  "pizzaType": "NA"
}'
    },
    tool_calls: undefined
  }
}
  • 第二种场景:additional_kwargs.function_call为undefined,函数调用信息以JSON形式存在于content属性中,对应用户输入Hey,响应示例:
AIMessage {
  lc_serializable: true,
  lc_kwargs: {
    content: '{
  "function": "functions.welcomeUser",
  "arguments": {}
}',
    additional_kwargs: { function_call: undefined, tool_calls: undefined }
  },
  lc_namespace: [ 'langchain', 'schema' ],
  content: '{
  "function": "functions.welcomeUser",
  "arguments": {}
}',
  name: undefined,
  additional_kwargs: { function_call: undefined, tool_calls: undefined }
}

两种场景下OpenAI都能正确识别要调用的函数,但响应格式不统一。我的函数Schema如下:

const greetUserSchema = {
    name: "welcomeUser",
    description:
      "This function is used for greeting users. Acknowledging the user with a welcome message.",
    parameters: {},
};

const pizzaOrderFunctionSchema = {
    name: "orderPizza",
    description:
      "This function is called to accept the pizza orders. If the pizza size or the pizza type is not mentioned then pass the value as NA in the parameters.",
    parameters: {
      type: "object",
      properties: {
        pizzaSize: {
          type: "string",
          description: "The size of the pizza",
          enum: ["NA", "small", "medium", "large"],
        },
        pizzaType: {
          type: "string",
          description: "The type of pizza",
          enum: ["NA", "cheese", "pepperoni", "veggie"],
        },
      },
      required: ["pizzaSize", "pizzaType"],
    },
};

想了解这种情况是否属于预期现象,还是代码存在bug导致的格式不一致?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 08:13:18