BoundaryML/baml · error

unexpected type for usage: %T

Error message

unexpected type for usage: %T

What it means

LLMCall.Usage() asserts the runtime result implements the Usage interface; this error fires when the deserialized FFI value is a different type (%T shows it). It means the runtime returned a present-but-unexpected payload for the usage attribute — a Go-binding/runtime contract mismatch. Nil results are handled earlier and return (nil, nil).

Source

Thrown at engine/language_client_go/pkg/rawobjects_llm_call.go:108

		return nil, fmt.Errorf("unexpected type for http response: %T", result)
	}

	return response, nil
}

func (l *llmCall) Usage() (Usage, error) {
	result, err := raw_objects.CallMethod(l, "usage", nil)
	if err != nil {
		return nil, fmt.Errorf("failed to get usage: %w", err)
	}

	if result == nil {
		return nil, nil
	}

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

	return usage, nil
}

func (l *llmCall) Selected() (bool, error) {
	result, err := raw_objects.CallMethod(l, "selected", nil)
	if err != nil {
		return false, fmt.Errorf("failed to get selected: %w", err)
	}

	selected, ok := result.(bool)
	if !ok {
		return false, fmt.Errorf("unexpected type for selected: %T", result)
	}

	return selected, nil
}

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Upgrade the Go bindings and BAML runtime to matching versions and regenerate.
  2. Note the actual type from %T and report the mismatch if it persists across versions.
  3. Degrade gracefully in metrics/tracing code: log and skip usage rather than failing.
  4. Verify the object is an LLM call from the same runtime instance you are querying.

Example fix

// before
usage, err := call.Usage()
if err != nil { return err }
record(usage)

// after
usage, err := call.Usage()
if err != nil {
    log.Printf("usage type mismatch: %v", err)
    return nil
}
record(usage)
Defensive patterns

Strategy: type-guard

Type guard

func isUsage(v interface{}) (baml.Usage, bool) {
    u, ok := v.(baml.Usage)
    return u, ok
}

Try / catch

usage, err := call.Usage()
if err != nil {
    log.Printf("usage type mismatch: %v", err)
    return nil
}

Prevention

When it happens

Trigger: raw_objects.CallMethod(l, "usage", nil) returns a non-nil value failing the usage.(Usage) assertion at rawobjects_llm_call.go:106-109 — e.g. a raw CFFI wrapper or an unrelated BAML object type.

Common situations: Version skew between Go bindings and native runtime changing the usage encoding; a runtime bug returning a wrapped value instead of the Usage object; objects from mismatched runtime instances.

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