BoundaryML/baml · error

unexpected type for type builder creation: %T

Error message

unexpected type for type builder creation: %T

What it means

After NewRawObject succeeds, NewTypeBuilder asserts the result is a *typeBuilder. If the runtime returned a different concrete type, this error is thrown. It is an internal consistency check that the cffi object factory returned the expected Go wrapper for OBJECT_TYPE_BUILDER.

Source

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

}

func (r *BamlRuntime) NewPDFFromBase64(base64 string, mimeType *string) (PDF, error) {
	return r.newMediaFromBase64(MediaType_PDF, base64, mimeType)
}

func (r *BamlRuntime) NewVideoFromBase64(base64 string, mimeType *string) (Video, error) {
	return r.newMediaFromBase64(MediaType_Video, base64, mimeType)
}

func (r *BamlRuntime) NewTypeBuilder() (TypeBuilder, error) {
	ptr, err := raw_objects.NewRawObject(r.runtime, cffi.BamlObjectType_OBJECT_TYPE_BUILDER, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to create type builder: %w", err)
	}

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

	return as_type_builder, nil
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Match Go SDK and native runtime versions (`go mod tidy`, clean reinstall)
  2. Rebuild or re-download the native runtime binary
  3. Inspect the %T value in the message and file an upstream bug if reproducible on a clean install
Defensive patterns

Strategy: try-catch

Type guard

func asTypeBuilder(v any) (*typeBuilder, bool) {
    tb, ok := v.(*typeBuilder)
    return tb, ok
}

Try / catch

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

Prevention

When it happens

Trigger: Calling NewTypeBuilder() when the returned raw object fails the *typeBuilder assertion — caused by bindings/runtime version mismatch or a NewRawObject dispatch bug, not caller input.

Common situations: Partial SDK upgrades leaving generated wrappers out of sync with the native library; patched engine builds.

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