BoundaryML/baml · error

unexpected type for media creation: %T

Error message

unexpected type for media creation: %T

What it means

After NewRawObject succeeds, newMediaFromUrl asserts the returned value implements the media interface. If it does not, this error is thrown. Like the collector variant, it is an internal consistency check indicating the cffi runtime returned an object of an unexpected type for the requested media type.

Source

Thrown at engine/language_client_go/pkg/rawobjects_constructors.go:49

}

func (r *BamlRuntime) newMediaFromUrl(mediaType MediaType, url string, mimeType *string) (media, error) {
	kwargs, err := serde.EncodeMapEntries(map[string]any{
		"mime_type": mimeType,
		"url":       url,
	}, "media constructor args")
	if err != nil {
		return nil, fmt.Errorf("failed to encode kwargs: %w", err)
	}

	ptr, err := raw_objects.NewRawObject(r.runtime, mediaType.objectType(), kwargs)
	if err != nil {
		return nil, fmt.Errorf("failed to create media: %w", err)
	}

	as_media, ok := ptr.(media)
	if !ok {
		return nil, fmt.Errorf("unexpected type for media creation: %T", ptr)
	}

	return as_media, nil
}

func (r *BamlRuntime) NewImageFromUrl(url string, mimeType *string) (Image, error) {
	return r.newMediaFromUrl(MediaType_Image, url, mimeType)
}

func (r *BamlRuntime) NewAudioFromUrl(url string, mimeType *string) (Audio, error) {
	return r.newMediaFromUrl(MediaType_Audio, url, mimeType)
}

func (r *BamlRuntime) NewPDFFromUrl(url string, mimeType *string) (PDF, error) {
	return r.newMediaFromUrl(MediaType_PDF, url, mimeType)
}

func (r *BamlRuntime) NewVideoFromUrl(url string, mimeType *string) (Video, error) {

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Align the Go SDK module and native BAML runtime to the same release version; run `go mod tidy` and reinstall
  2. Rebuild or re-download the native runtime binary
  3. Read the %T value in the message to identify which wrong type was returned and report upstream if reproducible on a clean install
Defensive patterns

Strategy: try-catch

Type guard

func asMedia(v any) (media, bool) {
    m, ok := v.(media)
    return m, ok
}

Try / catch

img, err := runtime.NewImageFromUrl(u, mime)
if err != nil {
    if strings.Contains(err.Error(), "unexpected type for media creation") {
        return nil, fmt.Errorf("baml runtime/bindings out of sync: %w", err)
    }
    return nil, err
}

Prevention

When it happens

Trigger: Calling any of the New*FromUrl constructors when the runtime returns a value that does not satisfy the media interface — a bindings/runtime version mismatch or a NewRawObject dispatch bug, not a caller-input problem.

Common situations: Partial SDK upgrades leaving the Go wrapper types out of sync with the native library; custom or patched engine builds that return different wrapper types for media objects.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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