Skip to content

gr.Dataframe with datatype="auto" crashes on empty list or 1D numpy array #13294

@ParamChordiya

Description

@ParamChordiya

Describe the bug

`gr.Dataframe` with `datatype="auto"` crashes with `IndexError` when given an empty list or a 1D numpy array as the value.

Root cause: In `gradio/components/dataframe.py`, the `set_auto_datatype` method has two crash paths:

1. Empty list (line 583):

elif isinstance(value, list):
    self.datatype = [... for val in value[0]]  # IndexError: list index out of range

2. 1D numpy array (lines 568, 572):

elif isinstance(value, np.ndarray):
    self.datatype = [
        ... str(type(value[0, i]).__name__) ...  # IndexError: too many indices for array
        for i in range(value.shape[1])            # IndexError: tuple index out of range
    ]

A 1D array doesn't support 2D indexing (`value[0, i]`), and `value.shape[1]` fails because a 1D shape tuple has no second element.

Have you searched existing issues?

Yes — no existing issue covers this.

Reproduction

import gradio as gr
import numpy as np

# Crash 1: empty list
gr.Dataframe(value=[], datatype="auto")
# IndexError: list index out of range

# Crash 2: 1D numpy array
gr.Dataframe(value=np.array([1, 2, 3]), datatype="auto")
# IndexError: tuple index out of range

Expected behavior

Both cases should fall back gracefully to the default `"str"` datatype instead of crashing. The component already has an `is_empty` method that properly handles empty inputs — `set_auto_datatype` should use similar guards.

Suggested fix:

elif isinstance(value, np.ndarray):
    if value.ndim < 2 or value.size == 0:
        self.datatype = "str"
        return
    # ... existing logic

elif isinstance(value, list):
    if len(value) == 0:
        self.datatype = "str"
        return
    # ... existing logic

System info

Reproduced on `main` branch, `gradio/components/dataframe.py` lines 564-584.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions