# Weird behavior with variable length attributes

**URL:** <https://forum.tiledb.com/t/weird-behavior-with-variable-length-attributes/508>\
**Category:** Uncategorized\
**Created:** [October 18, 2022, 3:02pm UTC](https://forum.tiledb.com/t/weird-behavior-with-variable-length-attributes/508 "2022-10-18T15:02:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yortuc](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/yortuc/32/245_2.png) [@yortuc](https://forum.tiledb.com/u/yortuc)\
**Post date:** [October 18, 2022, 3:02pm UTC](https://forum.tiledb.com/t/weird-behavior-with-variable-length-attributes/508/1 "2022-10-18T15:02:41Z")

</div>

Hi,

I am experimenting with variable length attributes using Python bindings. I am experiencing some unexpected behaviors. For instance, if all the subarrays have the same length, I get a buffer conversion error.

```auto
# Array creation
ctx = tiledb.default_ctx()

dim = tiledb.Dim(name="dimension", domain=(0, 10000), tile=100, dtype=np.int64)
domain = tiledb.Domain(dim)

attr = tiledb.Attr(name="attr", dtype=np.int64, var=True)

schema = tiledb.ArraySchema(domain=domain, sparse=False, attrs=[attr])
tiledb.Array.create(array_path, schema, overwrite=True)

```

```auto
# fails if all subarrays have the same length here
x = [
    np.array([0,1,2,3], dtype=np.int64),
    np.array([0,1,2,3], dtype=np.int64),
] 

with tiledb.open(array_path, 'w') as A:
    A[0 : len(x)] = np.array(x, dtype='O')

```

And then I get `TileDBError: Failed to convert buffer for attribute: 'attr'`.  
But, even if one sub-array has a different size, it works.

And also if only one sub-array is provided, regardless of its content, I get the same buffer conversion error.

```auto
# fails
x = [
    np.array([0,1,2,3], dtype=np.int64)
]

with tiledb.open(array_path, 'w') as A:
    A[0 : len(x)] = np.array(x, dtype='O')

```

Am I missing something here or maybe it is a bug?

Thanks.

---

<div class="post-metadata">

**Author:** ![nguyenv](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/nguyenv/32/127_2.png) [@nguyenv](https://forum.tiledb.com/u/nguyenv)\
**Post date:** [October 18, 2022, 6:11pm UTC](https://forum.tiledb.com/t/weird-behavior-with-variable-length-attributes/508/2 "2022-10-18T18:11:15Z")

</div>

Hi @yortuc,

This is a [known limitation](https://github.com/TileDB-Inc/TileDB-Py/issues/494) where Numpy coalesces homogenous subarrays.

My recommended workaround would be to add `None` at the end of `x` and slice it away when writing to the array.

```auto
x = [
    np.array([0,1,2,3], dtype=np.int64),
    np.array([0,1,2,3], dtype=np.int64),
    None
] 

with tiledb.open(array_path, 'w') as A:
    A[0 : len(x)-1] = np.array(x, dtype='O')[:-1]

```

Thanks.

---

<div class="post-metadata">

**Author:** ![yortuc](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.tiledb.com/yortuc/32/245_2.png) [@yortuc](https://forum.tiledb.com/u/yortuc)\
**Post date:** [October 19, 2022, 10:57am UTC](https://forum.tiledb.com/t/weird-behavior-with-variable-length-attributes/508/3 "2022-10-19T10:57:19Z")

</div>

Hi,

It works, thanks a lot.
