I’m having trouble sending function response back to the model after receiving a function call.
I tried different response formats with no success. I don’t have an idea to what I might be doing wrong.
This is my function calling loop:
// Loop untill no more function calls const functionCalls = []; while (response.functionCalls && response.functionCalls.length > 0) { const { name, args } = response.functionCalls[0]; console.log('Function call detected:', name, args); if (name) { try { const result = await callModelFunction(userId, userRole, name, args); console.log('Function call result:', result); const record: Record<string, unknown> = { output: result }; const functionResponsePart: Part = { functionResponse: { name, response: record } }; functionCalls.push({ name, args, fnReturn: result }); response = await chat.sendMessage({ message: [functionResponsePart] }); } catch (error) { console.error('Failed to call model function:', error); throw new Error('Failed to call model function'); } } else { break; } }
and this is my console output:
Function call detected: getTestByKitCode { kitCode: 'GA-VKVK123' } Function call result: { name: 'someName', status: 'inAnalysis', kitCode: 'GA-VKVK123', gender: 'fromUserGender', ethnicity: 'FromUserEthnicity', dateOfBirth: '1998-02-11', country: 'someCountry', sampleDate: 'Thu Jan 02 2020 13:10:49 GMT+0100 (Central European Standard Time)' } Failed to call model function: ClientError: got status: 400 Bad Request. {"error":{"code":400,"message":"Request contains an invalid argument.","status":"INVALID_ARGUMENT"}} at throwErrorIfNotOK (file:///Users/hrvojekolaric/Desktop/chat-bot/node_modules/@google/genai/dist/node/index.mjs:11206:33) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async file:///Users/hrvojekolaric/Desktop/chat-bot/node_modules/@google/genai/dist/node/index.mjs:10980:13 at async Models.generateContent (file:///Users/hrvojekolaric/Desktop/chat-bot/node_modules/@google/genai/dist/node/index.mjs:12157:24) at async file:///Users/hrvojekolaric/Desktop/chat-bot/node_modules/@google/genai/dist/node/index.mjs:4232:30 at async Chat.sendMessage (file:///Users/hrvojekolaric/Desktop/chat-bot/node_modules/@google/genai/dist/node/index.mjs:4248:9) at async ChatbotServerService.sendMessage (/Users/hrvojekolaric/Desktop/chat-bot/src/lib/chatbotServer.ts:259:24) at async POST (/Users/hrvojekolaric/Desktop/chat-bot/src/routes/api/chat/[sessionId]/+server.ts:25:22) at async render_endpoint (/Users/hrvojekolaric/Desktop/chat-bot/node_modules/@sveltejs/kit/src/runtime/server/endpoint.js:48:20) at async resolve (/Users/hrvojekolaric/Desktop/chat-bot/node_modules/@sveltejs/kit/src/runtime/server/respond.js:379:22) { [cause]: ...