BoundaryML/baml · error

failed to decode object at index %d: %w

Error message

failed to decode object at index %d: %w

What it means

When the FFI response contains multiple objects (InvocationResponseSuccess_Objects), each is decoded with decodeRawObject; if any element fails, the whole decode fails with this message naming the failing index and the wrapped cause. It indicates one element of a multi-object result could not be converted into a Go RawPointer.

Source

Thrown at engine/language_client_go/baml_go/raw_objects/utils.go:223

		return nil, fmt.Errorf("nil response")
	}

	switch response.GetResponse().(type) {
	case *cffi.InvocationResponse_Error:
		return nil, fmt.Errorf("%s", response.GetError())
	case *cffi.InvocationResponse_Success:
		success := response.GetSuccess()
		switch success.Result.(type) {
		case *cffi.InvocationResponseSuccess_Object:
			object := success.GetObject()
			return decodeRawObject(rt, object)
		case *cffi.InvocationResponseSuccess_Objects:
			objects := success.GetObjects()
			parsed := make([]RawPointer, len(objects.Objects))
			for i, obj := range objects.Objects {
				decoded, err := decodeRawObject(rt, obj)
				if err != nil {
					return nil, fmt.Errorf("failed to decode object at index %d: %w", i, err)
				}
				parsed[i] = decoded
			}
			return parsed, nil
		case *cffi.InvocationResponseSuccess_Value:
			value := success.GetValue()
			nilType := reflect.TypeOf((*nilObject)(nil)).Elem()
			decodedValue, goType := serde.Decode(value, serde.NewInternalTypeMap(map[string]reflect.Type{
				"INTERNAL.nil": nilType,
			}))
			if goType == nilType {
				return nil, nil
			}
			return decodedValue.Interface(), nil
		default:
			panic("unexpected cffi.isCFFIObjectResponseSuccess_Result")
		}
	default:

View on GitHub (pinned to bd85ce9dee)

Solutions

  1. Inspect the wrapped %w cause for the actual per-object decode failure
  2. Log the response objects to find which index is nil or malformed
  3. Regenerate/upgrade baml_client if a new object type lacks a decoder
  4. Report a repro to BAML maintainers if the native side returns nil elements unexpectedly
Defensive patterns

Strategy: try-catch

Try / catch

parsed, err := decodeObjectResponse(rt, resp)
if err != nil {
  var idxErr *IndexDecodeError
  if errors.As(err, &idxErr) { log.Printf("failed at object %d", idxErr.Index) }
  return err
}

Prevention

When it happens

Trigger: CallMethod or NewRawObject returning a batch of objects where decodeRawObject fails for element i — typically a nil or invalid BamlObjectHandle for that element.

Common situations: Native runtime returned a partially invalid object list, null entries inside the objects array, or a handle pointing to a type with no registered Go decoder.

Understand the failure class

Related errors


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