BoundaryML/baml · error

failed to get as base64: %w

Error message

failed to get as base64: %w

What it means

Wraps any error from the FFI call to the runtime's `as_base64` method, which converts media content into a base64 string when possible. The real cause from the Rust runtime is preserved via %w. A nil result (no error) simply means the runtime produced no base64 representation.

Source

Thrown at engine/language_client_go/pkg/rawobjects_media.go:139

	if err != nil {
		return nil, fmt.Errorf("failed to get as url: %w", err)
	}

	if result == nil {
		return nil, nil
	}

	as_url, ok := result.(string)
	if !ok {
		return nil, fmt.Errorf("unexpected type for as_url: %T", result)
	}

	return &as_url, nil
}
func (m *mediaHolder) AsBase64() (*string, error) {
	result, err := raw_objects.CallMethod(m, "as_base64", nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get as base64: %w", err)
	}

	if result == nil {
		return nil, nil
	}

	as_base64, ok := result.(string)
	if !ok {
		return nil, fmt.Errorf("unexpected type for as_base64: %T", result)
	}

	return &as_base64, nil
}

func newMedia(ptr int64, rt unsafe.Pointer, mediaType MediaType) media {
	media := mediaHolder{raw_objects.FromPointer(ptr, rt), mediaType}
	switch mediaType {
	case MediaType_Image:

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Inspect errors.Unwrap(err) for the underlying failure
  2. Check media.IsBase64()/IsUrl() first and use AsUrl for URL media
  3. Rebuild the media object from a fresh BAML response
  4. Keep the Go module and baml-cli runtime versions aligned
Defensive patterns

Strategy: fallback

Validate before calling

isB64, err := media.IsBase64()
if err != nil || !isB64 { return nil }

Try / catch

b64, err := media.AsBase64()
if err != nil {
    log.Printf("as_base64 failed: %v", err)
    if u, uerr := media.AsUrl(); uerr == nil { return useUrl(u) }
    return nil
}

Prevention

When it happens

Trigger: Calling AsBase64() on media whose raw object handle is invalid, or when the runtime-side `as_base64` conversion fails (e.g. content that cannot be re-encoded as base64).

Common situations: Calling AsBase64 on URL-only media with no stored bytes; stale handles after response release; version mismatch between the Go module and native runtime.

Understand the failure class

Background: "invalid response format", "malformed payload", "missing data field": when an API returns 200 but the response shape is wrong — this error's family across 23 libraries.

Related errors


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