Gemini Live API: Error 1007 when switching to TEXT-only mode for real-time STT

Hey everyone,

I’m currently messing around with the new gemini-2.5-flash-native-audio-preview-12-2025 model. My goal is to use it as a low-latency Whisper alternative for a real-time transcription tool (mic-to-text streaming) in python.

I’ve been digging through the official Google Cookbook example, but I’m hitting a wall. Whenever I try to swap response_modalities from ["AUDIO"] to ["TEXT"], the connection drops with a 1007 error (invalid frame payload data). It seems like the server is complaining because it can’t “extract voices”.

I suspect it’s because the speech_config is hardcoded in the setup, and even though I want text-only output, the API expects a voice config if any audio input is present. I just need a clean mic-in / text-out streaming loop without the model trying to talk back.

How do I bypass this restriction or properly initialize the session for TEXT-only modality while still streaming PCM audio?

Here is the full code I’m using based on the Google example:

"""
## Documentation
Quickstart: https://github.com/google-gemini/cookbook/blob/main/quickstarts/Get_started_LiveAPI.py

## Setup

To install the dependencies for this script, run:

```
pip install google-genai opencv-python pyaudio pillow mss
```
"""

import os
import asyncio
import base64
import io
import traceback

import cv2
import pyaudio
import PIL.Image
import mss

import argparse

from google import genai
from google.genai import types

FORMAT = pyaudio.paInt16
CHANNELS = 1
SEND_SAMPLE_RATE = 16000
RECEIVE_SAMPLE_RATE = 24000
CHUNK_SIZE = 1024

MODEL = "models/gemini-2.5-flash-native-audio-preview-12-2025"

DEFAULT_MODE = "camera"

client = genai.Client(
    http_options={"api_version": "v1beta"},
    api_key=os.environ.get("GEMINI_API_KEY"),
)


CONFIG = types.LiveConnectConfig(
    response_modalities=[
        "AUDIO",
    ],
    media_resolution="MEDIA_RESOLUTION_LOW",
    speech_config=types.SpeechConfig(
        voice_config=types.VoiceConfig(
            prebuilt_voice_config=types.PrebuiltVoiceConfig(voice_name="Zephyr")
        )
    ),
    context_window_compression=types.ContextWindowCompressionConfig(
        trigger_tokens=25600,
        sliding_window=types.SlidingWindow(target_tokens=12800),
    ),
)

pya = pyaudio.PyAudio()


class AudioLoop:
    def __init__(self, video_mode=DEFAULT_MODE):
        self.video_mode = video_mode

        self.audio_in_queue = None
        self.out_queue = None

        self.session = None

        self.send_text_task = None
        self.receive_audio_task = None
        self.play_audio_task = None

    async def send_text(self):
        while True:
            text = await asyncio.to_thread(
                input,
                "message > ",
            )
            if text.lower() == "q":
                break
            await self.session.send(input=text or ".", end_of_turn=True)

    def _get_frame(self, cap):
        # Read the frameq
        ret, frame = cap.read()
        # Check if the frame was read successfully
        if not ret:
            return None
        # Fix: Convert BGR to RGB color space
        # OpenCV captures in BGR but PIL expects RGB format
        # This prevents the blue tint in the video feed
        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = PIL.Image.fromarray(frame_rgb)  # Now using RGB frame
        img.thumbnail([1024, 1024])

        image_io = io.BytesIO()
        img.save(image_io, format="jpeg")
        image_io.seek(0)

        mime_type = "image/jpeg"
        image_bytes = image_io.read()
        return {"mime_type": mime_type, "data": base64.b64encode(image_bytes).decode()}

    async def get_frames(self):
        # This takes about a second, and will block the whole program
        # causing the audio pipeline to overflow if you don't to_thread it.
        cap = await asyncio.to_thread(
            cv2.VideoCapture, 0
        )  # 0 represents the default camera

        while True:
            frame = await asyncio.to_thread(self._get_frame, cap)
            if frame is None:
                break

            await asyncio.sleep(1.0)

            await self.out_queue.put(frame)

        # Release the VideoCapture object
        cap.release()

    def _get_screen(self):
        sct = mss.mss()
        monitor = sct.monitors[0]

        i = sct.grab(monitor)

        mime_type = "image/jpeg"
        image_bytes = mss.tools.to_png(i.rgb, i.size)
        img = PIL.Image.open(io.BytesIO(image_bytes))

        image_io = io.BytesIO()
        img.save(image_io, format="jpeg")
        image_io.seek(0)

        image_bytes = image_io.read()
        return {"mime_type": mime_type, "data": base64.b64encode(image_bytes).decode()}

    async def get_screen(self):

        while True:
            frame = await asyncio.to_thread(self._get_screen)
            if frame is None:
                break

            await asyncio.sleep(1.0)

            await self.out_queue.put(frame)

    async def send_realtime(self):
        while True:
            msg = await self.out_queue.get()
            await self.session.send(input=msg)

    async def listen_audio(self):
        mic_info = pya.get_default_input_device_info()
        self.audio_stream = await asyncio.to_thread(
            pya.open,
            format=FORMAT,
            channels=CHANNELS,
            rate=SEND_SAMPLE_RATE,
            input=True,
            input_device_index=mic_info["index"],
            frames_per_buffer=CHUNK_SIZE,
        )
        if __debug__:
            kwargs = {"exception_on_overflow": False}
        else:
            kwargs = {}
        while True:
            data = await asyncio.to_thread(self.audio_stream.read, CHUNK_SIZE, **kwargs)
            await self.out_queue.put({"data": data, "mime_type": "audio/pcm"})

    async def receive_audio(self):
        "Background task to reads from the websocket and write pcm chunks to the output queue"
        while True:
            turn = self.session.receive()
            async for response in turn:
                if data := response.data:
                    self.audio_in_queue.put_nowait(data)
                    continue
                if text := response.text:
                    print(text, end="")

            # If you interrupt the model, it sends a turn_complete.
            # For interruptions to work, we need to stop playback.
            # So empty out the audio queue because it may have loaded
            # much more audio than has played yet.
            while not self.audio_in_queue.empty():
                self.audio_in_queue.get_nowait()

    async def play_audio(self):
        stream = await asyncio.to_thread(
            pya.open,
            format=FORMAT,
            channels=CHANNELS,
            rate=RECEIVE_SAMPLE_RATE,
            output=True,
        )
        while True:
            bytestream = await self.audio_in_queue.get()
            await asyncio.to_thread(stream.write, bytestream)

    async def run(self):
        try:
            async with (
                client.aio.live.connect(model=MODEL, config=CONFIG) as session,
                asyncio.TaskGroup() as tg,
            ):
                self.session = session

                self.audio_in_queue = asyncio.Queue()
                self.out_queue = asyncio.Queue(maxsize=5)

                send_text_task = tg.create_task(self.send_text())
                tg.create_task(self.send_realtime())
                tg.create_task(self.listen_audio())
                if self.video_mode == "camera":
                    tg.create_task(self.get_frames())
                elif self.video_mode == "screen":
                    tg.create_task(self.get_screen())

                tg.create_task(self.receive_audio())
                tg.create_task(self.play_audio())

                await send_text_task
                raise asyncio.CancelledError("User requested exit")

        except asyncio.CancelledError:
            pass
        except ExceptionGroup as EG:
            self.audio_stream.close()
            traceback.print_exception(EG)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--mode",
        type=str,
        default=DEFAULT_MODE,
        help="pixels to stream from",
        choices=["camera", "screen", "none"],
    )
    args = parser.parse_args()
    main = AudioLoop(video_mode=args.mode)
    asyncio.run(main.run())

Hi @dashii, welcome to the community!

Can you try setting speech_config=NONE as you are trying for only TEXT, and see if it works?

You also need to modify your receive_audio function as it’s currently written to receive audio, not text (Parts).

Thank you!

Did it, still getting this error:

Traceback (most recent call last):
  File "C:\Users\BLOOD\Desktop\PROJECTWORLD\Hallway Desktop NEW\main.py", line 170, in run
    async with self.client.aio.live.connect(model=MODEL, config=CONFIG) as session:
  File "C:\Users\BLOOD\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 204, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\BLOOD\AppData\Local\Programs\Python\Python312\Lib\site-packages\google\genai\live.py", line 1093, in connect
    raw_response = await ws.recv(decode=False)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\BLOOD\AppData\Local\Programs\Python\Python312\Lib\site-packages\websockets\asyncio\connection.py", line 322, in recv
    raise self.protocol.close_exc from self.recv_exc
websockets.exceptions.ConnectionClosedError: received 1007 (invalid frame payload data) Cannot extract voices from a non-audio request.; then sent 1007 (invalid frame payload data) Cannot extract voices from a non-audio request.

Here’s my full code:

import os
import asyncio
import traceback
import pyaudio
import threading
import queue
import tkinter as tk
from tkinter import scrolledtext
from dotenv import load_dotenv
from google import genai

load_dotenv()
API_KEY = os.getenv("GEMINI_API_KEY")

if not API_KEY:
    print("API KEY ERROR")
    exit(1)

FORMAT = pyaudio.paInt16
CHANNELS = 1
SAMPLE_RATE = 16000
CHUNK_SIZE = 4096
MODEL = "models/gemini-2.5-flash-native-audio-preview-12-2025"

CONFIG = {
    "response_modalities": ["TEXT"],
    "input_audio_transcription": {},
    "system_instruction": {
        "parts": [{"text": "You are a professional transcriber. Output only the exact transcript of the user audio."}]
    },
    "realtime_input_config": {
        "automatic_activity_detection": {
            "silence_duration_ms": 600000, 
        }
    }
}

class TranscriberApp:
    def __init__(self, root):
        self.root = root
        self.root.title("Hallway Desktop - Clean Stream")
        self.root.geometry("900x600")
        self.root.configure(bg="#121212")

        self.msg_queue = queue.Queue()
        self.is_running = True

        self.text_area = scrolledtext.ScrolledText(
            root, 
            wrap=tk.WORD, 
            font=("Segoe UI", 15),
            bg="#121212", 
            fg="#ffffff", 
            padx=25, 
            pady=25,
            borderwidth=0,
            highlightthickness=0,
            insertbackground="white"
        )
        self.text_area.pack(expand=True, fill='both')
        
        self.status_frame = tk.Frame(root, bg="#1e1e1e", height=35)
        self.status_frame.pack(side=tk.BOTTOM, fill=tk.X)
        
        self.status_label = tk.Label(
            self.status_frame, 
            text="● INITIALIZING...", 
            font=("Segoe UI", 10, "bold"),
            bg="#1e1e1e",
            fg="#ff9800"
        )
        self.status_label.pack(side=tk.LEFT, padx=20)

        self.loop_thread = threading.Thread(target=self.start_async_loop, daemon=True)
        self.loop_thread.start()

        self.root.after(100, self.process_queue)
        self.root.protocol("WM_DELETE_WINDOW", self.on_close)

    def process_queue(self):
        try:
            while True:
                msg_type, content = self.msg_queue.get_nowait()
                
                if msg_type == "TEXT":
                    self.text_area.config(state='normal')
                    self.text_area.insert(tk.END, content)
                    self.text_area.see(tk.END)
                    self.text_area.config(state='disabled')
                
                elif msg_type == "STATUS":
                    if "LISTENING" in content:
                        self.status_label.config(text="● LIVE TRANSCRIPTION", fg="#4caf50")
                    else:
                        self.status_label.config(text=f"● {content}", fg="#f44336")

        except queue.Empty:
            pass
        
        if self.is_running:
            self.root.after(10, self.process_queue)

    def on_close(self):
        self.is_running = False
        self.root.destroy()
        os._exit(0)

    def start_async_loop(self):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        logic = TranscriberLogic(self.msg_queue)
        try:
            loop.run_until_complete(logic.run())
        except Exception as e:
            print(f"Async Error: {e}")
        finally:
            loop.close()

class TranscriberLogic:
    def __init__(self, msg_queue):
        self.msg_queue = msg_queue
        self.client = genai.Client(http_options={"api_version": "v1beta"}, api_key=API_KEY)
        self.pya = pyaudio.PyAudio()
        self.out_queue = asyncio.Queue()
        self.session = None

    async def listen_audio(self):
        try:
            mic_info = self.pya.get_default_input_device_info()
            stream = await asyncio.to_thread(
                self.pya.open,
                format=FORMAT,
                channels=CHANNELS,
                rate=SAMPLE_RATE,
                input=True,
                input_device_index=mic_info["index"],
                frames_per_buffer=CHUNK_SIZE,
            )
            self.msg_queue.put(("STATUS", "LISTENING"))
            
            while True:
                data = await asyncio.to_thread(stream.read, CHUNK_SIZE, exception_on_overflow=False)
                await self.out_queue.put({"data": data, "mime_type": "audio/pcm;rate=16000"})
        except Exception as e:
            self.msg_queue.put(("STATUS", f"MIC ERROR: {e}"))
            traceback.print_exc()

    async def send_realtime(self):
        while True:
            msg = await self.out_queue.get()
            if self.session:
                await self.session.send_realtime_input(audio=msg)
                await asyncio.sleep(0.001)

    async def receive_results(self):
        while True:
            try:
                async for response in self.session.receive():
                    if response.server_content and response.server_content.input_transcription:
                        text = response.server_content.input_transcription.text
                        if text:
                            self.msg_queue.put(("TEXT", text))
            except Exception as e:
                print(f"Receive Error: {e}")
                self.msg_queue.put(("STATUS", "RECONNECTING..."))
                break

    async def run(self):
        try:
            async with self.client.aio.live.connect(model=MODEL, config=CONFIG) as session:
                self.session = session
                print("Connected to Gemini Live API")
                
                async with asyncio.TaskGroup() as tg:
                    tg.create_task(self.listen_audio())
                    tg.create_task(self.send_realtime())
                    tg.create_task(self.receive_results())
                    
                    while True:
                        await asyncio.sleep(0.1)

        except Exception as e:
            traceback.print_exc()
            self.msg_queue.put(("STATUS", "OFFLINE / ERROR"))

if __name__ == "__main__":
    root = tk.Tk()
    app = TranscriberApp(root)
    root.mainloop()

Hey,

Sorry for the delayed response!

The server rejected your connection because Native Audio models require an AUDIO output channel to be open, even if you only want text.

Please try to change the config to accept AUDIO but give it a system instruction to remain silent, effectively turning it into a transcription-only stream.

Example code:

CONFIG = {
    "response_modalities": ["AUDIO"],
    "input_audio_transcription": {},
    "speech_config": {
        "voice_config": {"prebuilt_voice_config": {"voice_name": "Puck"}}
    },
    "system_instruction": {
        "parts": [{"text": "You are a transcriber. Listen to the user and do not speak."}]
    }
}

Thank you!

@dashii did this work for you?

Hi Srikanta. What do you mean by output channel exactly? Which line in your example is the output audio? If it doesn’t show one, how can we set one?