The name tf.Session is deprecated. Please use tf.compat.v1.Session instead
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
This warning appears when TensorFlow 1.x session-based code is run in a TensorFlow 2 environment. TensorFlow 2 defaults to eager execution, so tf.Session is no longer the main API; legacy graph code must go through the compatibility namespace, or better yet, be rewritten in the TensorFlow 2 style.
Why tf.Session Was Deprecated
TensorFlow 1.x used a two-step model:
- build a graph of operations
- execute that graph inside a session
TensorFlow 2 changed the default model to eager execution, where operations run immediately like ordinary Python code. Because of that shift, tf.Session stopped being the preferred entry point.
Old TensorFlow 1 style:
In TensorFlow 2, that code raises the deprecation message because the session API now lives under tf.compat.v1.
The Short-Term Fix for Legacy Code
If you need old code to keep running while you migrate, use the compatibility API and disable eager execution:
This is the correct bridge for TensorFlow 1.x style code that still depends on placeholders, sessions, and graph execution.
If your script also uses placeholders, update those too:
That preserves the old execution model intentionally.
The Better Long-Term Fix
For new work, stop writing session-based TensorFlow entirely. In TensorFlow 2, the equivalent code is much simpler:
The value is computed immediately. No session, no graph initialization step, and no run() call are needed.
That change also makes debugging easier. Ordinary Python control flow, print, exceptions, and step-by-step inspection behave much more naturally in eager mode than they did in large TensorFlow 1 graphs.
For model training, the preferred path is Keras:
That is the API surface TensorFlow expects you to use now.
When You Still Need tf.compat.v1.Session
There are still legitimate reasons to keep the compatibility layer for a while:
- large legacy codebases built around placeholders and graphs
- old research code that was never migrated
- imported code that depends on
tf.compat.v1APIs already
In those cases, the compatibility namespace is not a hack. It is the supported transition path. The mistake is treating it as the destination rather than the bridge.
Common Pitfalls
The most common mistake is replacing tf.Session with tf.compat.v1.Session but forgetting to disable eager execution. Some old graph-based code still behaves incorrectly unless the execution mode matches TensorFlow 1 semantics.
Another issue is mixing TensorFlow 1 patterns and TensorFlow 2 idioms in the same script without understanding which execution model is active. That creates confusing errors around tensors, placeholders, and evaluation.
Developers also sometimes keep migrating surface syntax while leaving the architecture unchanged. If the project is modernizing anyway, move toward eager execution and Keras rather than freezing the whole codebase in compatibility mode forever.
Summary
- '
tf.Sessionis deprecated because TensorFlow 2 uses eager execution by default.' - Use
tf.compat.v1.Session()only for legacy graph-based code. - Disable eager execution when old TensorFlow 1 semantics are required.
- For new code, execute tensors directly and use Keras APIs.
- Treat
tf.compat.v1as a migration bridge, not the long-term destination.
Related reading
- The print of string constant is always attached with 'b' inTensorFlow
- The result of fft in tensorflow is different from numpy
- This TensorFlow binary is optimized with IntelR MKL-DNN to use the following CPU instructions in performance critical
- This version of TensorFlow Probability requires TensorFlow version 2.3
- TimeDistributed vs. TimeDistributedDense Keras
- TimeDistributedDense vs Dense in Keras - Same number of parameters
- Tracking tensor shape at graph creation time
- Train multi-class image classifier in Keras
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.