Unity & Gemini Live API: WebSocket closed with InternalServerError after sending setup messag

Hello everyone,

I’m developing a Unity application that integrates with the Gemini Realtime API via WebSockets (wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1alpha.GenerativeService.BidiGenerateContent).

I’m able to connect to the WebSocket server successfully, but the connection is immediately closed right after I send the initial setup message. The server returns an InternalServerError.

Here are the relevant logs from the Unity console:

// Successful Connection
Connecting to: wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1alpha.GenerativeService.BidiGenerateContent?key=...
✅ Connected to Gemini Live API

// Sending setup message
🔧 Sending complete and fixed setup message (Audio: False): {"setup":{"generationConfig":{"responseModalities":["TEXT"],"temperature":0.7,"maxOutputTokens":1000,"topP":0.9,"topK":40,"candidateCount":1},"systemInstruction":{"parts":[{"text":"You are a helpful assistant and answer in a friendly tone."}]},"safetySettings":[{"category":"HARM_CATEGORY_HARASSMENT","threshold":"BLOCK_MEDIUM_AND_ABOVE"},{"category":"HARM_CATEGORY_HATE_SPEECH","threshold":"BLOCK_MEDIUM_AND_ABOVE"},{"category":"HARM_CATEGORY_SEXUALLY_EXPLICIT","threshold":"BLOCK_MEDIUM_AND_ABOVE"},{"category":"HARM_CATEGORY_DANGEROUS_CONTENT","threshold":"BLOCK_MEDIUM_AND_ABOVE"}],"tools":[]}}
✅ Setup message sent successfully

// Server closes the connection
WebSocket connection closed by server. Status: InternalServerError, Description: Internal error encountered.
UnityEngine.Debug:LogWarning (object)
UnityAI.Networking.WebSocketClient/<ReceiveLoop>d__14:MoveNext () (at Assets/Scripts/Runtime/Utilities/Networking/WebSocketClient.cs:57)

🔌 WebSocket connection closed by server

This is the JSON I send formatted:

{
  "setup": {
    "generationConfig": {
      "responseModalities": [
        "TEXT"
      ],
      "temperature": 0.7,
      "maxOutputTokens": 1000,
      "topP": 0.9,
      "topK": 40,
      "candidateCount": 1
    },
    "systemInstruction": {
      "parts": [
        {
          "text": "You are a helpful assistant and answer in a friendly tone."
        }
      ]
    },
    "safetySettings": [
      {
        "category": "HARM_CATEGORY_HARASSMENT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      },
      {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      },
      {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      },
      {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_MEDIUM_AND_ABOVE"
      }
    ],
    "tools": []
  }
}

Has anyone experienced a similar issue or can spot an error in my setup payload? Any help would be greatly appreciated.

Thanks!

The InternalServerError message usually indicates server-side problems. I get the same thing from time to time, but I try again after a few minutes and the problem goes away.

If the problem persists, try removing the safetySettings and tools from your payload. Also, when I set the temperature with the Live API, the server told me that the option wasn’t supported. Remember, the Live API is still in preview. Try leaving only the responseModalities in the generationConfig. Then try adding one parameter at a time.

Ciao

I have tried as well what you say but even in that case the web socket get closed instantly, also have tried multiple times to make sure is not a time to time thing. I tried with this json, with a JSON just with responseModalities.

{
  "setup": {
    "generationConfig": {
      "responseModalities": [
        "TEXT"
      ]
    },
    "systemInstruction": {
      "parts": [
        {
          "text": "You are a helpful assistant and answer in a friendly tone."
        }
      ]
    }
  }
}

Hi Pablo,
I’m sorry, but I haven’t checked your JSON payload carefully. I’ll start by saying that I don’t know what it is Unity, so my explanation might be silly, but I don’t see the "model": key in the JSON payload. As far as I know, the JSON payload should look something like this:

{
  "setup": {
    "model": "models/gemini-live-2.5-flash-preview",
    "generationConfig": {
      "responseModalities": [
        "TEXT"
      ]
    },
    "systemInstruction": {
      "parts": [
        {
          "text": "any message..."
        }
      ]
    }
  }
}

Try this, then, if it works, try adding the rest of your configuration as well.
I hope I’ve been more helpful this time.

Ciao

Hello, may I ask if your API has been successfully integrated? Can you share a demo?