Python
TensorFlow
ValueError
data transformation
machine learning

ValueError Can't convert non-rectangular Python sequence to Tensor

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This TensorFlow error means your nested Python data does not form a proper rectangle. In practice, one row is longer than another, or one nested element has a different inner shape than its neighbors. A normal dense tensor requires every item along a dimension to have the same length, so inconsistent nested lists cannot be converted directly.

What "Non-Rectangular" Means

A rectangular 2D structure looks like this:

python
[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]]

Every row has length 3, so TensorFlow can represent it as a dense tensor with shape (3, 3).

A non-rectangular structure looks like this:

python
[[1, 2, 3],
 [4, 5],
 [6, 7, 8, 9]]

Now the rows have different lengths. There is no single dense rectangular shape that fits all rows, so this fails:

python
1import tensorflow as tf
2
3bad = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
4tf.constant(bad)

That is the direct cause of the error.

Fix 1: Make The Data Rectangular

If the data is supposed to be dense, fix the input so every row has the same width.

python
1import tensorflow as tf
2
3rows = [[1, 2, 3], [4, 5, 0], [6, 7, 8]]
4tensor = tf.constant(rows)
5
6print(tensor)
7print(tensor.shape)

This is the right fix when the irregular shape is simply bad input data.

Fix 2: Pad Shorter Sequences

In machine learning, variable-length sequences are common. If you need a dense tensor anyway, pad the shorter rows to a consistent length.

python
1import tensorflow as tf
2
3sequences = [[1, 2, 3], [4, 5], [6]]
4max_len = max(len(row) for row in sequences)
5
6padded = [row + [0] * (max_len - len(row)) for row in sequences]
7tensor = tf.constant(padded)
8
9print(tensor)

Output shape is now uniform. Padding is common for token sequences, time series windows, and batch preparation.

TensorFlow also provides helpers in some workflow layers, but understanding the padding idea directly is more important than memorizing a utility call.

Fix 3: Use A Ragged Tensor When Variable Length Is Real

Sometimes the data is genuinely variable-length and should stay that way. In that case, use a ragged tensor instead of forcing it into a dense tensor.

python
1import tensorflow as tf
2
3values = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
4rt = tf.ragged.constant(values)
5
6print(rt)
7print(rt.shape)

A RaggedTensor is the correct representation when row lengths differ naturally. This is common for:

  • tokenized text before padding
  • click sequences of different lengths
  • neighbor lists in graph-like data
  • nested examples with optional variable-size features

Do not pad just because the error told you dense conversion failed. Use ragged structure when the variable length is meaningful.

Dense Tensor Versus Ragged Tensor

Choose based on the downstream operations.

Use a dense tensor when:

  • the model or op expects fixed shapes
  • padding is acceptable
  • batch operations are easier with uniform width

Use a ragged tensor when:

  • the varying lengths matter semantically
  • you want to avoid unnecessary padding
  • downstream TensorFlow operations support ragged inputs

The right representation depends on the pipeline, not just on what makes the error disappear.

Debug The Shape Before Conversion

A simple debugging step is to inspect row lengths before calling TensorFlow.

python
rows = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
print([len(row) for row in rows])

If those lengths are not all the same, you already know why tf.constant will fail.

This kind of check is especially helpful when the data comes from parsing JSON, CSV records, or user-generated nested structures.

Another Common Cause: Mixed Nested Shapes

The problem is not limited to plain lists of numbers. It can also happen when some items are lists and others are scalars, or when inner arrays have different dimensions.

python
bad = [[1, 2], [3, 4], 5]

This is also non-rectangular because the third item does not match the structure of the first two items.

Common Pitfalls

  • Assuming TensorFlow will automatically pad irregular nested lists into a dense tensor.
  • Padding data when the correct representation should really be a RaggedTensor.
  • Fixing the outer list shape while ignoring inconsistent inner dimensions deeper in the structure.
  • Debugging dtype problems when the real failure is shape inconsistency.
  • Passing parsed JSON directly into tf.constant without checking sequence lengths first.

Summary

  • A dense TensorFlow tensor must have a rectangular shape.
  • The error occurs when nested Python sequences have inconsistent lengths or structure.
  • Fix it by making the data rectangular, padding it, or using tf.ragged.constant.
  • Use dense tensors for fixed-shape workflows and ragged tensors for real variable-length data.
  • Inspect row lengths before conversion to find the mismatch quickly.

Course illustration
Course illustration

All Rights Reserved.