# Gemini Live WebSocket

Gemini Live WebSocket

## Endpoint and authentication

`GET https://api.tu-zi.com/v1beta/live?model={model}`

Transport: websocket

Use an inference API key. Examples contain placeholders; read credentials from environment variables.

```http
Authorization: Bearer YOUR_API_KEY
```

## Query parameters

| Field | Type | Required | Description / constraints |
| --- | --- | --- | --- |
| model | string | Yes | Selected model identifier. Example: YOUR_MODEL |

## Minimal request example

```json
{
  "setup": {
    "model": "models/YOUR_MODEL",
    "generationConfig": {
      "responseModalities": [
        "AUDIO"
      ]
    }
  }
}
```

## Before running

Replace https://api.tu-zi.com with this site origin (do not append /v1 twice), and YOUR_MODEL with the exact marketplace model ID. Install requests for Python; use Node.js 20+ and save JavaScript as .mjs. WebSocket samples list their own dependencies.

Create an inference key on the site and set API_KEY in the environment; replace YOUR_API_KEY in cURL. Prepare local input.png/input.wav files for upload examples.

Start with a minimal request, then read the result using the response fields below. For async APIs, a returned task ID requires polling; acceptance does not mean generation has finished.

## Code examples

### curl

```bash
# Gemini Live requires a WebSocket client.
# See the Python or JavaScript example and /docs/api/gemini.
```

### javascript

```javascript
// Node.js: npm install ws
import WebSocket from 'ws';

const ws = new WebSocket("wss://api.tu-zi.com/v1beta/live?model=YOUR_MODEL", {
  headers: { Authorization: "Bearer " + process.env.API_KEY },
  perMessageDeflate: false,
  maxPayload: 2 * 1024 * 1024,
});
let completed = false, sent = false, receivedOutput = false, failed = false;
function fail(error) {
  if (failed) return;
  failed = true;
  console.error(error);
  process.exitCode = 1;
  ws.terminate();
}
const timeout = setTimeout(() => fail(new Error('Timed out before a complete turn')), 120000);
ws.on('close', () => {
  clearTimeout(timeout);
  if (!completed && !failed) fail(new Error('Connection closed before a complete turn'));
});
ws.on('error', fail);
ws.on('open', () => ws.send(JSON.stringify({
  "setup": {
    "model": "models/YOUR_MODEL",
    "generationConfig": {
      "responseModalities": [
        "AUDIO"
      ]
    }
  }
})));
ws.on('message', (message) => {
  if (failed || completed) return;
  try {
    const event = JSON.parse(message.toString());
    if (event.error) throw new Error(JSON.stringify(event.error));
    if (event.setupComplete && !sent) {
      sent = true;
      ws.send(JSON.stringify({"clientContent":{"turns":[{"role":"user","parts":[{"text":"Say hello briefly."}]}],"turnComplete":true}}));
    }
    const content = event.serverContent || {};
    if (content.interrupted) throw new Error('Turn was interrupted; no complete result');
    const text = content.outputTranscription?.text;
    if (text) { receivedOutput = true; process.stdout.write(text); }
    // Consume content.modelTurn.parts[].inlineData for PCM audio.
    if (content.modelTurn?.parts?.some(part => part.inlineData?.data || part.text)) receivedOutput = true;
    if (content.turnComplete) {
      if (!sent || !receivedOutput) throw new Error('Turn ended without model output');
      completed = true;
      console.log(event.usageMetadata);
      ws.close();
    }
  } catch (error) { fail(error); }
});
```

### python

```python
# Python 3.11+: pip install 'websockets>=14,<16'
import asyncio, json, os
from websockets.asyncio.client import connect

async def main():
    async with asyncio.timeout(120):
        async with connect("wss://api.tu-zi.com/v1beta/live?model=YOUR_MODEL", additional_headers={
            'Authorization': "Bearer " + os.environ["API_KEY"]
        }, max_size=2*1024*1024, max_queue=4, compression=None) as ws:
            completed = sent = received_output = False
            await ws.send("{\"setup\":{\"model\":\"models/YOUR_MODEL\",\"generationConfig\":{\"responseModalities\":[\"AUDIO\"]}}}")
            async for message in ws:
                event = json.loads(message)
                if 'error' in event:
                    raise RuntimeError(event['error'])
                if 'setupComplete' in event and not sent:
                    sent = True
                    await ws.send("{\"clientContent\":{\"turns\":[{\"role\":\"user\",\"parts\":[{\"text\":\"Say hello briefly.\"}]}],\"turnComplete\":true}}")
                content = event.get('serverContent', {})
                if content.get('interrupted'):
                    raise RuntimeError('Turn was interrupted; no complete result')
                text = content.get('outputTranscription', {}).get('text', '')
                if text:
                    received_output = True
                    print(text, end='', flush=True)
                # Consume content.modelTurn.parts[].inlineData for PCM audio.
                if any(part.get('inlineData', {}).get('data') or part.get('text') for part in content.get('modelTurn', {}).get('parts', [])):
                    received_output = True
                if content.get('turnComplete'):
                    if not sent or not received_output:
                        raise RuntimeError('Turn ended without model output')
                    completed = True
                    print(event.get('usageMetadata', {}))
                    break
            if not completed:
                raise RuntimeError('Connection closed before a complete turn')

asyncio.run(main())
```

## Response fields

| Field | Type | Description / constraints |
| --- | --- | --- |
| setupComplete | object | Setup-complete event. |
| serverContent.modelTurn.parts[] | object | Model audio or other content parts. |
| serverContent.turnComplete | boolean | Turn completion. |

Illustrative response; some endpoints show essential fields only. Optional fields may be absent.

```json
{
  "setupComplete": {}
}
```

## Streaming

Connect with wss, send setup, then realtimeInput or clientContent. Handle audio/transcription events, turnComplete, interrupted and errors. The example sends one request after setupComplete and requires model output followed by turnComplete. Errors, timeout, premature close or interrupted mean incomplete; do not automatically resubmit.

## Errors and retries

Check HTTP status and protocol-specific errors; do not mix error formats across protocols. For 401/403 check credentials and permissions, for 429 back off, and for 5xx check gateway/upstream status. Error bodies may contain error/message/code depending on the protocol.

Use bounded exponential backoff for reads. Do not blindly retry task creation, generation, or key mutations, to avoid duplicate billing or execution.

## Constraints

This page describes the base protocol. Use the selected model quick start for supported fields, enums, limits and asynchronous behavior. Replace YOUR_MODEL. Form initial values are not upstream defaults.

## Documentation source

Definition version: 1

Reviewed: 2026-11-02

Source: https://api.tu-zi.com/en/docs/api/reference-relay-gemini-live

Related protocol guide: https://api.tu-zi.com/docs/api/gemini

This export contains documentation and examples only, without real credentials, account data or user requests.
