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

Images

BAML functions that accept an image input utilize the Image helper, which supports creating Image objects from URLs or Base64 encoded data.

Image Creation Methods

Image.from_url(url: string, media_type: string | null): Creates an Image object from the specified URL. (optionally specify the media type, otherwise it will be inferred from the URL)

Image.from_base64(mime_type: string, data: string): Creates an Image object using Base64 encoded data along with the given MIME type.

Usage

1from baml_py import Image
2from baml_client import b
3
4async def test_image_input():
5 # Create an Image from a URL
6 img = Image.from_url("https://upload.wikimedia.org/wikipedia/en/4/4d/Shrek_%28character%29.png")
7 res = await b.TestImageInput(img=img)
8
9 # Create an Image from Base64 data
10 image_b64 = "iVB0xyz..."
11 img = Image.from_base64("image/png", image_b64)
12 res = await b.TestImageInput(img=img)

Audio

BAML functions that accept audio inputs utilize the Audio helper. Similar to images, you can create Audio objects from URLs or Base64 encoded data.

Audio Creation Methods

• Audio.from_url(url: string): Creates an Audio object from the specified URL.

• Audio.from_base64(mime_type: string, data: string): Creates an Audio object using Base64 encoded audio data with the provided MIME type.

Usage

Python

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

TypeScript

1import { b } from '../baml_client'
2import { Audio } from "@boundaryml/baml"
3
4// Create an Audio object from a URL
5let res = await b.TestAudioInput(
6 Audio.fromUrl('https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg')
7)
8
9// Create an Audio object from Base64 data
10const audio_base64 = "iVB0xyz..."
11res = await b.TestAudioInput(
12 Audio.fromBase64('audio/ogg', audio_base64)
13)

Ruby (beta)

1# Ruby implementation is in development.
Built with