{"record":{"id":"789ede790c7b1931","repo":"BoundaryML/baml","slug":"encoding-list-element-d-w","errorCode":null,"errorMessage":"encoding list element %d: %w","messagePattern":"encoding list element (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"engine/language_client_go/baml_go/serde/encode.go","lineNumber":202,"sourceCode":"\t\t\t\tMapValue: encoded,\n\t\t\t},\n\t\t}, nil\n\n\tdefault:\n\t\t// Use originalValue's type for the error message as it's more accurate to the input\n\t\treturn nil, fmt.Errorf(\"unsupported type for BAML encoding: %T (Kind: %s)\", originalValue, rv.Kind())\n\t}\n}\n\n// --- Encoding helpers for specific types ---\n\n// encodeList now accepts and passes TypeMap\nfunc encodeList(value reflect.Value) (*cffi.HostListValue, error) {\n\tvalues := make([]*cffi.HostValue, value.Len())\n\tfor i := value.Len() - 1; i >= 0; i-- {\n\t\telemOffset, err := encodeValue(value.Index(i).Interface()) // Pass typeMap recursively\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding list element %d: %w\", i, err)\n\t\t}\n\t\tvalues[i] = elemOffset\n\t}\n\n\treturn &cffi.HostListValue{\n\t\tValues: values,\n\t}, nil\n}\n\n// encodeMap now accepts and passes TypeMap\nfunc encodeMap(mapValue reflect.Value) (*cffi.HostMapValue, error) {\n\n\tentries := make([]*cffi.HostMapEntry, 0, mapValue.Len())\n\tfor _, key := range mapValue.MapKeys() {\n\t\tvalue := mapValue.MapIndex(key)\n\t\tvalueHolder, err := encodeValue(value.Interface())\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"encoding map value: %w\", err)","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/engine/language_client_go/baml_go/serde/encode.go#L184-L220","documentation":"Contextual wrapper added by encodeList when element i of a slice/array fails encodeValue. The message includes the element index, and %w preserves the underlying cause (unsupported type, failed internal Encode, etc.).","triggerScenarios":"Any baml call with a list argument (or list-typed class field) where a specific element is unencodable — the index in the message points directly at the culprit element.","commonSituations":"Heterogeneous []any lists with one bad element; lists built from external data containing structs or unsupported kinds; nil-vs-empty confusion (nil pointers are OK, non-nil bad elements are not).","solutions":["Use the reported index to find and fix or drop the offending element","Validate element types before the call (primitives, maps, serializable types only)","Convert custom element types to supported representations or add serializers","Log the full error chain to see the root cause type"],"exampleFix":"// before\nitems := []any{\"ok\", time.Now()} // element 1 unsupported\nb.Fn(ctx, items)\n// after\nitems := []any{\"ok\", now.Format(time.RFC3339)}\nb.Fn(ctx, items)","handlingStrategy":"validation","validationCode":"func checkList(xs []any) error { for i, x := range xs { if err := checkEncodable(x); err != nil { return fmt.Errorf(\"elem %d: %w\", i, err) } }; return nil }","typeGuard":"func isEncodable(v any) bool { switch v.(type) { case string, int, int64, float64, bool, nil: return true }; k := reflect.ValueOf(v).Kind(); return k == reflect.Slice || k == reflect.Map }","tryCatchPattern":"if err := b.Fn(ctx, items); err != nil {\n  var idx int\n  if n, _ := fmt.Sscanf(err.Error(), \"encoding list element %d\", &idx); n == 1 { /* inspect items[idx] */ }\n}","preventionTips":["Validate lists before calls using a shared helper","Log the full wrapped chain to identify root element types","Keep list element types uniform and primitive-or-serializable"],"tags":["go","list","serialization","baml"],"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-14T05:17:10.506Z"}