Tensorflow 2 throwing ValueError as_list is not defined on an unknown TensorShape
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The TensorFlow error saying as_list is not defined on an unknown TensorShape appears when code expects a fully known static shape but TensorFlow only has dynamic shape information at that point in graph execution. This is common in custom layers, tf.data pipelines, and tracing with tf.function. The fix is to use dynamic shape-safe operations and provide shape hints where possible.
Why This Error Happens
TensorFlow tracks shapes in two ways:
- static shape metadata known at graph build time
- dynamic runtime shape available only during execution
tensor.shape and tensor.shape.as_list() rely on static metadata. If dimension values are unknown, as_list() can fail.
A typical failing pattern:
Inside traced functions, static shape information can be incomplete, especially for batch dimension and dynamically sized inputs.
Prefer tf.shape for Dynamic Execution
When shape values are needed for computation, use tf.shape.
tf.shape returns runtime tensor values and works even when static metadata is incomplete.
Use Shape Hints to Reduce Unknown Dimensions
Where possible, provide expected shape constraints to TensorFlow early.
For datasets, use output_signature or explicit tensor specs.
These hints improve graph building and reduce shape ambiguity.
Custom Layer Pattern That Avoids as_list Issues
In custom Keras layers, avoid shape assumptions in call that require fully known static values.
If you need static shape for weight creation, do that in build, where input shape is provided.
Mixed NumPy and TensorFlow Gotcha
Another trigger is mixing NumPy shape assumptions with TensorFlow symbolic tensors.
Unsafe pattern:
- convert shape to Python list during graph tracing
- use list values for tensor operations
Safer pattern:
- keep shape operations in TensorFlow tensors using
tf.shape - only convert to Python integers outside traced functions when guaranteed concrete
Debugging Workflow
When this error appears, isolate where as_list is called.
Practical steps:
- search for
.shape.as_list()in custom code - replace runtime-dependent uses with
tf.shape - add
print(tensor.shape)before failing location to inspect static metadata - add input signatures or tensor specs to reduce unknown dimensions
- test both eager mode and
tf.functionmode
This usually reveals whether failure is due to tracing context or missing input shape contracts.
Performance and Stability Notes
Using tf.shape for runtime computations is not just safer, it is often the correct graph-friendly approach. However, avoid excessive shape operations in inner loops when not needed. Keep shape logic minimal and consistent.
Also validate models with variable batch sizes, since unknown batch dimensions are a common source of shape-related errors.
Common Pitfalls
- Calling
as_list()inside traced code where static shape is partially unknown. - Assuming Keras input shape metadata is always fully defined at call time.
- Building custom layers that mix static and dynamic dimensions incorrectly.
- Defining
tf.datapipelines withoutoutput_signature, causing shape ambiguity. - Converting symbolic dimensions to Python integers too early.
Summary
- The error means TensorFlow lacks complete static shape information for
as_list. - Use
tf.shapefor runtime-dependent dimension logic. - Provide input and dataset shape hints to improve graph inference.
- Keep custom layer
buildandcallresponsibilities clear for shape handling. - Debug by finding static-shape assumptions and replacing them with dynamic-safe operations.

