Fixed: NotFoundError: Graph execution error … [[{{node ReadFile}}]] After tf.io.write_file in tf.function
Inside a tf.function, reading a file right after writing it stopped with NotFoundError: Graph execution error, then output/note.txt; No such file or directory and [[{{node ReadFile}}]]. TensorFlow didn't run the read after the write: it ran it first. Making the read wait for the write with tf.control_dependencies fixed it. I reported the problem to TensorFlow, in issue #131029.
What I Was Doing
I was saving a note to a file inside a tf.function, TensorFlow's way of turning a Python function into a graph, and reading it back in the same function to check it. The file name and the text were passed in as tensors:
import tensorflow as tf
@tf.function
def save_and_read(path, text):
tf.io.write_file(path, text)
return tf.io.read_file(path)
path = tf.constant("output/note.txt")
text = tf.constant("Saved inside a tf.function\n")
print(save_and_read(path, text))
The output folder didn't exist yet: tf.io.write_file creates missing folders. I ran the script in a uv project with TensorFlow 2.22.0:
uv run python save_and_read.py
What I Expected
tf.Tensor(b'Saved inside a tf.function\n', shape=(), dtype=string): the note, read back. Outside a tf.function, the same two lines run in order and do exactly that.
What I Got
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
W0000 00:00:1796448570.163976 341063 op_kernel.cc:1934] OP_REQUIRES failed at whole_file_read_ops.cc:120 : NOT_FOUND: output/note.txt; No such file or directory
=== Source Location Trace: ===
external/xla/xla/tsl/platform/errors.cc:181
tensorflow/core/kernels/whole_file_read_ops.cc:44
W0000 00:00:1796448570.164015 341063 local_rendezvous.cc:461] Local rendezvous is aborting with status: NOT_FOUND: output/note.txt; No such file or directory
[[{{node ReadFile}}]]
=== Source Location Trace: ===
external/xla/xla/tsl/platform/errors.cc:181
tensorflow/core/kernels/whole_file_read_ops.cc:44
tensorflow/core/common_runtime/executor.cc:1156
Traceback (most recent call last):
File "/Users/you/source/fx-function-read-file/save_and_read.py", line 12, in <module>
print(save_and_read(path, text))
~~~~~~~~~~~~~^^^^^^^^^^^^
File "/Users/you/source/fx-function-read-file/.venv/lib/python3.14/site-packages/tensorflow/python/util/traceback_utils.py", line 167, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/you/source/fx-function-read-file/.venv/lib/python3.14/site-packages/tensorflow/python/eager/execute.py", line 59, in quick_execute
except TypeError as e:
...<5 lines>...
raise e
tensorflow.python.framework.errors_impl.NotFoundError: Graph execution error:
Detected at node ReadFile defined at (most recent call last):
File "/Users/you/source/fx-function-read-file/save_and_read.py", line 12, in <module>
File "/Users/you/source/fx-function-read-file/save_and_read.py", line 7, in save_and_read
output/note.txt; No such file or directory
[[{{node ReadFile}}]] [Op:__inference_save_and_read_8]
W0000 00:00:1796448570.292629 340986 local_rendezvous.cc:461] Local rendezvous is aborting with status: ABORTED: Shutdown
=== Source Location Trace: ===
./tensorflow/core/common_runtime/eager/rendezvous_cache.h:42

The lines starting with W0000 are TensorFlow's own log: the ReadFile operation failed with NOT_FOUND. "Graph execution error" means the error happened while the function's graph ran, and "Detected at node ReadFile" points at line 7, the tf.io.read_file call.
What I Checked
1. Whether the write happened
It did: after the error, output/note.txt was there, holding the note. The write ran, but the read didn't wait for it.
2. What a second run does
With the file now there from the first run, I ran the script again:
uv run python save_and_read.py
tf.Tensor(b'', shape=(), dtype=string)

No error this time, but no note either: an empty string, b''. The read found the file, but not the text the write was putting in it. The result depends on which of the two happens first.
3. The order in the graph
A tf.function traces the Python code into a graph of operations, and in a graph TensorFlow chooses the order: an operation waits only for the operations it depends on, not for the lines written above it. To see what makes one operation wait for another, I printed every operation in the traced graph, whether TensorFlow marks it as stateful (having effects beyond its result) and which operations it runs after:
import tensorflow as tf
@tf.function
def save_and_read(path, text):
tf.io.write_file(path, text)
return tf.io.read_file(path)
path = tf.constant("output/note.txt")
text = tf.constant("Saved inside a tf.function\n")
graph = save_and_read.get_concrete_function(path, text).graph
for op in graph.get_operations():
print(op.type, "| stateful:", op.op_def.is_stateful, "| runs after:", [c.type for c in op.control_inputs])
uv run python show_graph.py
Placeholder | stateful: False | runs after: []
Placeholder | stateful: False | runs after: []
WriteFile | stateful: True | runs after: []
ReadFile | stateful: False | runs after: []
Identity | stateful: False | runs after: ['NoOp']
NoOp | stateful: False | runs after: ['WriteFile']

get_concrete_function traces the function for these inputs and returns it with its graph. WriteFile is stateful, so the function's final NoOp waits for it, and the returned value, Identity, waits for the NoOp: the write always finishes before the function returns. But ReadFile is not stateful and runs after nothing. Its only input is the file name, which is ready at the start.
4. What TensorFlow's documentation and source say
The tf.control_dependencies API page says: "In TensorFlow 2 with eager and/or Autograph, you should not need this method most of the times, as ops execute in the expected order thanks to automatic control dependencies. Only use it to manually control ordering". TensorFlow's source, in auto_control_deps.py, lists what those automatic dependencies promise: "All stateful ops in the scope will execute" and "Stateful ops which modify the same resource will execute in program order". ReadFile isn't a stateful operation, so neither promise orders it after the write.
Why It Happens
In a tf.function, an operation waits only for the operations it depends on. It keeps stateful operations such as WriteFile in order, but tf.io.read_file isn't one, and nothing connects it to the write. So the read can run before the write, and then the file isn't there yet.
The Fix
Make the read wait for the write, with tf.control_dependencies:
import tensorflow as tf
@tf.function
def save_and_read(path, text):
written = tf.io.write_file(path, text)
with tf.control_dependencies([written]):
return tf.io.read_file(path)
path = tf.constant("output/note.txt")
text = tf.constant("Saved inside a tf.function\n")
print(save_and_read(path, text))
Inside a tf.function, tf.io.write_file returns the write operation, kept here as written. Every operation created inside the with tf.control_dependencies([written]): block runs after it. The tf.Graph.control_dependencies documentation explains the with block: "all operations constructed within the context should have control dependencies on control_inputs."
Another way: read the file after the function returns
The graph showed that a function always finishes its write before it returns. So write inside the function and read outside it:
import tensorflow as tf
@tf.function
def save(path, text):
tf.io.write_file(path, text)
path = tf.constant("output/note.txt")
text = tf.constant("Saved inside a tf.function\n")
save(path, text)
print(tf.io.read_file(path))
uv run python save_then_read.py
tf.Tensor(b'Saved inside a tf.function\n', shape=(), dtype=string)

The read, outside the function, ran after save had returned, and found the note.
After the Fix
With output/note.txt deleted first, I ran the fixed script:
uv run python save_and_read.py
tf.Tensor(b'Saved inside a tf.function\n', shape=(), dtype=string)

The note came back. Three more runs from a clean folder, and one with the file already there, all printed the same. The commands on this page work unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Testing the broken version a second time | The first run leaves the file behind, so the second run had no error, and printed b'' instead of the note | Delete output/note.txt before each test |
Expecting the lines of a tf.function to run in order | Only stateful operations are kept in order, and tf.io.read_file isn't one | Use tf.control_dependencies wherever one operation must wait for another |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Graph execution error with No such file or directory and [[{{node ReadFile}}]] | The read ran before a write in the same tf.function | Put the read under tf.control_dependencies([written]) |
The read returns b'' or old contents | The same: the read and the write ran in either order | The same fix |
NotFoundError: … No such file or directory [Op:ReadFile] outside a tf.function | A relative path, looked up from the folder you ran Python in | Fixed: NotFoundError: No such file or directory [Op:ReadFile] |
FAQ
Is this a bug in TensorFlow?
I reported it to TensorFlow as issue #131029, with a minimal example: it failed in 10 runs out of 10 on TensorFlow 2.22.0, and in 9 out of 10 on the nightly build, 2.23.0-dev20261202. Until TensorFlow answers it, use tf.control_dependencies.
Why do I still see an OP_REQUIRES failed … NOT_FOUND line after the fix?
That happened when I passed the file name and text as plain Python strings instead of tensors. The fixed function printed the right note, but first logged W0000 … OP_REQUIRES failed at whole_file_read_ops.cc:120 : NOT_FOUND: output/note.txt; No such file or directory. The line was still there with constant folding turned off, which TensorFlow's graph optimization guide shows how to do, and TensorFlow's documentation doesn't say where it comes from. I added it to the issue. With tensors as on this page, the line didn't appear.
Conclusion
NotFoundError: Graph execution error … [[{{node ReadFile}}]] meant that inside the tf.function, tf.io.read_file ran before tf.io.write_file: the read is not a stateful operation, so TensorFlow didn't keep it after the write. with tf.control_dependencies([written]): made the read wait, and the note came back every time.
Additional Resources
- tf.control_dependencies — ordering operations by hand.
- tf.function — turning Python functions into graphs.
- tensorflow/tensorflow#131029 — the issue I reported.
