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
- Upgrade the Go bindings and BAML runtime to matching versions and regenerate.
- Note the actual type from %T and report the mismatch if it persists across versions.
- Degrade gracefully in metrics/tracing code: log and skip usage rather than failing.
- 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
- Regenerate bindings after every runtime upgrade.
- Log %T on mismatch to pinpoint contract drift.
- Wrap usage reads in tolerant error handling for metrics code.
- Ensure objects come from the same runtime instance.
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
- unexpected type for client name: %T
- unexpected type for provider: %T
- unexpected type for http request: %T
- unexpected type for http response: %T
- failed to get usage: %w
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/aa75e2560af7e5a6.
Report an issue: GitHub.