BoundaryML/baml · error

unexpected type for class type: %T

Error message

unexpected type for class type: %T

What it means

The class builder's Type method invokes an internal builder call and asserts the generic result is convertible to the Type interface. If the underlying value is a different kind (e.g. the object refers to an enum, literal, or union type rather than a class type), the assertion fails and this error reports the actual Go type.

Source

Thrown at engine/language_client_go/pkg/rawobjects_class_builder.go:36

	return cffi.BamlObjectType_OBJECT_CLASS_BUILDER
}

func newClassBuilder(ptr int64, rt unsafe.Pointer) ClassBuilder {
	bldr := classBuilder{raw_objects.FromPointer(ptr, rt), llmRenderableObject{}}
	bldr.llmRenderableObject = llmRenderableObject{&bldr}
	return &bldr
}

// Type returns the type definition for this class
func (cb *classBuilder) Type() (Type, error) {
	result, err := raw_objects.CallMethod(cb, "type_", nil)
	if err != nil {
		return nil, err
	}

	typ, ok := result.(Type)
	if !ok {
		return nil, fmt.Errorf("unexpected type for class type: %T", result)
	}

	return typ, nil
}

// ListProperties returns all properties in the class
func (cb *classBuilder) ListProperties() ([]ClassPropertyBuilder, error) {
	result, err := raw_objects.CallMethod(cb, "list_properties", nil)
	if err != nil {
		return nil, err
	}

	rawObjects, ok := result.([]raw_objects.RawPointer)
	if !ok {
		return nil, fmt.Errorf("unexpected type for class property builders: %T", result)
	}

	rawObjectsCast := make([]ClassPropertyBuilder, len(rawObjects))

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Check the %T in the message to see what you actually got; use the corresponding builder method for that kind.
  2. Ensure you obtained the builder from the class-specific accessor (e.g. tb.ClassName() style) rather than a generic type lookup.
  3. Regenerate the BAML client so generated builders match your baml_schema sources.
  4. Guard with a comma-ok type assertion on the returned value before use if the kind can vary.

Example fix

// before
typ, err := builder.Type() // builder is actually an enum builder

// after
if clsBuilder, ok := builder.(ClassBuilder); ok {
    typ, err = clsBuilder.Type()
} else {
    return fmt.Errorf("expected class builder, got %T", builder)
}
Defensive patterns

Strategy: type-guard

Validate before calling

func isClassBuilder(b any) bool {
	_, ok := b.(ClassBuilder)
	return ok
}

Type guard

func toClassBuilder(b any) (ClassBuilder, error) {
	cb, ok := b.(ClassBuilder)
	if !ok {
		return nil, fmt.Errorf("not a class builder: %T", b)
	}
	return cb, nil
}

Try / catch

typ, err := builder.Type()
if err != nil {
	if strings.Contains(err.Error(), "unexpected type for class type") {
		return fmt.Errorf("wrong builder kind used: %w", err)
	}
	return err
}

Prevention

When it happens

Trigger: Calling Type() on a class builder result whose underlying representation is not a class Type — e.g. using the builder API on a type wrapper that resolves to an enum or primitive, or passing the wrong builder object type through the generic internal call path.

Common situations: Confusing class builders with enum/union builders in generated client code, refactoring generated code by hand, or calling Type() on the wrong builder returned from a registry lookup.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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