Implementation:Googleapis Python genai AsyncLiveMusic Connect
| Knowledge Sources | |
|---|---|
| Domains | Music_Generation, Real_Time_AI, Streaming |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Concrete tool for real-time music generation via WebSocket streaming provided by the Google Gen AI SDK.
Description
The AsyncLiveMusic class provides an experimental API module for real-time music generation sessions. The AsyncMusicSession class manages individual music sessions, supporting weighted text prompts, playback control (play/pause/stop), and continuous audio output streaming. This is an experimental feature.
Usage
Import this when building applications that generate music in real-time based on text prompts, such as AI-powered music composition tools or interactive audio experiences.
Code Reference
Source Location
- Repository: Googleapis_Python_genai
- File: google/genai/live_music.py
- Lines: 45-211 (AsyncMusicSession at L45-157, AsyncLiveMusic at L160-211)
Signature
class AsyncLiveMusic(_api_module.BaseModule):
@asynccontextmanager
async def connect(
self,
*,
model: str,
) -> AsyncIterator[AsyncMusicSession]: ...
class AsyncMusicSession:
async def set_weighted_prompts(
self, prompts: list[types.WeightedPrompt]
) -> None: ...
async def set_music_generation_config(
self, config: types.LiveMusicGenerationConfig
) -> None: ...
async def play(self) -> None: ...
async def pause(self) -> None: ...
async def stop(self) -> None: ...
async def reset_context(self) -> None: ...
async def receive(self) -> AsyncIterator[types.LiveMusicServerMessage]: ...
async def close(self) -> None: ...
Import
from google import genai
client = genai.Client(api_key="...")
# Access via: client.aio.live.music
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | Yes | Model ID for music generation |
| prompts | list[WeightedPrompt] | For set_weighted_prompts | Text prompts with weights guiding generation |
| config | LiveMusicGenerationConfig | For set_music_generation_config | Music generation parameters |
Outputs
| Name | Type | Description |
|---|---|---|
| connect() yields | AsyncMusicSession | Music session context manager |
| receive() yields | LiveMusicServerMessage | Audio data messages from the server |
Usage Examples
import asyncio
from google import genai
from google.genai import types
client = genai.Client(api_key="GEMINI_API_KEY")
async def main():
async with client.aio.live.music.connect(model="lyria-realtime-exp") as session:
# Set music prompts
await session.set_weighted_prompts([
types.WeightedPrompt(text="calm piano jazz", weight=1.0),
])
# Start playback
await session.play()
# Receive audio chunks
async for message in session.receive():
if message.data:
# Process audio data
pass
asyncio.run(main())