ai icon indicating copy to clipboard operation
ai copied to clipboard

Inconsistence tools call response with Azure Openai API

Open haifeng-amaysim opened this issue 10 months ago • 3 comments

Description

When using Azure openai package for tools function calls there is incorrect response in the tools call response type which Azure openai is not exactly the same as openai response, not sure if this should be handled in this ai package but will be good to flag this bug.

The issue will cause the response invalid to Azure Openai API calls as the response in the OpenAIStream is using "tool_call_id" while Azure API is expecting "toolCallId"

Will this inconsistence will be handled?

Code example

OpenAIStream response using "tool_call_id"

// https://github.com/vercel/ai/blob/main/packages/core/streams/openai-stream.ts#L640
                      // Append the function call result message
                      {
                        role: 'tool',
                        tool_call_id,
                        name: function_name,
                        content: JSON.stringify(tool_call_result),
                      },

But Azure is using "toolCallId" in their client

// Purely for convenience and clarity, this function handles tool call responses.
function applyToolCall({ function: call, id }) {
    if (call.name === "get_current_weather") {
      const { location, unit } = JSON.parse(call.arguments);
      // In a real application, this would be a call to a weather API with location and unit parameters
      return {
        role: "tool",
        content: `The weather in ${location} is 72 degrees ${unit} and sunny.`,
        toolCallId: id,
      }
    }
    throw new Error(`Unknown tool call: ${call.name}`);
}

Additional context

No response

haifeng-amaysim avatar Apr 17 '24 00:04 haifeng-amaysim

Also ran into this. Workaround is to manually pass both tool_call_id and toolCallId to my message object. Would be great if this wouldn't be necessary in the future 👍

toonverbeek avatar May 03 '24 09:05 toonverbeek

Also got error Missing parameter 'tool_call_id': messages with role 'tool' must have a 'tool_call_id'. when run with Azure OpenAI Service.

@toonverbeek Can you provide your workaround? I tried to pass toolCallId but still does not work:

appendToolCallMessage({
  // tool_call_id: toolCall.id,
  // @ts-ignore
  toolCallId: toolCall.id,
  function_name: 'search',
  tool_call_result: result,
})

gouku avatar May 12 '24 15:05 gouku

@gouku it's not in the appendToolCallMessage where you need to pass it, but when you're returning the messages from the tool:

const finalMessages = [
            ...messages.map((message) => ({
              content: message.content,
              role: message.role,
            })),
            ...newMessages.map((message) => ({
              ...message,
              content: message.content,
              role: message.role as any,
              // azureOpenaI generates pascal case keys, but vercel ai expects snake_case
              toolCallId: message.tool_call_id,
            })),
      ];

return client.streamChatCompletions(finalMessages);

toonverbeek avatar May 12 '24 17:05 toonverbeek

Superseeded by #1675

lgrammel avatar May 24 '24 17:05 lgrammel