TensorFlow
feed_dict
TypeError
Tensor
machine learning

Tensorflow 'feed_dict' using same symbol for key-value pair got 'TypeError Cannot interpret feed_dict key as Tensor'

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

In TensorFlow 1.x, feed_dict is the primary way to pass runtime data into a computation graph through placeholders. A frustrating error that many developers hit is TypeError: Cannot interpret feed_dict key as Tensor. This almost always means the key you passed into the dictionary is not actually a TensorFlow placeholder object -- it is an ordinary Python variable that happens to share the same name. This article explains why the error occurs, walks through the most common causes, and shows how to fix each one.

How feed_dict Works

When you build a TensorFlow 1.x graph, you define placeholder nodes that act as slots for data. At runtime, you create a session and pass a dictionary mapping each placeholder to a concrete value:

python
1import tensorflow as tf
2
3# Build the graph
4x = tf.placeholder(tf.float32, shape=[None, 3], name="x")
5y = tf.placeholder(tf.float32, shape=[None, 1], name="y")
6output = tf.matmul(x, tf.Variable(tf.ones([3, 1])))
7
8# Run the graph
9with tf.Session() as sess:
10    sess.run(tf.global_variables_initializer())
11    result = sess.run(output, feed_dict={x: [[1, 2, 3]]})
12    print(result)

The keys in feed_dict must be TensorFlow Tensor objects (usually placeholders). The values are NumPy arrays or Python lists that match the placeholder's shape and dtype.

Cause 1 -- Reusing a Variable Name

The most common trigger is accidentally overwriting the placeholder variable with a plain Python value:

python
1x = tf.placeholder(tf.float32, name="x")
2
3# ... later in the code ...
4x = 5.0  # x is now a Python float, not a placeholder
5
6sess.run(output, feed_dict={x: [1.0, 2.0]})  # TypeError!

Here x no longer refers to the TensorFlow placeholder. The fix is straightforward -- do not reassign the variable, or use a distinct name:

python
1x_placeholder = tf.placeholder(tf.float32, name="x")
2x_value = 5.0  # separate variable for the Python scalar
3
4sess.run(output, feed_dict={x_placeholder: [1.0, 2.0]})  # works

Cause 2 -- Graph Scope Mismatch

TensorFlow operations belong to a specific graph. If you create a placeholder in one graph and try to feed it in a session bound to a different graph, you get the same error:

python
1g1 = tf.Graph()
2g2 = tf.Graph()
3
4with g1.as_default():
5    a = tf.placeholder(tf.float32, name="a")
6
7with g2.as_default():
8    b = tf.placeholder(tf.float32, name="b")
9    result = b * 2
10
11with tf.Session(graph=g2) as sess:
12    # 'a' belongs to g1, not g2 -- TypeError
13    sess.run(result, feed_dict={a: [1.0]})

The solution is to make sure the placeholder and the session share the same graph. In most applications you should use a single default graph and avoid creating multiple graph objects.

Cause 3 -- Recreating the Graph in a Loop

Another subtle variant happens when graph-building code runs inside a training loop. Each iteration creates new placeholder objects, but feed_dict still references the old ones:

python
1for epoch in range(10):
2    x = tf.placeholder(tf.float32)     # new node each iteration
3    loss = (x - 3) ** 2
4
5# After the loop, x points to the placeholder from epoch 9,
6# but loss also points to the node from epoch 9.
7# Previous placeholders are orphaned.

Build the graph once, outside the loop, then feed values inside the loop:

python
1x = tf.placeholder(tf.float32)
2loss = (x - 3) ** 2
3
4with tf.Session() as sess:
5    for epoch in range(10):
6        current_loss = sess.run(loss, feed_dict={x: 5.0})
7        print(f"Epoch {epoch}: loss = {current_loss}")

Cause 4 -- Passing a String Instead of a Tensor

Sometimes developers pass the placeholder's name string as the key:

python
sess.run(output, feed_dict={"x:0": [1.0]})  # TypeError

While TensorFlow does let you look up tensors by name, feed_dict expects the actual tensor object, not a string. If you only have the name, retrieve the tensor first:

python
x_tensor = tf.get_default_graph().get_tensor_by_name("x:0")
sess.run(output, feed_dict={x_tensor: [1.0]})

The TF2 Migration Path

TensorFlow 2.x removes the need for feed_dict entirely. Eager execution is on by default, so operations execute immediately with regular Python values:

python
1import tensorflow as tf  # TF 2.x
2
3x = tf.constant([1.0, 2.0, 3.0])
4result = x * 2
5print(result.numpy())  # [2. 4. 6.]

For model training, use tf.function and pass data through tf.data.Dataset pipelines or direct function arguments instead of placeholders. If you are maintaining legacy TF1 code, the tf.compat.v1 module preserves the old API, but migrating to TF2 eliminates the entire class of feed_dict errors.

Common Pitfalls

  • Overwriting a placeholder variable with a Python value: This is the number-one cause. Use distinct, descriptive names like x_ph for placeholders so they stand out.
  • Building graph nodes inside a training loop: Each iteration creates duplicate nodes and orphans the previous placeholders. Always separate graph construction from the training loop.
  • Using multiple graphs without careful scoping: Placeholders belong to the graph in which they were created. Feeding a placeholder from graph A into a session running graph B triggers the TypeError.
  • Passing a tensor name string as a feed_dict key: Use graph.get_tensor_by_name() to resolve the string to an actual tensor object first.
  • Ignoring the TF2 migration: If you are starting a new project, use TensorFlow 2.x with eager execution. The feed_dict pattern is a legacy mechanism that TF2 replaces with cleaner, less error-prone alternatives.

Summary

  • The TypeError: Cannot interpret feed_dict key as Tensor means the dictionary key is not a valid TensorFlow tensor object.
  • The most frequent cause is accidentally reassigning a placeholder variable to an ordinary Python value.
  • Graph scope mismatches and graph rebuilding inside loops are other common triggers.
  • Always build your graph once, keep placeholder references stable, and use one graph per session.
  • For new projects, migrate to TensorFlow 2.x where eager execution eliminates the need for feed_dict altogether.

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
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
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