Trouble sending FunctionResponse back to model

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]: ... 

Hello,

You might find it helpful to go through the Function Calling with Gemini documentation. It could provide some guidance and help resolve your error.

If that doesn’t work, could you please share additional parts of your code, specifically the section where you are creating an instance of the Gemini API and making the function selection call? Having that will allow me to better understand and reproduce your issue.

This is how I create vertex instance:

this.genAI = new GoogleGenAI({ vertexai: true, project: 'my-project-here', location: 'global' }); 

This is how I make the function call:

 let response = await chat.sendMessage({ message: [textPart, ...fileParts], config: modelConfig(userRole, temperature) }); // 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; } } 

This is my model config:

import type { GenerateContentConfig, Tool } from '@google/genai'; import { Type } from '@google/genai'; import { generalResponseDirection, RoleSpecificResponseDirection, RoleSpecificDataGathering, interpretationDirection, interpretationStructuring } from './instructionTexts.ts'; import { modelFunctions } from './modelFunctions.ts'; export const models = { fast: 'gemini-2.5-flash-lite', basic: 'gemini-2.0-flash-exp', advanced: 'gemini-2.5-pro' }; export const modelConfig = ( userRole: string = 'customer', temperature: number = 0.55, candidateCount: number = 1 ): GenerateContentConfig => ({ temperature, candidateCount, tools: [RAG, FunctionDeclarations], systemInstruction: systemInstruction(userRole) }); const RAG: Tool = { retrieval: { vertexRagStore: { ragResources: [ { ragCorpus: 'my-corpus-here' } ], similarityTopK: 20 } } }; const FunctionDeclarations: Tool = { functionDeclarations: [ { name: 'getListOfTests', description: 'Retrieves a list of kit codes. Whenever user asks you about tests taken by him or his patients, this function must be called.' }, { name: 'getTestByKitCode', description: 'Retrieves a specific tests data (e.g., status, results) for a user when they provide a kit code. This function must be called whenever a user mentions a kit code, which is always in the format GA-XXXXXX (where X is string or number). Call the function even when user does not mention kit code but provides the code in GA-XXXXXX format.', parameters: { type: Type.OBJECT, properties: { kitCode: { type: Type.STRING, description: 'The unique kit code provided by the user (e.g., GA-123456).' } }, required: ['kitCode'] } } ] }; export const callModelFunction = async ( userId: string, userRole: string, name: string, args: Record<string, unknown> | undefined ) => { try { switch (name) { case 'getListOfTests': return await modelFunctions.getListOfTests(userId, userRole); case 'getTestByKitCode': return await modelFunctions.getTestByKitCode(userId, userRole, args?.kitCode as string); default: throw new Error(`Unknown model function: ${name}`); } } catch (error) { console.error('Error calling model function:', error); throw new Error('Failed to call model function'); } }; const systemInstruction = (userRole: string) => { let systemInstruction = ''; systemInstruction += generalResponseDirection(); systemInstruction += RoleSpecificResponseDirection(userRole); systemInstruction += RoleSpecificDataGathering(userRole); systemInstruction += interpretationDirection(); systemInstruction += interpretationStructuring(); return systemInstruction; }; 

Function implementations you don’t need I assume since they’re being called properly and return proper data as seen in console log of my original reply.

Bump; I’d really appreciate if somebody would take a closer look. I’ve read every single example there is and still have no idea what could be the issue.

Disabling the RAG when sending function response fixes the problem. Is this intended behaviour or should I make a github issue?