WARNINGtensorflowAutoGraph could not transform function format_example at ... and will run it as-is
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The warning WARNING:tensorflow:AutoGraph could not transform function <name> and will run it as-is appears when TensorFlow's AutoGraph system cannot convert a Python function into a TensorFlow graph operation. AutoGraph normally converts Python control flow (if, for, while) into equivalent TensorFlow ops (tf.cond, tf.while_loop). When it fails, the function runs in eager mode instead, which is slower but functionally correct.
What AutoGraph Does
When you decorate a function with @tf.function, AutoGraph automatically converts Python code to TensorFlow graph operations:
Behind the scenes, AutoGraph rewrites the if statement into tf.cond(x > 0, lambda: x * 2, lambda: x * -1).
Why the Warning Occurs
AutoGraph fails to transform a function when it encounters Python constructs that cannot be converted to graph operations:
1. Unsupported Python Features
2. External Library Calls
3. Complex String Formatting
4. Python-Level Side Effects
Fix 1: Move Unsupported Code Outside @tf.function
Fix 2: Use TensorFlow Operations Instead of Python
Fix 3: Use tf.py_function for Unavoidable Python Code
When you must call Python functions inside a graph:
tf.py_function wraps a Python callable as a TensorFlow operation. It runs eagerly inside the graph, so it is slower than native TF ops.
Fix 4: Suppress the Warning
If the function works correctly in eager mode and you want to silence the warning:
Or suppress only AutoGraph warnings:
When Is the Warning Safe to Ignore?
The warning is safe to ignore when:
- The function is not in a performance-critical loop
- The function is called infrequently (e.g., data preprocessing, logging)
- You are in development/debugging mode and will optimize later
The warning matters when:
- The function is called in a training loop (significant performance impact)
- You are deploying a SavedModel (eagerly-executed functions may not serialize)
- You need GPU acceleration (eager execution does not benefit from XLA compilation)
Performance Impact
| Execution Mode | Speed | GPU Support | Serializable |
Graph mode (@tf.function) | Fast | Full | Yes |
| Eager mode (fallback) | Slower | Limited | No |
tf.py_function | Slowest | No | Yes (with caveats) |
Common Pitfalls
- Silent performance regression: The warning means the function runs eagerly, which can be 2-10x slower than graph mode. In training loops, this significantly impacts throughput. Always fix AutoGraph warnings for code in the training loop.
- tf.py_function shape issues:
tf.py_functiondoes not propagate shape information. You must callresult.set_shape()after the call, or downstream operations that need static shapes will fail. - Retracing:
@tf.functionretraces when called with different input signatures (shapes, dtypes). Each retrace triggers AutoGraph conversion. If conversion fails each time, you get repeated warnings. - Mixing eager and graph: Calling
.numpy()on a tensor inside@tf.functionfails because.numpy()requires eager execution. Use TensorFlow operations instead. - Debugging difficulty: Graph-mode errors are harder to debug than eager-mode errors. During development, remove
@tf.functionto debug, then add it back for performance.
Summary
- The AutoGraph warning means TensorFlow cannot convert a Python function to graph operations — it falls back to eager execution
- Common causes:
try/except, Pythonprint, NumPy operations, list mutations, and f-string formatting on tensors - Fix by replacing Python operations with TensorFlow equivalents (
tf.print,tf.reduce_mean, etc.) - Use
tf.py_functionto wrap unavoidable Python code inside a graph - Always fix this warning for functions in the training loop — eager fallback is significantly slower

