BoundaryML/baml · error
failed to get mime type: %w
Error message
failed to get mime type: %w
What it means
This error wraps any failure returned by the underlying FFI call to the Rust runtime's `mime_type` method on a media object (image, audio, PDF, or video). It is raised by the `MimeType()` method of the Go BAML client's media holder when `raw_objects.CallMethod` reports an error. The original cause (e.g. the runtime object was invalid or the method failed) is preserved via %w, so inspect the wrapped error for details.
Source
Thrown at engine/language_client_go/pkg/rawobjects_media.go:86
}
}
func (m *mediaHolder) ObjectType() cffi.BamlObjectType {
return m.mediaType.objectType()
}
func (m *mediaHolder) MediaType() (MediaType, error) {
return m.mediaType, nil
}
func (m *mediaHolder) pointer() int64 {
return m.RawObject.Pointer()
}
func (m *mediaHolder) MimeType() (*string, error) {
result, err := raw_objects.CallMethod(m, "mime_type", nil)
if err != nil {
return nil, fmt.Errorf("failed to get mime type: %w", err)
}
if result == nil {
return nil, nil
}
as_mime_type, ok := result.(string)
if !ok {
return nil, fmt.Errorf("unexpected type for mime type: %T", result)
}
return &as_mime_type, nil
}
func (m *mediaHolder) IsUrl() (bool, error) {
result, err := raw_objects.CallMethod(m, "is_url", nil)
if err != nil {
return false, fmt.Errorf("failed to get is url: %w", err)View on GitHub (pinned to bd85ce9dee)
Solutions
- Inspect the wrapped cause with errors.Is/errors.As to find the underlying FFI failure
- Re-acquire the media object from a fresh BAML function call instead of reusing an old handle
- Upgrade github.com/boundaryml/baml Go client and the baml-cli runtime so versions match
- Log media.MediaType() and the object pointer before the call to confirm the handle is valid
Example fix
// before
mime, err := media.MimeType() // opaque failure
// after
mime, err := media.MimeType()
if err != nil {
var wrapped error
if errors.As(err, &wrapped) { log.Printf("mime_type cause: %v", errors.Unwrap(err)) }
media = reFetchMediaFromResponse() // rebuild handle
} Defensive patterns
Strategy: try-catch
Validate before calling
if media == nil { return fmt.Errorf("media object is nil") } Type guard
func validMedia(m baml.Media) bool { var t baml.MediaType; t, err := m.MediaType(); return err == nil && t != "" } Try / catch
mime, err := media.MimeType()
if err != nil {
log.Printf("mime type unavailable: %v (cause: %v)", err, errors.Unwrap(err))
return fallbackMime(media)
} Prevention
- Use media objects only within the lifetime of the response that produced them
- Keep the baml Go module and native runtime versions in lockstep
- Log the wrapped cause with errors.Unwrap to distinguish FFI vs content issues
When it happens
Trigger: Calling MimeType() on an Image, Audio, PDF, or Video instance whose underlying CFFI raw object handle is stale, freed, or whose remote `mime_type` method fails inside the BAML runtime.
Common situations: Holding onto a media object after the runtime response was released; a version mismatch between the Go client and the native BAML runtime; corrupted or unusual media content that the runtime cannot classify.
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
- failed to get is url: %w
- failed to get is base64: %w
- failed to get as url: %w
- failed to get as base64: %w
- error decoding type, checked type not found:
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/d4971909a77482c5.
Report an issue: GitHub.