TensorFlow
tf.Session
deprecated
migration
compatibility

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.

Practice ML system design

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:

python
1import tensorflow as tf
2
3a = tf.constant(2)
4b = tf.constant(3)
5
6with tf.Session() as sess:
7    print(sess.run(a + b))

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:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5a = tf.constant(2)
6b = tf.constant(3)
7
8with tf.compat.v1.Session() as sess:
9    print(sess.run(a + b))

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:

python
1import tensorflow as tf
2
3tf.compat.v1.disable_eager_execution()
4
5x = tf.compat.v1.placeholder(tf.float32)
6y = tf.compat.v1.placeholder(tf.float32)
7
8with tf.compat.v1.Session() as sess:
9    result = sess.run(x + y, feed_dict={x: 4.0, y: 5.0})
10    print(result)

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:

python
1import tensorflow as tf
2
3a = tf.constant(2)
4b = tf.constant(3)
5
6print(a + b)

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:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(8, activation="relu"),
5    tf.keras.layers.Dense(1)
6])
7
8model.compile(optimizer="adam", loss="mse")

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.v1 APIs 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.Session is 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.v1 as a migration bridge, not the long-term destination.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.