Lyria 2 Music Generation
Source notebook
Repo path:
audio/music/getting-started/lyria2_music_generation.ipynb· Open on GitHub · intro
Generates 30-second 48 kHz WAV music clips from text prompts with Lyria 2 on Vertex AI.
Summary
This notebook teaches how to call Google’s Lyria 2 music generation model through a Vertex AI prediction endpoint. It authenticates with Application Default Credentials, builds a REST request with prompts and parameters, decodes base64 WAV output, and plays generated audio in the notebook. The examples demonstrate genre, mood, tempo, instrumentation, negative prompts, sample counts, and seed-based deterministic generation.
Key code patterns
Authenticated REST call
creds, project = google.auth.default()
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
headers = {
"Authorization": f"Bearer {creds.token}",
"Content-Type": "application/json",
}
response = requests.post(api_endpoint, headers=headers, json=data)
response.raise_for_status()Shows how the notebook obtains an access token and calls the prediction endpoint directly.
Lyria predict endpoint
music_model = (
f"https://us-central1-aiplatform.googleapis.com/v1/"
f"projects/{PROJECT_ID}/locations/us-central1/"
"publishers/google/models/lyria-002:predict"
)Defines the regional Vertex AI endpoint for the Lyria 2 model.
Music generation request
req = {"instances": [request], "parameters": {}}
resp = send_request_to_google_api(music_model, req)
return resp["predictions"]Wraps prompt settings into the predict request shape expected by the model.
Decode and play audio
bytes_b64 = dict(pred)["bytesBase64Encoded"]
decoded_audio_data = base64.b64decode(bytes_b64)
audio = Audio(decoded_audio_data, rate=48000, autoplay=False)
display(audio)Converts model output from base64 into playable 48 kHz audio in the notebook.
Models & APIs used
- Models: lyria-002
- APIs / services: Vertex AI, Agent Platform API
- SDKs / libraries:
google.auth,requests,IPython.display
When to use this
Use this pattern when generating short music clips from text prompts with Lyria 2 through Vertex AI REST predictions.
Gotchas & caveats
- Requires an existing Google Cloud project.
- The Agent Platform API must be enabled through aiplatform.googleapis.com.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- The endpoint is hard-coded to us-central1.
- seed and sample_count cannot be set in the same request.
- Generated clips are described as 30 second WAV audio at a 48 kHz sample rate.
Best practices
- Use detailed prompts describing style, mood, tempo, rhythm, and instrumentation.
- Use negative_prompt to specify audio qualities to exclude.
- Use seed for deterministic generation when not using sample_count.
- Decode bytesBase64Encoded output before playing the audio.
- Rely on response.raise_for_status() to surface failed API calls.
Related
- Concepts: Audio & Speech
- Entities: Vertex AI · Lyria
- Area: Audio Notebooks
- Best practices: Audio & Speech - Best Practices