Skip to content

[Data] Fix TensorDtype.__from_arrow__ crash on empty tensor columns - #64767

Merged
elliot-barn merged 3 commits into
ray-project:masterfrom
marwan116:data-fix-from-arrow-empty-tensor-64766
Jul 16, 2026
Merged

elliot-barn merged 3 commits into
ray-project:masterfrom
marwan116:data-fix-from-arrow-empty-tensor-64766

Conversation

@marwan116

Copy link
Copy Markdown
Contributor

Description

Dataset.to_pandas() (and any Arrow → pandas batch conversion) crashes on Ray 2.56 when a tensor-extension column has zero-size elements, i.e. element_shape contains a 0. This happens naturally when a per-frame / per-group operator emits an empty tensor for some rows (e.g. a detection op that finds nothing in a frame).

TensorDtype.__from_arrow__ reshapes the reconstructed values with a -1 dimension:

values = values.reshape((-1,) + self.element_shape)

numpy cannot infer a -1 dimension from a size-0 array when another dimension is 0, so this raises:

ValueError: cannot reshape array of size 0 into shape (0)

Minimal repro (fails on 2.56.0, worked on 2.55):

import ray, numpy as np
ray.data.from_items([{"pts": np.zeros((0,), np.float32)} for _ in range(3)]).to_pandas()

This regressed in 2.56 via #61733, which added the reshape (2.55's __from_arrow__ simply returned TensorArray(values) with no reshape).

Related issues

Closes #64766.

Additional information

The fix uses the explicit row count instead of -1 (numpy no longer has to infer a dimension), and builds the empty buffer directly for the zero-row object-dtype case, since np.stack requires at least one array:

num_rows = len(array)
if values.dtype == object:
    values = (
        np.stack(values)
        if num_rows > 0
        else np.empty((0,) + self.element_shape, dtype=self._dtype)
    )
values = values.reshape((num_rows,) + self.element_shape)

Added test_tensors_in_tables_pandas_roundtrip_empty_elements, parametrized over element shapes (0,) and (0, 2) plus the existing tensor-format (V1 / V2 / ARROW_NATIVE) and auto-cast fixtures. Verified locally that the V1 and V2 paths crash before this fix and pass after; the native pa.fixed_shape_tensor path did not crash (it does not route through __from_arrow__) and continues to work.

@marwan116
marwan116 requested a review from a team as a code owner July 15, 2026 10:33

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where empty columns or per-row tensors with zero-size dimensions cause errors during Pandas round-trips. It replaces the use of -1 in reshape with an explicit row count and safely handles empty object arrays. A regression test is also added. The reviewer suggested simplifying the handling of empty arrays when num_rows == 0 to avoid unnecessary reshaping and ensure the correct data type is always returned.

Converting a tensor-extension column to pandas crashed when the column
had zero-size elements (element_shape containing a 0, e.g. an operator
emitting empty per-row tensors). `TensorDtype.__from_arrow__` reshaped
with a `-1` dimension, which numpy cannot infer from a size-0 array,
raising `ValueError: cannot reshape array of size 0 into shape (0)`.

Reshape with the explicit row count instead of `-1`, and short-circuit
the empty-block (num_rows == 0) case by building the correctly typed
empty buffer directly (`np.stack` needs at least one array). Regressed
in 2.56 by ray-project#61733.

Closes ray-project#64766.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Signed-off-by: Marwan Sarieddine <[email protected]>
@marwan116
marwan116 force-pushed the data-fix-from-arrow-empty-tensor-64766 branch from ad0ca67 to d5c8010 Compare July 15, 2026 11:10
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Jul 15, 2026

@ayushk7102 ayushk7102 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the fix!

@goutamvenkat-anyscale goutamvenkat-anyscale added the go add ONLY when ready to merge, run all tests label Jul 15, 2026
@goutamvenkat-anyscale
goutamvenkat-anyscale enabled auto-merge (squash) July 15, 2026 20:28
@github-actions
github-actions Bot disabled auto-merge July 15, 2026 21:51
@elliot-barn
elliot-barn merged commit 34607f7 into ray-project:master Jul 16, 2026
3 checks passed
elliot-barn added a commit that referenced this pull request Jul 16, 2026
…olumns (#64767) (#64794)

Cherry-pick of #64767 onto the `releases/2.56.1` branch.

## Why are these changes needed?
Fixes a 2.56 regression where converting tensor-extension columns to
pandas crashed on zero-size tensor elements. Replaces numpy's `-1`
dimension inference with an explicit row count and builds the buffer
directly for empty arrays instead of using `np.stack`.

## Related issue number
Cherry-pick of #64767

## Checks
Cherry-pick applied cleanly (no conflicts). DCO sign-off added.
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Marwan Sarieddine <[email protected]>
Signed-off-by: elliot-barn <[email protected]>
Co-authored-by: Marwan Sarieddine <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
400Ping pushed a commit to 400Ping/ray that referenced this pull request Aug 18, 2026
…ay-project#64767)

## Description

`Dataset.to_pandas()` (and any Arrow → pandas batch conversion) crashes
on Ray 2.56 when a tensor-extension column has zero-size elements, i.e.
`element_shape` contains a `0`. This happens naturally when a per-frame
/ per-group operator emits an empty tensor for some rows (e.g. a
detection op that finds nothing in a frame).

`TensorDtype.__from_arrow__` reshapes the reconstructed values with a
`-1` dimension:

```python
values = values.reshape((-1,) + self.element_shape)
```

numpy cannot infer a `-1` dimension from a size-0 array when another
dimension is `0`, so this raises:

```
ValueError: cannot reshape array of size 0 into shape (0)
```

Minimal repro (fails on 2.56.0, worked on 2.55):

```python
import ray, numpy as np
ray.data.from_items([{"pts": np.zeros((0,), np.float32)} for _ in range(3)]).to_pandas()
```

This regressed in 2.56 via ray-project#61733, which added the reshape (2.55's
`__from_arrow__` simply returned `TensorArray(values)` with no reshape).

## Related issues

Closes ray-project#64766.

## Additional information

The fix uses the explicit row count instead of `-1` (numpy no longer has
to infer a dimension), and builds the empty buffer directly for the
zero-row `object`-dtype case, since `np.stack` requires at least one
array:

```python
num_rows = len(array)
if values.dtype == object:
    values = (
        np.stack(values)
        if num_rows > 0
        else np.empty((0,) + self.element_shape, dtype=self._dtype)
    )
values = values.reshape((num_rows,) + self.element_shape)
```

Added `test_tensors_in_tables_pandas_roundtrip_empty_elements`,
parametrized over element shapes `(0,)` and `(0, 2)` plus the existing
tensor-format (V1 / V2 / ARROW_NATIVE) and auto-cast fixtures. Verified
locally that the V1 and V2 paths crash before this fix and pass after;
the native `pa.fixed_shape_tensor` path did not crash (it does not route
through `__from_arrow__`) and continues to work.

Signed-off-by: Marwan Sarieddine <[email protected]>
Co-authored-by: Claude Opus 4.8 <[email protected]>
Co-authored-by: Ayush Kumar <[email protected]>
Signed-off-by: 400Ping <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data] 2.56 regression: TensorDtype.__from_arrow__ crashes reconstructing an empty tensor column: cannot reshape array of size 0 into shape (0)

4 participants