OpenAI compatibility with function calling not response message

I’m currently testing tool-call integration using the OpenAI SDK with the Gemini API. Below is the workflow I’ve implemented and the issue I encountered:
Workflow:

  1. Step 1: Send a user message to Gemini. :white_check_mark: (Completed)

  2. Step 2: Handle the tool-call response by executing the specified function. :white_check_mark: (Completed)

  3. Step 3: Send the tool-call result back to Gemini for further processing. :white_check_mark: (Completed)

  4. Step 4 (Issue): I expect to receive a text message as the final response from Gemini. :x:

Actual result: Instead of a text message, Gemini returns another tool-call request.

 try {
      const completion = await clientAi.chat.completions.create({
        model: 'gemini-1.5-flash',
        messages: [...messages, userMessage],
        tools,
        tool_choice: 'auto',
      });
      const choice = completion.choices[0];
      const toolCalls = choice.message.tool_calls;
      if (toolCalls && toolCalls.length > 0) {
        const newMessages: ChatCompletionMessageParam[] = [
          ...messages,
          {
            ...choice.message,
            content: 'Calling tools...',
          },
          userMessage,
        ];
        for (const toolCall of toolCalls) {
          const name = toolCall.function.name;
          const args = JSON.parse(toolCall.function.arguments);
          const result = await callFunction(name, args);
          newMessages.push({
            role: 'tool',
            tool_call_id: toolCall.id,
            content: result.toString(),
          });
        }
        const fcCompletion = await clientAi.chat.completions.create({
          model: 'gemini-1.5-flash',
          messages: newMessages,
          tools,
        });
        const fcChoice = fcCompletion.choices[0];
        console.log('Function completion---:', fcChoice);
        console.log('Function completion message:', fcChoice.message);
        const fcBotMessage = fcChoice.message?.content || 'No response from function completion';
        setMessages((prev) => [...prev, { role: 'assistant', content: fcBotMessage }]);
        return;
      }
      const botMessage = choice.message?.content || 'No response';
      setMessages((prev) => [...prev, { role: 'assistant', content: botMessage }]);
    } catch (error: any) {
      console.log('Error occurred while sending message', error);
      setMessages((prev) => [...prev, { role: 'assistant', content: 'Error occurred!' }]);
    } finally {
      setLoading(false);
    }

image

I am experiencing the same problem as well.

2 Likes

same problem here, but it works if the model is gemini-1.5-pro somehow