charmbracelet/gum · error

invalid number of columns

Error message

invalid number of columns

What it means

gum table rejects rows that contain more cells than the declared number of columns, since there is no header/style entry to render the extra cells. Rows with fewer cells are padded with empty strings, but extra columns are an unrecoverable mismatch. The library throws this instead of silently dropping data.

Source

Thrown at table/command.go:94

		columns = append(columns, table.Column{
			Title: title,
			Width: width,
		})
	}

	defaultStyles := table.DefaultStyles()
	top, right, bottom, left := style.ParsePadding(o.Padding)

	styles := table.Styles{
		Cell:     defaultStyles.Cell.Inherit(o.CellStyle.ToLipgloss()),
		Header:   defaultStyles.Header.Inherit(o.HeaderStyle.ToLipgloss()),
		Selected: o.SelectedStyle.ToLipgloss(),
	}

	rows := make([]table.Row, 0, len(data))
	for row := range data {
		if len(data[row]) > len(columns) {
			return fmt.Errorf("invalid number of columns")
		}

		// fixes the data in case we have more columns than rows:
		for len(data[row]) < len(columns) {
			data[row] = append(data[row], "")
		}

		for i, col := range data[row] {
			if len(o.Widths) == 0 {
				width := lipgloss.Width(col)
				if width > columns[i].Width {
					columns[i].Width = width
				}
			}
		}

		rows = append(rows, table.Row(data[row]))
	}

View on GitHub (pinned to 4d089f9550)

Solutions

  1. Quote fields that contain commas so they don't split into extra columns
  2. Ensure the header row has the same number of fields as every data row
  3. Preprocess the input (e.g. awk/csvkit) to normalize row lengths before piping
  4. Use --columns to explicitly define a header matching the widest row

Example fix

// before (3 cells vs 2-column header)
echo 'name,age
"Doe, John",30' | gum table
// after — quoted field stays one cell
echo 'name,age
"Doe, John",30' | gum table  # correct if quoting is preserved
Defensive patterns

Strategy: validation

Validate before calling

awk -F',' 'NR==1{n=NF;next} NF!=n{print "row "NR" has "NF" cols, expected "n; exit 1}' data.csv

Try / catch

if ! out=$(gum table < data.csv 2>&1); then
  echo "$out" >&2; exit 1
fi

Prevention

When it happens

Trigger: Running `gum table` where some row in the input has more comma-separated fields than the header/columns list (len(data[row]) > len(columns)).

Common situations: CSV rows containing unquoted commas inside values so they split into extra fields; header row edited/shortened while data rows kept the old width; joining data sources with different column counts.

Related errors


AI-assisted analysis of charmbracelet/gum@4d089f9550 (2026-08-31). Data as JSON: /api/errors/16fa85d8b3e4c444. Report an issue: GitHub.