Início Blog

How to Use the Qwen Audio 3.1 Live API

Qwen Audio 3.1 Live API blog cover

Nov 23, 2026

EmpirioLabs AI

Divulgação: Este artigo foi escrito com auxílio de IA e revisado pela EmpirioLabs AI.

Qwen Audio 3.1 Live is available on EmpirioLabs. It is Alibaba's full-duplex voice model for assistants and support lines: you stream the caller's audio over a WebSocket, the spoken reply streams back, and while your application works on a longer task, such as an order lookup, the model keeps talking with the caller and reads the result when it arrives. It also calls functions you declare, speaks Chinese and English, and is billed by the length of the session.

Try it in the Playground: click Start session and talk.

Connecting

Open the realtime endpoint with the model in the query string and your API key on the handshake:

wss://api.empiriolabs.ai/v1/realtime?model=qwen-audio-3-1-live
Authorization: Bearer YOUR_EMPIRIOLABS_API_KEY

This model has its own event protocol rather than the OpenAI Realtime protocol, so use a plain WebSocket client. Configure the session with one session.update and wait for session.updated. session.model is required and picks the voice model that holds the conversation:

  • qwen-audio-4.0-realtime-preview with turn_detection set to {"type": "duplex"} listens while it speaks, so the caller can interrupt it at any moment. It has 8 voices: yunshu (the default), xiaoman, wanfeng, qingyou, poxiao, maiya, wulong and xuesong.
  • qwen-audio-3.1-realtime-flash answers after the caller pauses, speaks the voices of Qwen Audio 3.1 Realtime Flash, and reads each delegated result aloud.

A complete session in Python

This client sends a recorded question (16-bit mono PCM at 16 kHz), prints the reply's subtitles as they arrive, and saves the spoken reply:

import asyncio, base64, json, os, wave

import websockets

URL = "wss://api.empiriolabs.ai/v1/realtime?model=qwen-audio-3-1-live"


async def main():
    headers = [("Authorization", f"Bearer {os.environ['EMPIRIOLABS_API_KEY']}")]
    async with websockets.connect(URL, additional_headers=headers, max_size=None) as ws:
        await ws.send(json.dumps({"type": "session.update", "event_id": "evt_setup", "session": {
            "model": "qwen-audio-4.0-realtime-preview",
            "turn_detection": {"type": "duplex"},
            "voice": "qingyou",
            "instructions": "Answer in English, in one or two short sentences."}}))
        while True:  # the session is ready once session.updated arrives
            event = json.loads(await ws.recv())
            if event["type"] == "error":
                raise RuntimeError(event["error"]["message"])
            if event["type"] == "session.updated":
                break

        async def speak():
            with wave.open("question_16k.wav") as wav:  # 16-bit mono PCM at 16 kHz
                pcm = wav.readframes(wav.getnframes())
            pcm += bytes(16000 * 2 * 3)  # three seconds of silence after the question
            for i in range(0, len(pcm), 640):  # 20 ms frames, in real time
                await ws.send(json.dumps({"type": "session.input_audio.append",
                                          "audio": base64.b64encode(pcm[i:i + 640]).decode()}))
                await asyncio.sleep(0.02)
            await asyncio.sleep(4)
            await ws.send(json.dumps({"type": "session.close", "event_id": "evt_close"}))

        sender = asyncio.create_task(speak())
        reply = bytearray()
        async for message in ws:
            event = json.loads(message)
            if event["type"] == "session.output_audio.delta":
                reply += base64.b64decode(event["delta"])
            elif event["type"] == "session.output_transcript.delta":
                print(event["delta"], end="", flush=True)
            elif event["type"] == "session.closed":
                print(f"\nsession seconds: {event['usage']['seconds']}")
                break
        await sender

    with wave.open("reply_24k.wav", "wb") as out:  # the reply: 16-bit mono PCM at 24 kHz
        out.setnchannels(1)
        out.setsampwidth(2)
        out.setframerate(24000)
        out.writeframes(bytes(reply))


asyncio.run(main())

The transcript of the caller's speech arrives as session.input_transcript.delta, and session.usage.updated reports the session's elapsed seconds every 5 seconds.

Delegating a task to your application

Add "delegation": {"type": "client"} to session.update, with qwen-audio-3.1-realtime-flash as session.model. When the caller asks for something your application handles, a session.delegation.created event arrives with a delegation_id and a summary such as "Check the delivery status of order number 12345." The model tells the caller it is checking, and you return the result when you have it:

{"type": "session.commentary.append", "event_id": "evt_result",
 "delegation_id": "<delegation_id from the event>",
 "content": "Order 12345 was delivered yesterday at 3 PM to the front desk."}

The model then reads it to the caller: "Your order #12345 was delivered yesterday at 3 PM to the front desk." Send progress in the meantime with session.thinking.append, or ask the caller for a missing detail with session.instruction.append.

Function calling

Declare functions in tools in session.update. Asked "How many black umbrellas do you have in stock right now?", the model called lookup_stock with {"item": "black umbrellas"}, and given {"stock": 7} answered "We have 7 black umbrellas in stock right now." A call arrives as a session.event whose event.item is a function_call; return the result with session.item.create:

{"type": "session.item.create", "event_id": "evt_fn",
 "item": {"type": "function_call_output", "call_id": "<call_id from the event>", "output": "{\"stock\": 7}"}}

Operational notes

  • Answer each call_id once. The same call can arrive in more than one session.event, and a second result for it is refused.
  • Typed text does not start a reply. A message sent with session.item.create adds information to the spoken conversation; the caller's speech is what the model answers.
  • Close the session when the call ends. Listening and silence count as session time, so send session.close, or close the socket, as soon as the caller hangs up.

Watch the launch film

Every voice in the film is Qwen Audio 3.1 Live answering a spoken question through the EmpirioLabs API: in English, in Chinese, and reading back a delegated order result. Watch it on YouTube.

Preços

A session is billed by its length, per second from when it opens until it closes, with a short minimum per session; listening, silence and text all count, and there is no per-token charge. Current rates are on the model page and the pricing page.

Start building

Try Qwen Audio 3.1 Live in the Playground, read the API documentation, and find every event in the Realtime Voice API guide.

Pronto para usar endpoints melhores?

Explore nossos modelos ou entre em contato conosco para consultas de negócios, implantações personalizadas ou qualquer outra coisa.