Skip to content

Firebase Live API WebSocket Connection Closes on Function Calling #9264

@jsguy

Description

@jsguy

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

  1. 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 */]
  }]
});
  1. Start audio conversation with function calling handler:
const audioController = await startAudioConversation(session, {
  functionCallingHandler: customHandler
});
  1. Trigger any custom function call via voice or text input
  2. 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:

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions