BoundaryML/baml · error

unexpected type for collector creation: %T

Error message

unexpected type for collector creation: %T

What it means

NewCollector calls the Rust runtime's raw object factory and then asserts the returned generic pointer is a *collector. If the runtime returns any other concrete Go wrapper type, this error is thrown. It is an internal consistency check: it means the cffi layer and the Go wrapper types have drifted out of sync, not that caller input was wrong.

Solutions

  1. Check that the baml Go module version and the bundled native runtime are from the same release; run `go mod tidy` and upgrade to a consistent version
  2. Rebuild/re-download the native BAML runtime binary so it matches the Go bindings
  3. If reproducible on a clean install, file a bug with the BAML maintainers including the %T value printed in the message
Defensive patterns

Strategy: try-catch

Validate before calling

// Go: ensure module consistency before use
import (
    "runtime/debug"
    baml "github.com/boundaryml/baml/engine/language_client_go"
)
func checkBamlVersion() error {
    bi, ok := debug.ReadBuildInfo()
    if !ok { return nil }
    for _, d := range bi.Deps {
        if strings.HasPrefix(d.Path, "github.com/boundaryml/baml") {
            _ = d.Version // log and compare against native runtime version
        }
    }
    return nil
}

Type guard

func asCollector(v any) (*collector, bool) {
    c, ok := v.(*collector)
    return c, ok
}

Try / catch

c, err := baml.NewCollector(ctx)
if err != nil {
    if strings.Contains(err.Error(), "unexpected type for collector creation") {
        return nil, fmt.Errorf("baml bindings/runtime version mismatch: %w", err)
    }
    return nil, err
}

Prevention

When it happens

Trigger: Calling baml.NewCollector() when raw_objects.NewRawObject(OBJECT_TYPE_COLLECTOR) returns a non-*collector value — i.e. a version mismatch between the generated cffi bindings and the hand-written Go wrappers, or a runtime bug in NewRawObject dispatch.

Common situations: Mixing a BAML Go SDK version with a mismatched native library/binary (e.g. after a partial upgrade), custom builds of the engine, or an upstream cffi change that renamed or reordered object types.

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/bd199e7a533973d1. Report an issue: GitHub.

Appendix: source

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

)

// / Construct Collector
func (r *BamlRuntime) NewCollector(name string) (Collector, error) {
	kwargs, err := serde.EncodeMapEntries(map[string]any{
		"name": name,
	}, "collector constructor args")
	if err != nil {
		return nil, fmt.Errorf("failed to encode kwargs: %w", err)
	}

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

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

	return as_collector, nil
}

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)
	}

View on GitHub (pinned to bd85ce9dee)