{"record":{"id":"5d95d2cb26c4bdc0","repo":"BoundaryML/baml","slug":"encoding-collector-w","errorCode":null,"errorMessage":"encoding collector: %w","messagePattern":"encoding collector: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/pkg/rawobjects_function_args.go","lineNumber":59,"sourceCode":"\t}\n\n\tvar env []*cffi.HostEnvVar\n\tif args.Env != nil {\n\t\tenv, err = serde.EncodeEnvVar(args.Env)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding env vars: %w\", err)\n\t\t}\n\t}\n\n\tvar collectors []*cffi.BamlObjectHandle\n\tif args.Collectors != nil {\n\t\tfor _, collector := range args.Collectors {\n\t\t\tif collector == nil {\n\t\t\t\treturn nil, fmt.Errorf(\"nil collector found in collectors\")\n\t\t\t}\n\t\t\tencodedCollector := raw_objects.EncodeRawObject(collector)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"encoding collector: %w\", err)\n\t\t\t}\n\t\t\tcollectors = append(collectors, encodedCollector)\n\t\t}\n\t}\n\n\tvar typeBuilder *cffi.BamlObjectHandle\n\tif args.TypeBuilder != nil {\n\t\tencodedTypeBuilder := raw_objects.EncodeRawObject(args.TypeBuilder)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding type builder: %w\", err)\n\t\t}\n\t\ttypeBuilder = encodedTypeBuilder\n\t}\n\n\tvar tags []*cffi.HostMapEntry\n\tif args.Tags != nil {\n\t\tfor key, value := range args.Tags {\n\t\t\ttags = append(tags, &cffi.HostMapEntry{","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/pkg/rawobjects_function_args.go#L41-L77","documentation":"Each non-nil collector is converted into a CFFI BamlObjectHandle with raw_objects.EncodeRawObject. The encode() code wraps any resulting error with \"encoding collector: %w\", meaning the collector's underlying raw object could not be serialized into a handle for the host runtime.","triggerScenarios":"Encoding a Collector whose raw object pointer is invalid or whose runtime registration failed — typically when the collector was created against a different/older runtime or its handle was already freed/reset.","commonSituations":"Sharing a collector across two BamlRuntime instances; using a collector after the runtime it belongs to was dropped by GC; mixed BAML versions between collector creation and the call site.","solutions":["Create the collector with the same runtime (or via the shared c collector constructors) used for the function call","Hold a Go reference to both the collector and runtime for the call's lifetime to avoid GC of the underlying handle","Recreate the collector immediately before the call if it came from a previous runtime instance","Align BAML Go bindings and runtime versions"],"exampleFix":"// before\noldCollector := oldRuntime.NewCollector(\"logs\") // different runtime\nruntime.CallFunction(ctx, \"Fn\", params, kwargs, args) // encode fails\n// after\ncollector := c.NewCollector(\"logs\") // same runtime / constructor\nruntime.CallFunction(ctx, \"Fn\", params, kwargs, args)","handlingStrategy":"validation","validationCode":"// ensure collector was created successfully before attaching\ncollector, err := c.NewCollector(\"logs\")\nif err != nil || collector == nil { return err }","typeGuard":null,"tryCatchPattern":"err = runtime.CallFunction(ctx, \"Fn\", params, kwargs, args)\nif err != nil && strings.Contains(err.Error(), \"encoding collector\") {\n    return fmt.Errorf(\"recreate collector with the same runtime: %w\", err)\n}","preventionTips":["Create collectors in the same runtime context as the call","Keep Go references to collectors alive until the call finishes","Recreate collectors after runtime restarts instead of reusing old ones"],"tags":["go","serialization","ffi","collectors"],"backgroundTag":"invalid-argument-value","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}