The Gemini API, using Lyria RealTime, provides access to a state-of-the-art, real-time, streaming music generation model. It allows developers to build applications where users can interactively create, continuously steer, and perform instrumental music.
Lyria RealTime music generation uses a persistent, bidirectional, low-latency streaming connection using WebSocket.
To experience what can be built using Lyria RealTime, try it on AI Studio using the Prompt DJ or the MIDI DJ apps.
Generate and control music
Lyria RealTime works similarly to the Live API in that it uses Websockets to maintain real-time communication with the model.
The following code demonstrates how to generate music:
Python
This example initializes the Lyria RealTime session using
client.aio.live.music.connect(), then sends an
initial prompt with session.set_weighted_prompts() along with an initial
configuration using session.set_music_generation_config, starts the music
generation using session.play() and sets up
receive_audio() to process the audio chunks it receives.
import asyncio
from google import genai
from google.genai import types
client = genai.Client(http_options={'api_version': 'v1beta'})
async def main():
async def