BoundaryML/baml · critical

invalid media type: '%s'

Error message

invalid media type: '%s'

What it means

MediaType.objectType() maps a MediaType constant to a cffi.BamlObjectType and panics on any value outside Image/Audio/PDF/Video. Because it panics (not returns an error), an unrecognized media type crashes the process at the point of serialization or ObjectType() lookup.

Source

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

// mediaHolder implements InternalBamlSerializer
func (m *mediaHolder) InternalBamlSerializer() {}

func (m *mediaHolder) Encode() (*cffi.BamlObjectHandle, error) {
	return raw_objects.EncodeRawObject(m), nil
}

func (mediaType MediaType) objectType() cffi.BamlObjectType {
	switch mediaType {
	case MediaType_Image:
		return cffi.BamlObjectType_OBJECT_MEDIA_IMAGE
	case MediaType_Audio:
		return cffi.BamlObjectType_OBJECT_MEDIA_AUDIO
	case MediaType_PDF:
		return cffi.BamlObjectType_OBJECT_MEDIA_PDF
	case MediaType_Video:
		return cffi.BamlObjectType_OBJECT_MEDIA_VIDEO
	default:
		panic(fmt.Sprintf("invalid media type: '%s'", mediaType))
	}
}

func (mediaType MediaType) cffiType() (cffi.MediaTypeEnum, error) {
	switch mediaType {
	case MediaType_Image:
		return cffi.MediaTypeEnum_IMAGE, nil
	case MediaType_Audio:
		return cffi.MediaTypeEnum_AUDIO, nil
	case MediaType_PDF:
		return cffi.MediaTypeEnum_PDF, nil
	case MediaType_Video:
		return cffi.MediaTypeEnum_VIDEO, nil
	default:
		return 0, fmt.Errorf("invalid media type: '%s'", mediaType)
	}
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Only construct MediaType via the exported MediaType_Image/Audio/PDF/Video constants
  2. Update all BAML Go packages to the same version so the enum and switch agree
  3. Never cast raw integers to MediaType

Example fix

// before
var mt baml.MediaType
media := baml.NewMediaFromBase64(mt, ...)
// after
media := baml.NewMediaFromBase64(baml.MediaType_PDF, ...) // use a valid constant
Defensive patterns

Strategy: validation

Validate before calling

// Go: validate before use
func validMediaType(mt baml.MediaType) bool {
    switch mt {
    case baml.MediaType_Image, baml.MediaType_Audio, baml.MediaType_PDF, baml.MediaType_Video:
        return true
    }
    return false
}

Prevention

When it happens

Trigger: Constructing a media holder with a MediaType value not produced by the library's own constants — e.g. a zero-value MediaType{}, a value from a different/older package version, or a custom int cast to MediaType.

Common situations: Upgrading BAML where a new MediaType was added in one package but the mapping switch wasn't regenerated; declaring var m MediaType = 0 and passing it into media constructors.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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