BoundaryML/baml · error
nil collector found in collectors
Error message
nil collector found in collectors
What it means
When encoding the Collectors list for a function call, each collector must be a valid raw object handle. A nil element in the Collectors slice cannot be encoded as a BamlObjectHandle, so this explicit error is returned before crossing the FFI boundary.
Solutions
- Filter out nil entries before assigning args.Collectors
- Use collectory.NewCollector (the constructor) for each collector rather than zero-value structs
- Check that the collector creation call returned no error and a non-nil value
- Only pass args.Collectors when len > 0 and all entries are non-nil
Example fix
// before
collectors := make([]Collector, 2)
collectors[0] = c
args.Collectors = collectors // collectors[1] is nil
// after
collectors := []Collector{c}
args.Collectors = collectors Defensive patterns
Strategy: validation
Validate before calling
func hasNilCollector(cs []Collector) bool {
for _, c := range cs { if c == nil { return true } }
return false
} Try / catch
if hasNilCollector(collectors) {
return errors.New("collectors contains a nil entry")
}
// then proceed with args.Collectors = collectors Prevention
- Append collectors with append(nil-slice, c...) instead of preallocating with make(..., n)
- Check constructor errors so collector variables are never nil
- Only set args.Collectors when all entries are non-nil
When it happens
Trigger: Passing a Collectors slice to BamlFunctionArguments that contains a nil entry — e.g. building the slice with make([]Collector, n) and only partially filling it, or appending a zero-valued collector.
Common situations: Preallocating a collectors slice with make(..., 2) but registering only one; a collector variable that failed to initialize earlier (err ignored); copying collectors between calls with a nil placeholder.
Related errors
- encoding collector
- decodeLiteralValue: valueLiteral is nil
- destructor returned unexpected result
- encoding client registry
- encoding env vars
AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12).
Data as JSON: /api/errors/eeb08313df0f5e49.
Report an issue: GitHub.
Appendix: source
Thrown at engine/language_client_go/pkg/rawobjects_function_args.go:55
clientRegistry, err = encodeClientRegistry(args.ClientRegistry)
if err != nil {
return nil, fmt.Errorf("encoding client registry: %w", err)
}
}
var env []*cffi.HostEnvVar
if args.Env != nil {
env, err = serde.EncodeEnvVar(args.Env)
if err != nil {
return nil, fmt.Errorf("encoding env vars: %w", err)
}
}
var collectors []*cffi.BamlObjectHandle
if args.Collectors != nil {
for _, collector := range args.Collectors {
if collector == nil {
return nil, fmt.Errorf("nil collector found in collectors")
}
encodedCollector := raw_objects.EncodeRawObject(collector)
if err != nil {
return nil, fmt.Errorf("encoding collector: %w", err)
}
collectors = append(collectors, encodedCollector)
}
}
var typeBuilder *cffi.BamlObjectHandle
if args.TypeBuilder != nil {
encodedTypeBuilder := raw_objects.EncodeRawObject(args.TypeBuilder)
if err != nil {
return nil, fmt.Errorf("encoding type builder: %w", err)
}
typeBuilder = encodedTypeBuilder
}
View on GitHub (pinned to bd85ce9dee)