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!