> Version notice: This site documents BAML v0, the legacy DSL. BAML v1 is a separate, fully featured programming language in public beta. If the user intends to use BAML v1, ignore this site's content and use the BAML v1 documentation instead. If the intended version is unclear, clarify it before using this page.
> For clean Markdown of any page, append .md to the page URL. For the complete documentation index, see https://docs.boundaryml.com/llms.txt. For MCP access, connect to https://docs.boundaryml.com/_mcp/server.

# Audio

> Learn how to handle audio inputs in BAML functions

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](/ref/baml/types#audio).

## Usage Examples

```python
from baml_py import Audio
from baml_client import b

async def test_audio_input():
    # Create an Audio object from a URL
    audio = Audio.from_url("https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg")
    res = await b.TestAudioInput(audio=audio)

    # Create an Audio object from Base64 data
    audio_b64 = "iVB0xyz..."
    audio = Audio.from_base64("audio/ogg", audio_b64)
    res = await b.TestAudioInput(audio=audio)
```

```typescript
import { b } from '../baml_client'
import { Audio } from "@boundaryml/baml"

// Create an Audio object from a URL
let res = await b.TestAudioInput(
    Audio.fromUrl('https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg')
)

// Create an Audio object from Base64 data
const audio_b64 = "iVB0xyz..."
res = await b.TestAudioInput(
    Audio.fromBase64('audio/ogg', audio_b64)
)

// Browser-specific methods
const fileAudio = await Audio.fromFile(file)
const blobAudio = await Audio.fromBlob(blob, 'audio/ogg')
const fetchedAudio = await Audio.fromUrlAsync('https://example.com/audio.ogg')
```

```tsx
import { useTestAudioInput } from '../baml_client/react/hooks'
import { Audio } from "../baml_client/react/media"

export function TestAudioInput() {
    const { mutate } = useTestAudioInput()

    const handleClick = async () => {
        const audio = await Audio.fromUrl('https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg')
        mutate(audio)
    }

    return (
      <div>
          <button onClick={handleClick}>
              Test Audio Input
          </button>
      </div>
    )
}
```

```go
package main

import (
    "context"
    
    b "example.com/myproject/baml_client"
)

func testAudioInput() error {
    ctx := context.Background()
    
    // Create an Audio from a URL
    aud, err := b.NewAudioFromUrl("https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg", nil)
    if err != nil {
        return err
    }
    
    result, err := b.TestAudioInput(ctx, aud)
    if err != nil {
        return err
    }

    // Create an Audio from Base64 data
    audioB64 := "SUQzAwAAAAABAAAAAAAAAAAAAAA..."
    aud2, err := b.NewAudioFromBase64(audioB64, stringPtr("audio/mp3"))
    if err != nil {
        return err
    }
    
    result2, err := b.TestAudioInput(ctx, aud2)
    if err != nil {
        return err
    }
    
    return nil
}

// Helper function for string pointer
func stringPtr(s string) *string {
    return &s
}
```

**`Rust`**

```rust Rust
use myproject::baml_client::sync_client::B;
use myproject::baml_client::new_audio_from_url;

fn test_audio_input() {
    // Create an Audio from a URL
    let audio = new_audio_from_url(
        "https://actions.google.com/sounds/v1/emergency/beeper_emergency_call.ogg",
        None,
    );
    let res = B.TestAudioInput.call(&audio).unwrap();
}
```

```ruby
# Ruby implementation is in development.
```

## API Reference

#### Python

### 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}`.

---

#### TypeScript

### Static Methods

**`fromUrl`** `(url: string, mediaType?: string) => Audio`

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

---

**`fromBase64`** `(mediaType: string, base64: string) => Audio`

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

---

**`fromFile`** `(file: File) => Promise<Audio>`

> **Info**
>
> Only available in browser environments. @boundaryml/baml/browser

Creates an Audio object from a File object. Available in browser environments only.

---

**`fromBlob`** `(blob: Blob, mediaType?: string) => Promise<Audio>`

> **Info**
>
> Only available in browser environments. @boundaryml/baml/browser

Creates an Audio object from a Blob object. Available in browser environments only.

---

**`fromUrlToBase64`** `(url: string) => Promise<Audio>`

> **Info**
>
> Only available in browser environments.

Creates an Audio object by fetching from a URL. Available in browser environments only.

---

### Instance Methods

**`isUrl`** `() => boolean`

Check if the audio is stored as a URL.

---

**`asUrl`** `() => string`

Get the URL of the audio if it's stored as a URL. Throws an Error if the audio is not stored as a URL.

---

**`asBase64`** `() => [string, string]`

Get the base64 data and media type if the audio is stored as base64. Returns `[base64Data, mediaType]`. Throws an Error if the audio is not stored as base64.

---

**`toJSON`** `() => { url: string } | { base64: string; media_type: string }`

Convert the audio to a JSON representation. Returns either a URL object or a base64 object with media type.

---

#### Go

### Static Methods

**`NewAudioFromUrl`** `(url string, mediaType *string) (*Audio, error)`

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

---

**`NewAudioFromBase64`** `(base64 string, mediaType *string) (*Audio, error)`

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

---

### Instance Methods

**`IsUrl`** `() bool`

Check if the audio is stored as a URL.

---

**`AsUrl`** `() (string, error)`

Get the URL of the audio if it's stored as a URL. Returns an error if the audio is not stored as a URL.

---

**`AsBase64`** `() (string, string, error)`

Get the base64 data and media type if the audio is stored as base64. Returns `(base64Data, mediaType, error)`. Returns an error if the audio is not stored as base64.

---

**`ToJSON`** `() (map[string]interface{}, error)`

Convert the audio to a map representation. Returns either `{"url": string}` or `{"base64": string, "media_type": string}`.

---

#### Rust

### Static Methods

**`new_audio_from_url`** `(url: &str, media_type: Option<&str>) -> BamlAudio`

Creates an Audio from a URL. Optionally specify the media type.

---

**`new_audio_from_base64`** `(base64: &str, media_type: Option<&str>) -> BamlAudio`

Creates an Audio from Base64 encoded data with an optional MIME type.

---

#### Ruby

Ruby implementation is in development.

## URL Handling

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

* **[OpenAI](/ref/llm-client-providers/open-ai#media_url_handler)**: By default converts to base64 (`send_base64`) for compatibility.
* **[Vertex AI](/ref/llm-client-providers/google-vertex#media_url_handler)**: By default uses `send_url_add_mime_type` to include MIME type.
* **[Anthropic](/ref/llm-client-providers/anthropic#media_url_handler)**: By default keeps URLs as-is (`send_url`).
* **[Google AI](/ref/llm-client-providers/google-ai-gemini#media_url_handler)**: By default keeps URLs as-is (`send_url`).
* **[AWS Bedrock](/ref/llm-client-providers/aws-bedrock#media_url_handler)**: By default converts to base64 (`send_base64`).

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