{"record":{"id":"8495c5a368aace99","repo":"BoundaryML/baml","slug":"failed-to-get-usage-w","errorCode":null,"errorMessage":"failed to get usage: %w","messagePattern":"failed to get usage: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/pkg/rawobjects_collector.go","lineNumber":30,"sourceCode":"\t*raw_objects.RawObject\n}\n\nfunc newCollector(ptr int64, rt unsafe.Pointer) Collector {\n\treturn &collector{raw_objects.FromPointer(ptr, rt)}\n}\n\nfunc (c *collector) ObjectType() cffi.BamlObjectType {\n\treturn cffi.BamlObjectType_OBJECT_COLLECTOR\n}\n\nfunc (c *collector) pointer() int64 {\n\treturn c.RawObject.Pointer()\n}\n\nfunc (c *collector) Usage() (Usage, error) {\n\tresult, err := raw_objects.CallMethod(c, \"usage\", nil)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get usage: %w\", err)\n\t}\n\n\tusage, ok := result.(Usage)\n\tif !ok {\n\t\treturn nil, fmt.Errorf(\"unexpected type for usage: %T\", result)\n\t}\n\n\treturn usage, nil\n}\n\nfunc (c *collector) Name() (string, error) {\n\tresult, err := raw_objects.CallMethod(c, \"name\", nil)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get name: %w\", err)\n\t}\n\n\tname, ok := result.(string)\n\tif !ok {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/pkg/rawobjects_collector.go#L12-L48","documentation":"Collector.Usage() calls the runtime's `usage` method over FFI and wraps any CallMethod failure as \"failed to get usage: %w\". This is a generic wrapper: the underlying error (unwrapped via errors.Unwrap) carries the real cause — an FFI transport failure, a runtime-side exception in the usage method, or an object-lifetime problem. It indicates the usage data could not be retrieved from the collector's runtime object at all.","triggerScenarios":"Calling collector.Usage() when CallMethod fails: the collector's runtime object was already freed/GC'd, the runtime returned an exception, or the FFI bridge errored before a result could be decoded.","commonSituations":"Holding a Collector across a runtime shutdown or process boundary; calling Usage after Clear or after the BAML runtime was torn down; concurrency issues where the collector pointer becomes invalid.","solutions":["Read the wrapped cause with %v/errors.Unwrap(err) to identify the actual failure before changing code.","Ensure the Collector is used only while the BAML runtime is alive in the same process; recreate the collector if the runtime restarted.","Check for concurrent use of the collector from multiple goroutines and add synchronization.","Retry the call once to rule out a transient FFI hiccup, then investigate runtime logs if it persists."],"exampleFix":"// before\nusage, err := collector.Usage()\n// after\nusage, err := collector.Usage()\nif err != nil {\n    return nil, fmt.Errorf(\"usage unavailable: %v (cause: %v)\", err, errors.Unwrap(err))\n}","handlingStrategy":"try-catch","validationCode":"// Ensure the collector is still tracked by the runtime:\n// if collector == nil { return errors.New(\"collector is nil\") }","typeGuard":"func hasValidCollector(c baml.Collector) bool { return c != nil }","tryCatchPattern":"usage, err := collector.Usage()\nif err != nil {\n    var retryable = strings.Contains(err.Error(), \"failed to get usage\")\n    if retryable {\n        time.Sleep(100 * time.Millisecond)\n        usage, err = collector.Usage()\n    }\n    if err != nil {\n        return nil, fmt.Errorf(\"usage unavailable: %w\", err)\n    }\n}","preventionTips":["Call Usage() only while the BAML runtime is alive in the same process","Don't share collectors across goroutines without synchronization","Recreate collectors after any runtime restart or reload","Always inspect the wrapped cause via errors.Unwrap before reporting"],"tags":["go","ffi","collector","runtime"],"backgroundTag":"api-request-failed","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}