BoundaryML/baml · error

encoding type builder

Error message

encoding type builder: %w

What it means

A TypeBuilder attached to BamlFunctionArguments is encoded into a BamlObjectHandle via raw_objects.EncodeRawObject before the call. Wrapping failure means the type builder's raw object could not be converted into a host-side handle, so the call cannot proceed.

Solutions

  1. Create the TypeBuilder via tb.NewTypeBuilder (or runtime's builder API) and use it only with the matching runtime
  2. Keep a Go reference to the TypeBuilder alive until the function call completes
  3. Recreate the TypeBuilder in the same scope as the call if it was shared across runtimes
  4. Match BAML Go bindings and runtime versions (regenerate client)

Example fix

// before
tb := &TypeBuilder{} // zero value, not registered with runtime
args.TypeBuilder = tb
// after
tb := tb.NewTypeBuilder()
tb.Class("User").Field("id", "string")
args.TypeBuilder = tb
Defensive patterns

Strategy: validation

Validate before calling

// ensure type builder is constructed and registered
tb := tb.NewTypeBuilder()
tb.Class("User").Field("id", "string")
if tb == nil { return errors.New("type builder not initialized") }

Try / catch

err = runtime.CallFunction(ctx, "Fn", params, kwargs, args)
if err != nil && strings.Contains(err.Error(), "encoding type builder") {
    return fmt.Errorf("recreate TypeBuilder for this runtime: %w", err)
}

Prevention

When it happens

Trigger: Passing a TypeBuilder created outside the current runtime context, a builder whose registration of classes/enums partially failed, or a builder whose underlying raw object pointer is stale/freed.

Common situations: Reusing one TypeBuilder across multiple runtime instances; building a TypeBuilder and letting it go out of scope/GC before the call; constructing TypeBuilder manually without the official constructor in a version-skewed build.

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

Appendix: source

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

	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{
				Key: &cffi.HostMapEntry_StringKey{StringKey: key},
				Value: &cffi.HostValue{
					Value: &cffi.HostValue_StringValue{
						StringValue: value,
					},
				},
			})
		}
	}

View on GitHub (pinned to bd85ce9dee)