BoundaryML/baml · error · Error

Audio is not base64

Error message

Audio is not base64

What it means

JavaScript client state guard: asBase64() was called on a BamlAudio object whose internal type is 'url', not 'base64'. The media objects are tagged unions; calling the accessor for the wrong representation throws. Use asUrl() instead, or convert the representation before calling.

Solutions

  1. Check this.type === 'base64' (or an equivalent guard) before calling asBase64().
  2. Use asUrl() for URL-stored audio.
  3. Build the Audio with Audio.fromBase64 if base64 access is required.

Example fix

// before
const [data, mediaType] = audio.asBase64(); // throws for URL audio
// after
const [data, mediaType] = audio.type === 'base64' ? audio.asBase64() : [null, null];
Defensive patterns

Strategy: type-guard

Validate before calling

if (audio.type === 'base64') { /* safe to call asBase64 */ }

Type guard

function isBase64Audio(audio) { return audio.type === 'base64'; }

Try / catch

try {
  const [data, mt] = audio.asBase64();
} catch (e) {
  if (e.message === 'Audio is not base64') { /* use asUrl instead */ }
  else throw e;
}

Prevention

When it happens

Trigger: Calling audio.asBase64() on an Audio instance constructed from a URL (type !== 'base64').

Common situations: Passing a URL-based Audio where base64 payload is expected, or assuming an LLM-returned audio reference is inline base64.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/7f2a86a8139ba12e. Report an issue: GitHub.

Appendix: source

Thrown at engine/language_client_typescript/audio.js:79

    }
    /**
     * Get the URL of the audio if it's stored as a URL
     * @throws Error if the audio is not stored as a URL
     */
    asUrl() {
        if (!this.isUrl()) {
            throw new Error('Audio is not a URL');
        }
        return this.content;
    }
    /**
     * Get the base64 data and media type if the audio is stored as base64
     * @returns [base64Data, mediaType]
     * @throws Error if the audio is not stored as base64
     */
    asBase64() {
        if (this.type !== 'base64') {
            throw new Error('Audio is not base64');
        }
        return [this.content, this.mediaType || ''];
    }
    /**
     * Convert the audio to a JSON representation
     */
    toJSON() {
        if (this.type === 'url') {
            return { url: this.content };
        }
        return {
            base64: this.content,
            media_type: this.mediaType || '',
        };
    }
}
exports.BamlAudio = BamlAudio;

View on GitHub (pinned to bd85ce9dee)