Firebase Live API WebSocket Connection Closes on Function Calling
Closed this issue ยท 7 comments
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 bugVoice 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:
- googleapis/python-genai#803: "Critical Issue: Gemini Live API Hangs on Function Call Attempts"
- pipecat-ai/pipecat#1564: "Long-Running Function Calls Break Tool Response Processing"
- googleapis/nodejs-vertexai#426: "400 INVALID_ARGUMENT error with Function Calling"
- google-gemini/gemini-cli#2676, #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.
I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
Hi @jsguy thanks for reporting this detailed issue! This is definitely a bug. This issue only seems to occur when using the Gemini Developer API- Vertex AI Gemini API behaves as expected for me. I'll begin looking into whether this is a backend issue, or if it's an issue with how we're sending the function response to the Gemini Developer API backend.
This is a bug in our SDK, thanks for catching it!
This should be fixed after #9272. The PR includes an explanation of what's wrong and how we're fixing it.
Since you're one of the early users of the Live API in the JS SDK, I'm really interested to hear any feedback you have on the API (what's annoying to use, confusing, buggy, missing, etc.). Please share any feedback you have in this thread, or in new issue(s), or email me dlarocque@google.com.
@dlarocque - I emailed you - also I see great progress has been made on the other issue - I'd be happy to help test a build, if that would help move it along?
@dlarocque - I emailed you - also I see great progress has been made on the other issue - I'd be happy to help test a build, if that would help move it along?
Thank you! The fix should be going into the next release. We publish a staging release two days before production releases. Would you be able to confirm the fix using this staging release? I could notify you when it's out. firebase@next is the version tag.
@dlarocque I've verified the fix, and it's working well in firebase@next - thank you so much for fixing it so quickly! ๐