BoundaryML/baml · error

encoding collector

Error message

encoding collector: %w

What it means

Each non-nil collector is converted into a CFFI BamlObjectHandle with raw_objects.EncodeRawObject. The encode() code wraps any resulting error with "encoding collector: %w", meaning the collector's underlying raw object could not be serialized into a handle for the host runtime.

Solutions

  1. Create the collector with the same runtime (or via the shared c collector constructors) used for the function call
  2. Hold a Go reference to both the collector and runtime for the call's lifetime to avoid GC of the underlying handle
  3. Recreate the collector immediately before the call if it came from a previous runtime instance
  4. Align BAML Go bindings and runtime versions

Example fix

// before
oldCollector := oldRuntime.NewCollector("logs") // different runtime
runtime.CallFunction(ctx, "Fn", params, kwargs, args) // encode fails
// after
collector := c.NewCollector("logs") // same runtime / constructor
runtime.CallFunction(ctx, "Fn", params, kwargs, args)
Defensive patterns

Strategy: validation

Validate before calling

// ensure collector was created successfully before attaching
collector, err := c.NewCollector("logs")
if err != nil || collector == nil { return err }

Try / catch

err = runtime.CallFunction(ctx, "Fn", params, kwargs, args)
if err != nil && strings.Contains(err.Error(), "encoding collector") {
    return fmt.Errorf("recreate collector with the same runtime: %w", err)
}

Prevention

When it happens

Trigger: Encoding a Collector whose raw object pointer is invalid or whose runtime registration failed — typically when the collector was created against a different/older runtime or its handle was already freed/reset.

Common situations: Sharing a collector across two BamlRuntime instances; using a collector after the runtime it belongs to was dropped by GC; mixed BAML versions between collector creation and the call site.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at engine/language_client_go/pkg/rawobjects_function_args.go:59

	}

	var env []*cffi.HostEnvVar
	if args.Env != nil {
		env, err = serde.EncodeEnvVar(args.Env)
		if err != nil {
			return nil, fmt.Errorf("encoding env vars: %w", err)
		}
	}

	var collectors []*cffi.BamlObjectHandle
	if args.Collectors != nil {
		for _, collector := range args.Collectors {
			if collector == nil {
				return nil, fmt.Errorf("nil collector found in collectors")
			}
			encodedCollector := raw_objects.EncodeRawObject(collector)
			if err != nil {
				return nil, fmt.Errorf("encoding collector: %w", err)
			}
			collectors = append(collectors, encodedCollector)
		}
	}

	var typeBuilder *cffi.BamlObjectHandle
	if args.TypeBuilder != nil {
		encodedTypeBuilder := raw_objects.EncodeRawObject(args.TypeBuilder)
		if err != nil {
			return nil, fmt.Errorf("encoding type builder: %w", err)
		}
		typeBuilder = encodedTypeBuilder
	}

	var tags []*cffi.HostMapEntry
	if args.Tags != nil {
		for key, value := range args.Tags {
			tags = append(tags, &cffi.HostMapEntry{

View on GitHub (pinned to bd85ce9dee)