vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
9.2k stars 1.33k forks source link

Missing tool_call_id #2178

Open iGmainC opened 1 month ago

iGmainC commented 1 month ago

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"]',
      },
    ],
  },
];
jeremyphilemon commented 1 month ago

@iGmainC would like more information

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.

iGmainC commented 1 month ago

@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.