BoundaryML/baml · error

encoding list: %w

Error message

encoding list: %w

What it means

A slice or array being encoded as a baml function input contained an element that failed to encode; the element error is wrapped with 'encoding list: %w' as it bubbles out of encodeValue. This is a contextual wrapper, not a root cause — the inner error identifies the bad element.

Source

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

	case reflect.Float32, reflect.Float64:
		return &cffi.HostValue{
			Value: &cffi.HostValue_FloatValue{
				FloatValue: rv.Float(),
			},
		}, nil

	case reflect.Bool:
		return &cffi.HostValue{
			Value: &cffi.HostValue_BoolValue{
				BoolValue: rv.Bool(),
			},
		}, nil

	case reflect.Slice, reflect.Array:
		encoded, err := encodeList(rv)
		if err != nil {
			return nil, fmt.Errorf("encoding list: %w", err)
		}
		return &cffi.HostValue{
			Value: &cffi.HostValue_ListValue{
				ListValue: encoded,
			},
		}, nil

	case reflect.Map:
		if rv.Type().Key().Kind() != reflect.String {
			return nil, fmt.Errorf("map key type must be string, got %s", rv.Type().Key().Kind())
		}

		encoded, err := encodeMap(rv)
		if err != nil {
			return nil, fmt.Errorf("encoding map: %w", err)
		}
		return &cffi.HostValue{
			Value: &cffi.HostValue_MapValue{

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Read the innermost wrapped error to find which element/type failed
  2. Ensure list elements are primitives, maps, or types with BamlSerializer/InternalBamlSerializer implementations
  3. Implement or register a serializer for custom element types
  4. Convert unsupported elements to supported representations before the call

Example fix

// before
items := []any{myStruct{}} // struct has no serializer
b.Fn(ctx, items)
// after
items := []any{map[string]any{"a": 1}}
b.Fn(ctx, items)
Defensive patterns

Strategy: validation

Validate before calling

func encodableList(xs []any) bool { for _, x := range xs { if !isEncodable(x) { return false } }; return true }

Type guard

func isEncodable(v any) bool {
  switch v.(type) { case string, int, int64, float64, bool, nil: return true }
  rv := reflect.ValueOf(v)
  switch rv.Kind() { case reflect.Slice, reflect.Array, reflect.Map: return true }
  return false
}

Try / catch

if err := b.Fn(ctx, items); err != nil {
  if strings.Contains(err.Error(), "encoding list element") { /* log index and sanitize list */ }
}

Prevention

When it happens

Trigger: Passing a Go slice/array as a baml function argument (or as a class field) where at least one element is of a type the encoder cannot handle (e.g. a nested struct without a serializer, a Checked value, or an unsupported kind).

Common situations: Lists containing nested custom structs missing BamlSerializer implementations; lists of interfaces holding unsupported dynamic types; media objects inside lists failing their own Encode.

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/b5826604cf8d5e45. Report an issue: GitHub.