TensorFlow 'InvalidArgumentError: Graph execution error' — Find the Real Cause
Quick answer
InvalidArgumentError: Graph execution error is a generic wrapper — the actual cause is the innermost error in the nested traceback. The three most common are: a shape mismatch (e.g. batching tensors of different shapes, or a layer input/output shape that doesn't line up), a label out of range for the number of classes (a label >= num_classes with sparse_categorical_crossentropy), and a dtype mismatch. Scroll to the deepest error, print the shapes/dtypes and label range feeding the failing op, and fix that.
Short answer: Graph execution error is a generic wrapper — the real cause is the innermost error in the nested traceback. The three usual ones are a shape mismatch, a label out of range for the number of classes, and a dtype mismatch. Read to the deepest error and fix that op.
InvalidArgumentError: Graph execution error on its own tells you almost nothing — it's the outer wrapper. The message you actually need is nested further down the traceback. Here are the three causes behind most occurrences.
1. Shape mismatch
The classic is batching tensors of different shapes (e.g. images of varying sizes):
InvalidArgumentError: Cannot batch tensors with different shapes in
component 0. First element had shape [408,500,3] and element 1 had shape [360,343,3].
Resize/pad to a common shape before batching:
def preprocess(image, label):
image = tf.image.resize(image, [400, 400])
return image, label
ds = ds.map(preprocess).batch(32)The same class of error appears when a layer's input/output shapes don't line up — check model.summary() and the shape feeding the failing layer.
2. Label out of range (very common)
With sparse_categorical_crossentropy, labels must be 0 .. num_classes-1. A label equal to num_classes (1-indexed labels, or a stray extra class) triggers:
InvalidArgumentError: Received a label value of 10 which is outside the
valid range of [0, 10). Label values: ...
Fix the labels or the final layer's unit count so they agree:
import numpy as np
print(np.min(y), np.max(y)) # must be within [0, num_classes-1]
# e.g. 10 classes -> Dense(10), labels 0..9
outputs = tf.keras.layers.Dense(num_classes, activation="softmax")3. Dtype mismatch
An op fed a dtype it doesn't expect (e.g. integer where float is required). Cast explicitly:
x = tf.cast(x, tf.float32)How to debug any graph execution error
- Scroll to the innermost error — ignore the outer wrapper.
- Print shapes and dtypes of the tensors feeding the failing op.
- For classification, print label
min/maxversus the number of output units. - Reproduce with one batch (
ds.take(1)) to isolate it quickly.
Related guides
Sources
Key takeaways
- •'Graph execution error' is a WRAPPER — read down to the innermost error message for the real cause.
- •Cause 1: shape mismatch — batching different-shaped tensors, or a layer whose input/output shapes don't align.
- •Cause 2: label out of range — a class label >= num_classes with sparse_categorical_crossentropy (extremely common).
- •Cause 3: dtype mismatch — an op fed an unexpected dtype; cast to what it expects.
- •Debug by printing shapes, dtypes, and label min/max feeding the failing op before you train.
Frequently asked questions
What does 'InvalidArgumentError: Graph execution error' mean?
It's a generic wrapper TensorFlow raises when an op fails during graph execution. It doesn't tell you the cause by itself — the real error is nested inside the traceback. Scroll to the innermost message (often 'Cannot batch tensors…', 'Received a label value of N…', or a dtype/shape complaint) and fix that.
Why do I get this with sparse_categorical_crossentropy?
Usually because a label is out of range: with N classes, labels must be 0..N-1. A label equal to N (e.g. classes 0..9 but a label of 10, or 1-indexed labels) triggers 'Received a label value of N which is outside the valid range'. Fix the labels or the final layer's unit count so they match.
How do I debug a graph execution error?
Read to the innermost error, then inspect the tensors feeding the failing op: print their shapes and dtypes, and for classification print the label min/max versus the number of output units. Reproduce with a single batch to isolate it faster.
Software Engineering Leader & Technical Author · Updated August 26, 2026