BoundaryML/baml · error

encoding list element %d: %w

Error message

encoding list element %d: %w

What it means

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.).

Source

Thrown at engine/language_client_go/baml_go/serde/encode.go:202

				MapValue: encoded,
			},
		}, nil

	default:
		// Use originalValue's type for the error message as it's more accurate to the input
		return nil, fmt.Errorf("unsupported type for BAML encoding: %T (Kind: %s)", originalValue, rv.Kind())
	}
}

// --- Encoding helpers for specific types ---

// encodeList now accepts and passes TypeMap
func encodeList(value reflect.Value) (*cffi.HostListValue, error) {
	values := make([]*cffi.HostValue, value.Len())
	for i := value.Len() - 1; i >= 0; i-- {
		elemOffset, err := encodeValue(value.Index(i).Interface()) // Pass typeMap recursively
		if err != nil {
			return nil, fmt.Errorf("encoding list element %d: %w", i, err)
		}
		values[i] = elemOffset
	}

	return &cffi.HostListValue{
		Values: values,
	}, nil
}

// encodeMap now accepts and passes TypeMap
func encodeMap(mapValue reflect.Value) (*cffi.HostMapValue, error) {

	entries := make([]*cffi.HostMapEntry, 0, mapValue.Len())
	for _, key := range mapValue.MapKeys() {
		value := mapValue.MapIndex(key)
		valueHolder, err := encodeValue(value.Interface())
		if err != nil {
			return nil, fmt.Errorf("encoding map value: %w", err)

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Use the reported index to find and fix or drop the offending element
  2. Validate element types before the call (primitives, maps, serializable types only)
  3. Convert custom element types to supported representations or add serializers
  4. Log the full error chain to see the root cause type

Example fix

// before
items := []any{"ok", time.Now()} // element 1 unsupported
b.Fn(ctx, items)
// after
items := []any{"ok", now.Format(time.RFC3339)}
b.Fn(ctx, items)
Defensive patterns

Strategy: validation

Validate before calling

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 }

Type guard

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 }

Try / catch

if err := b.Fn(ctx, items); err != nil {
  var idx int
  if n, _ := fmt.Sscanf(err.Error(), "encoding list element %d", &idx); n == 1 { /* inspect items[idx] */ }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/789ede790c7b1931. Report an issue: GitHub.