BoundaryML/baml · error

encoding %s field '%s': %w

Error message

encoding %s field '%s': %w

What it means

EncodeMapEntries is the public entry point that encodes a map of named fields into HostMapEntries for the BAML FFI layer. When encoding any single value fails, the error is wrapped with the context string and field name so developers know exactly which field was the problem. It is used by CallMethod, EncodeClass, client registry encoding, and media construction.

Source

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

		default:
			return nil, fmt.Errorf("unsupported map key type: %s", key.Kind())
		}
	}

	return &cffi.HostMapValue{
		Entries: entries,
	}, nil
}

// Helper function to encode map entries into a vector offset
func EncodeMapEntries(fields map[string]any, context string) ([]*cffi.HostMapEntry, error) {
	entries := make([]*cffi.HostMapEntry, 0, len(fields))
	// Build entries (order doesn't strictly matter, but need to build bottom-up)
	for k, v := range fields {
		key := k
		valueHolder, err := encodeValue(v)
		if err != nil {
			return nil, fmt.Errorf("encoding %s field '%s': %w", context, k, err)
		}

		entries = append(entries, &cffi.HostMapEntry{
			Key:   &cffi.HostMapEntry_StringKey{StringKey: key},
			Value: valueHolder,
		})
	}

	return entries, nil
}

func EncodeValue(value any) (*cffi.HostValue, error) {
	return encodeValue(value)
}

func EncodeEnvVar(fields map[string]string) ([]*cffi.HostEnvVar, error) {
	if len(fields) == 0 || fields == nil {
		return nil, nil

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Read the wrapped inner error and the field name reported ('encoding <ctx> field '<k>') to locate the offending value.
  2. Replace the offending field value with a supported type (string, number, bool, slice, map[string]..., or encodable class).
  3. For media, verify the URL/base64 input is valid before calling the helper.
  4. Encode fields individually with EncodeMapEntries per-field during debugging to isolate the failing value.

Example fix

// before
fields := map[string]any{"cb": make(chan int)}
entries, err := serde.EncodeMapEntries("MyClass", fields) // fails on 'cb'

// after
fields := map[string]any{"cbStatus": "active"} // encode supported representation
entries, err := serde.EncodeMapEntries("MyClass", fields)
Defensive patterns

Strategy: try-catch

Validate before calling

func validateEncodable(v any, depth int) error {
	if depth > 10 {
		return fmt.Errorf("too deep")
	}
	rv := reflect.ValueOf(v)
	switch rv.Kind() {
	case reflect.Chan, reflect.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer:
		return fmt.Errorf("unsupported kind %s", rv.Kind())
	}
	return nil
}

Type guard

func isEncodable(v any) bool {
	switch reflect.ValueOf(v).Kind() {
	case reflect.Chan, reflect.Func, reflect.Complex64, reflect.Complex128, reflect.UnsafePointer:
		return false
	}
	return true
}

Try / catch

valueHolder, err := encodeValue(v)
if err != nil {
	var encErr *serde.EncodeError
	if errors.As(err, &encErr) {
		log.Printf("field %q failed: %v", fieldName, err)
	}
	return err
}

Prevention

When it happens

Trigger: Any nested value inside the fields map fails encodeValue: e.g. passing a map with an unsupported key type (error 1300), an unsupported value kind, or a failing nested EncodeClass. Occurs via baml.CallMethod, class encoding, encodeClientRegistry, NewCollector, or newMediaFromUrl/newMediaFromBase64.

Common situations: A field value is of a Go type the serde layer can't encode (chan, func, complex, cyclic structure); a media URL/base64 helper receives data that fails downstream encoding; a client registry entry contains an unsupported type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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