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:
-
Step 1: Send a user message to Gemini.
(Completed)
-
Step 2: Handle the tool-call response by executing the specified function.
(Completed)
-
Step 3: Send the tool-call result back to Gemini for further processing.
(Completed)
-
Step 4 (Issue): I expect to receive a text message as the final response from Gemini.
• 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);
}