ai icon indicating copy to clipboard operation
ai copied to clipboard

Missing tool_call_id

Open iGmainC opened this issue 1 year ago • 2 comments

Description

error message:

{
  url: 'https://api.ai-gaochao.cn/v1/chat/completions',
  requestBodyValues: {
    model: 'gpt-3.5-turbo-0125',
    logit_bias: undefined,
    logprobs: undefined,
    top_logprobs: undefined,
    user: undefined,
    parallel_tool_calls: undefined,
    max_tokens: undefined,
    temperature: 0.5,
    top_p: undefined,
    frequency_penalty: undefined,
    presence_penalty: undefined,
    seed: undefined,
    messages: [ [Object], [Object], [Object], [Object], [Object] ],
    tools: [ [Object] ],
    tool_choice: 'auto',
    stream: true,
    stream_options: undefined
  },
  statusCode: 400,
  responseHeaders: {
    connection: 'keep-alive',
    'content-length': '282',
    'content-type': 'application/json; charset=utf-8',
    date: 'Thu, 04 Jul 2024 04:34:00 GMT',
    server: 'nginx',
    'x-cache-lookup': 'Cache Miss, Cache Miss, Cache Miss',
    'x-nws-log-uuid': '13820491355925190028',
    'x-oneapi-request-id': '2024070412335989301426876979553'
  },
  responseBody: `{"error":{"message":"Missing parameter 'tool_call_id': messages with role 'tool' must have a 'tool_call_id'. (request id: 2024070404335997779905546548020) (request id: 20240704123359926885698l4WWc0Dk)","type":"invalid_request_error","param":"messages.[3].tool_call_id","code":null}}`,
  cause: undefined,
  isRetryable: false,
  data: {
    error: {
      message: "Missing parameter 'tool_call_id': messages with role 'tool' must have a 'tool_call_id'. (request id: 2024070404335997779905546548020) (request id: 20240704123359926885698l4WWc0Dk)",
      type: 'invalid_request_error',
      param: 'messages.[3].tool_call_id',
      code: null
    }
  }
}

Code example

server code:

import { createOpenAI } from "@ai-sdk/openai";
import { convertToCoreMessages, streamText } from "ai";
import { OPENAI_API_KEY, OPENAI_BASE_URL } from "@/config/constants";
import { z } from "zod";

const openai = createOpenAI({
  apiKey: OPENAI_API_KEY,
  baseURL: OPENAI_BASE_URL,
});

export async function POST(req: Request) {
  const { messages, config } = await req.json();

  let tmp = convertToCoreMessages(messages);
  const result = await streamText({
    model: openai(config.model),
    system: config.prompt ? config.prompt : undefined,
    temperature: config.temperature,
    messages: tmp,
    tools: {
      searchDB: {
        description: "xxx",
        parameters: z.object({
          query: z.string().describe("xxx"),
        }),
        execute: async ({ query }) => {
          return JSON.stringify(["xxx", "xxx"]);
        },
      },
    },
  });
  return result.toAIStreamResponse();
}

Additional context

"@ai-sdk/openai": "^0.0.34", "ai": "^3.2.16",

let tmp = convertToCoreMessages(messages);

[
  {
    role: "user",
    content: "xxx",
  },
  {
    role: "assistant",
    content: [
      {
        type: "text",
        text: "",
      },
      {
        type: "tool-call",
        toolCallId: "call_cnrX1hmJQfZjWs6WxbPRiKV8",
        toolName: "searchDB",
        args: {
          query: "xxx",
        },
      },
    ],
  },
  {
    role: "tool",
    content: [
      {
        type: "tool-result",
        toolCallId: "call_cnrX1hmJQfZjWs6WxbPRiKV8",
        toolName: "searchDB",
        args: {
          query: "xxx",
        },
        result: '["xxx"]',
      },
    ],
  },
];

iGmainC avatar Jul 04 '24 04:07 iGmainC

@iGmainC would like more information

  • are you seeing the error after sending the first message, or does it happen after sending a couple of messages?
  • are you seeing the error during the tool call, or after sending a message after a tool call has completed its execution?
  • are you seeing the error only when using your custom baseUrl, or are you also seeing it when using openai's default baseUrl?
messages: [ [Object], [Object], [Object], [Object], [Object] ],

Asking because the messages in your request body shows 5 messages while the example you provided only shows 3 messages.

jeremyphilemon avatar Jul 07 '24 19:07 jeremyphilemon

@jeremyphilemon

  1. Usually this error happens after the first message, but sometimes it doesn't happen, which is weird.
  2. The error is seen after the tool call has finished executing and sends a message.
  3. Because I am in China, I can only use GPT through a third-party API access point. I will try to access OpenAI directly to test it next.

In one of my most simplified demos, this error does not occur even if I use a third-party API port.

iGmainC avatar Jul 08 '24 01:07 iGmainC

Please check if this still happens with the latest versions of ai and the @ai-sdk/openai provider. We made several fixes in this area.

lgrammel avatar Dec 12 '24 15:12 lgrammel