-
Notifications
You must be signed in to change notification settings - Fork 976
Description
Operating System
macOS (Darwin 24.6.0)
Environment (if applicable)
Chrome/Safari/Firefox (affects all browsers)
Firebase SDK Version
12.2.1
Firebase SDK Product(s)
AI
Project Tooling
Netxjs Typescript react web app with:
"firebase": "^12.2.1",
"@genkit-ai/googleai": "^1.19.2",
"@genkit-ai/next": "^1.19.2",
"next": "15.3.3",
"react": "^18.3.1"
Detailed Problem Description
When using Firebase Live API with custom function calling enabled, WebSocket connections close immediately after function execution with the error:
WebSocket connection closed by server. Reason: 'Request contains an invalid argument.'
The function calls themselves execute successfully, but the WebSocket terminates before the model can generate an audio response, breaking the voice interaction flow.
Steps and code to reproduce issue
- Initialize Firebase Live API with custom function declarations:
const model = getLiveGenerativeModel(ai, {
model: "gemini-live-2.5-flash-preview",
generationConfig: {
responseModalities: [ResponseModality.AUDIO]
},
tools: [{
functionDeclarations: [/* custom functions */]
}]
});
- Start audio conversation with function calling handler:
const audioController = await startAudioConversation(session, {
functionCallingHandler: customHandler
});
- Trigger any custom function call via voice or text input
- Observe that:
- Function executes successfully (logs show completion)
- WebSocket connection immediately closes
- No audio response is generated
- Session becomes unresponsive
Expected Behavior
- Function call executes successfully
- WebSocket connection remains open
- Model receives function result and generates audio response
- Conversation continues normally
Actual Behavior
- Function call executes successfully
- WebSocket connection closes with "Request contains an invalid argument"
- No audio response generated
- Session requires restart
Error Logs
live-api-chat.ts:256 📤 Returning structured function response for model to process: {functionResponse: {…}}
logger.ts:115 [2025-09-19T09:13:52.553Z] @firebase/vertexai: WebSocket connection closed by server. Reason: 'Request contains an invalid argument.'
websocket.ts:112 Uncaught (in promise) FirebaseError: AI: WebSocket is not open. (AI/request-error)
at WebSocketHandlerImpl.send (websocket.ts:112:13)
at live-session.ts:118:29
at Array.forEach (<anonymous>)
at LiveSession.sendMediaChunks (live-session.ts:114:17)
at AudioConversationRunner.deps.workletNode.port.onmessage (live-session-helpers.ts:187:29)
Function Calling Handler Code
function createFunctionCallingHandler(
toolRegistry: Map<string, ClientTool | SelfContainedTool>,
context: ToolExecutionContext
) {
return async (toolCall: any): Promise<Part> => {
const functionCalls = toolCall.functionCalls || toolCall;
const firstFunctionCall = functionCalls[0];
// Execute function (this works correctly)
const tool = toolRegistry.get(firstFunctionCall.name);
const result = await tool.execute(firstFunctionCall.args || {}, context);
// Return function response (this causes WebSocket closure)
return {
functionResponse: {
name: firstFunctionCall.name,
response: {
result: responseText
}
}
};
};
}
Workaround
Currently working around the issue by disabling function calling in Live API mode:
tools: undefined, // Disabled due to WebSocket closure bug
Voice mode works perfectly for conversations without custom tools. Built-in tools (like calculator) work fine.
Related Issues
This appears to be part of a broader issue affecting multiple Gemini Live API implementations:
- Critical Issue: Gemini Live API (gemini-2.0-flash-live-001) Hangs on Function Call Attempts googleapis/python-genai#803: "Critical Issue: Gemini Live API Hangs on Function Call Attempts"
- Bug: Long-Running Function Calls Break Tool Response Processing in Gemini Live Realtime [Websockets] pipecat-ai/pipecat#1564: "Long-Running Function Calls Break Tool Response Processing"
- 400 INVALID_ARGUMENT error when attempting to use Function Calling with Vertex AI Search retrieval tool googleapis/nodejs-vertexai#426: "400 INVALID_ARGUMENT error with Function Calling"
- API Error: Request contains an invalid argument, wondering what that is? google-gemini/gemini-cli#2676, V9.0.0-beta.3 cannot get string value form key #5030: "Request contains an invalid argument"
Timeline
- Issue started: ~May 13, 2025 around 4:00 AM UTC+3 (reported in related issues)
- Current status: Consistently reproducible across different implementations
- Affects: Multiple frameworks (Firebase, direct API, CLI tools)
Additional Context
The issue appears to be in the WebSocket message validation on the server side when processing function response parts. The Firebase implementation may be sending function responses in a format that the Gemini Live API WebSocket handler doesn't properly validate.
Built-in tools (calculator, etc.) work correctly, suggesting the issue is specific to custom function calling implementations rather than the core Live API functionality.