Audio

Audio values to BAML functions can be created in client libraries. This document explains how to use these functions both at compile time and runtime to handle audio data. For more details, refer to audio types.

Usage Examples

1from baml_py import Audio
2from baml_client import b
3
4async def test_audio_input():
5 # Create an Audio object from a URL
6 audio = Audio.from_url("https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg")
7 res = await b.TestAudioInput(audio=audio)
8
9 # Create an Audio object from Base64 data
10 audio_b64 = "iVB0xyz..."
11 audio = Audio.from_base64("audio/ogg", audio_b64)
12 res = await b.TestAudioInput(audio=audio)

API Reference

Static Methods

from_url
(url: str, media_type: Optional[str] = None) -> Audio

Creates an Audio object from a URL. Optionally specify the media type, otherwise it will be inferred from the URL.

from_base64
(media_type: str, base64: str) -> Audio

Creates an Audio object using Base64 encoded data along with the given MIME type.

Instance Methods

is_url
() -> bool

Check if the audio is stored as a URL.

as_url
() -> str

Get the URL of the audio if it’s stored as a URL. Raises an exception if the audio is not stored as a URL.

as_base64
() -> list[str]

Get the base64 data and media type if the audio is stored as base64. Returns [base64_data, media_type]. Raises an exception if the audio is not stored as base64.

baml_serialize
() -> dict

Convert the audio to a dictionary representation. Returns either {"url": str} or {"base64": str, "media_type": str}.

URL Handling

Audio URLs are processed according to your client’s media_url_handler configuration:

  • OpenAI: By default converts to base64 (send_base64) for compatibility.
  • Vertex AI: By default uses send_url_add_mime_type to include MIME type.
  • Anthropic: By default keeps URLs as-is (send_url).
  • Google AI: By default keeps URLs as-is (send_url).
  • AWS Bedrock: By default converts to base64 (send_base64).

Note: OpenAI requires audio to be base64-encoded, which is why the default is send_base64.