Fixed: ValueError: Attempt to convert a value (PosixPath(…)) with an unsupported type to a Tensor
Reading a file with tf.io.read_file(data_dir / "notes.txt") stopped with ValueError: Attempt to convert a value (PosixPath('data/notes.txt')) with an unsupported type (<class 'pathlib.PosixPath'>) to a Tensor. TensorFlow turns the file name into a string tensor, and it can't turn a pathlib.Path into one. Wrapping the path in str() fixed it.
What I Was Doing
I was reading a small text file, data/notes.txt, into a tensor. I built its path with Python's pathlib, which joins folder and file names with /:
from pathlib import Path
import tensorflow as tf
data_dir = Path("data")
notes = tf.io.read_file(data_dir / "notes.txt")
print(notes)
The file holds one line, Buy milk. I ran the script in a uv project with TensorFlow 2.22.0:
uv run python read_notes.py
What I Expected
A string tensor with the file's contents: b'Buy milk\n'.
What I Got
Traceback (most recent call last):
File "/Users/you/source/fx-posixpath/read_notes.py", line 6, in <module>
notes = tf.io.read_file(data_dir / "notes.txt")
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/ops/io_ops.py", line 134, in read_file
return gen_io_ops.read_file(filename, name)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/ops/gen_io_ops.py", line 583, in read_file
return read_file_eager_fallback(
filename, name=name, ctx=_ctx)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/ops/gen_io_ops.py", line 603, in read_file_eager_fallback
filename = _ops.convert_to_tensor(filename, _dtypes.string)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/profiler/trace.py", line 183, in wrapped
return func(*args, **kwargs)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/framework/ops.py", line 814, in convert_to_tensor
return tensor_conversion_registry.convert(
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
value, dtype, name, as_ref, preferred_dtype, accepted_result_types
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/framework/tensor_conversion_registry.py", line 234, in convert
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/framework/constant_tensor_conversion.py", line 29, in _constant_tensor_conversion_function
return constant_op.constant(v, dtype=dtype, name=name)
~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/ops/weak_tensor_ops.py", line 142, in wrapper
return op(*args, **kwargs)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/framework/constant_op.py", line 275, in constant
return _constant_impl(value, dtype, shape, name, verify_shape=False,
allow_broadcast=True)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/framework/constant_op.py", line 287, in _constant_impl
return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/framework/constant_op.py", line 300, in _constant_eager_impl
t = convert_to_eager_tensor(value, ctx, dtype)
File "/Users/you/source/fx-posixpath/.venv/lib/python3.14/site-packages/tensorflow/python/framework/constant_op.py", line 109, in convert_to_eager_tensor
return ops.EagerTensor(value, ctx.device_name, dtype)
~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Attempt to convert a value (PosixPath('data/notes.txt')) with an unsupported type (<class 'pathlib.PosixPath'>) to a Tensor.

The script stopped at line 6. The traceback is long because it follows the path through TensorFlow's code, from read_file to the place that gave up.
What I Checked
1. Which value TensorFlow couldn't convert
The message names it: PosixPath('data/notes.txt'), the result of data_dir / "notes.txt". That's a path object, not text. The pathlib documentation says Path represents paths of "the system’s path flavour (instantiating it creates either a PosixPath or a WindowsPath)". On macOS and Linux it's a PosixPath; on Windows it would be a WindowsPath.
2. Where the conversion happens
The traceback shows read_file calling convert_to_tensor(filename, _dtypes.string): before reading, TensorFlow turns the file name into a string tensor. The tf.convert_to_tensor API page lists what it can convert: "It accepts Tensor objects, numpy arrays, Python lists, and Python scalars." A path object is none of those.
3. What tf.io.read_file takes
The tf.io.read_file API page describes its argument filename as a "string. filename to read from."
Why It Happens
TensorFlow operations take their inputs as tensors. tf.io.read_file converts the file name into a string tensor, and that conversion only knows text, numbers, lists, NumPy arrays and tensors. A Path is a Python object TensorFlow doesn't convert, even though Python's own functions accept it.
The Fix
Turn the path into text with str():
from pathlib import Path
import tensorflow as tf
data_dir = Path("data")
notes = tf.io.read_file(str(data_dir / "notes.txt"))
print(notes)
The pathlib documentation describes the result: "The string representation of a path is the raw filesystem path itself (in native form, e.g. with backslashes under Windows), which you can pass to any function taking a file path as a string".
Another way: os.fspath
os.fspath(data_dir / "notes.txt") gives the same text, and tf.io.read_file read the file with it too. It needs import os.
After the Fix
uv run python read_notes.py
tf.Tensor(b'Buy milk\n', shape=(), dtype=string)

The file's contents came back as a string tensor: b'Buy milk\n', the line and its line break. The command works unchanged in PowerShell.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Calling str() on the folder only: str(data_dir) / "notes.txt" | TypeError: unsupported operand type(s) for /: 'str' and 'str': / joins paths, not text | Wrap the whole joined path: str(data_dir / "notes.txt") |
Fixing tf.io.read_file and not tf.io.write_file | tf.io.write_file(path, "x") with a Path stopped with the same ValueError | Pass str(path) to every TensorFlow file operation |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Attempt to convert a value (PosixPath(…)) with an unsupported type | A Path passed to tf.io.read_file or tf.io.write_file | str(path) |
NotFoundError: … No such file or directory [Op:ReadFile] after the fix | The relative path starts from the folder you ran Python in | Fixed: NotFoundError: No such file or directory [Op:ReadFile] |
FAQ
Why does Python's open() accept a Path when tf.io.read_file doesn't?
Python's open() documentation says its file is "a path-like object", so it accepts both text and Path objects. tf.io.read_file is a TensorFlow operation and converts its input to a tensor first.
Do all TensorFlow functions reject a Path?
No. tf.io.gfile.exists(data_dir / "notes.txt") accepted it and returned True. The ones that fail are TensorFlow operations that take a string tensor, such as tf.io.read_file and tf.io.write_file.
Conclusion
Attempt to convert a value (PosixPath('data/notes.txt')) with an unsupported type to a Tensor meant tf.io.read_file got a pathlib.Path and couldn't turn it into a string tensor. str(data_dir / "notes.txt") passed the path as text, and the file read as b'Buy milk\n'.
Additional Resources
- tf.io.read_file — the API page.
- tf.convert_to_tensor — what TensorFlow can turn into a tensor.
- pathlib — Python's paths.
