TensorFlow
TypeError
debugging
Python
machine learning

TypeError Expected binary or unicode string, got list Tensorflow

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

The TensorFlow error TypeError: Expected binary or unicode string, got list usually means a TensorFlow API expected one string value and received a Python list object instead. The fix is rarely “convert everything to strings.” The real fix is to identify whether the API wants a single path, a batch of string tensors, or a loop over many values.

Why TensorFlow Rejects a Python List Here

TensorFlow APIs often distinguish between three different shapes of input:

  • one Python string such as a filename
  • one tensor of type tf.string
  • a Python container that should be handled outside the API call

The error appears when code passes the third kind where the function only supports the first or second.

A common example is file loading. tf.io.read_file reads one file path at a time. Passing a list directly does not mean “read them all.” It means “here is a Python list where a string was expected.”

python
1import tensorflow as tf
2
3filenames = ["a.txt", "b.txt"]
4
5# wrong for tf.io.read_file
6# contents = tf.io.read_file(filenames)

That mismatch is exactly the sort of situation that produces this error.

Fix 1: Pass a Single String When the API Wants One

If the function is designed for one string input, pass one string.

python
1import tensorflow as tf
2
3filename = "a.txt"
4contents = tf.io.read_file(filename)
5print(contents)

This is the simplest fix, and it is often all that is needed when the list was introduced accidentally.

Fix 2: Convert a Batch of Strings into a String Tensor

Some TensorFlow string operations can work on a batch, but they want a tensor of type tf.string, not a raw Python list that lands in the wrong code path.

python
1import tensorflow as tf
2
3sentences = tf.constant([
4    "hello tensorflow",
5    "string ops are strict",
6], dtype=tf.string)
7
8words = tf.strings.split(sentences)
9print(words)

This works because tf.strings.split supports batched string tensors. The key is understanding the API contract rather than assuming every list of strings will be auto-converted correctly.

Fix 3: Iterate with tf.data for Many Files

When you really do want to process many filenames, build a dataset or loop explicitly.

python
1import tensorflow as tf
2
3filenames = ["a.txt", "b.txt", "c.txt"]
4
5dataset = tf.data.Dataset.from_tensor_slices(filenames)
6dataset = dataset.map(tf.io.read_file)
7
8for contents in dataset:
9    print(contents.numpy())

This pattern is scalable and idiomatic. It also makes later transformations such as decoding, tokenization, and batching much easier.

Watch for Nested Lists and Mixed Types

The error message mentions a list, but the deeper problem is often shape or container confusion. For example, a nested list can sneak in during preprocessing:

python
texts = [["first sentence"], ["second sentence"]]

If a later operation expects a flat batch of strings, that structure is wrong even though it “looks like text data.” A safer preprocessing step is:

python
1import tensorflow as tf
2
3texts = ["first sentence", "second sentence"]
4text_tensor = tf.constant(texts, dtype=tf.string)
5print(text_tensor)

When debugging, print both the Python type and the tensor dtype:

python
print(type(texts))
print(text_tensor.dtype)

Those two checks often reveal the mismatch immediately.

Text Pipelines Need Clear Boundaries

This error shows up often in NLP pipelines because it is easy to blur the line between Python preprocessing and TensorFlow execution. Tokenizers, filename lists, and label mappings may start as Python lists, but TensorFlow ops only accept certain forms.

A good rule is:

  • use Python lists for collection and control flow
  • convert to tf.constant or tf.data.Dataset before calling TensorFlow string ops
  • keep APIs that expect a single string on a single-item path

That boundary keeps your pipeline readable and reduces type surprises.

A Practical Debugging Strategy

When you hit this error, find the exact function call that raised it and ask one concrete question: does this function want one string or a string tensor?

If it wants one string, index into the list or loop over the list. If it wants batched string data, create a tf.string tensor or dataset. If you are not sure, inspect the function signature and try a one-item minimal example first.

That approach is faster than blindly wrapping the list in str(...), which only hides the real issue and usually creates invalid input.

Common Pitfalls

The most common pitfall is assuming TensorFlow will automatically treat a Python list of strings as a valid batched tensor in every API.

Another issue is using a single-item API such as tf.io.read_file on a whole list of paths.

Nested lists are also easy to miss. A list of lists is not the same as a flat string batch.

Finally, avoid fixing the message by stringifying the list itself. Converting ['a', 'b'] into one giant string is almost never what the TensorFlow operation wants.

Summary

  • This error means a TensorFlow API expected a string-like input and got a Python list.
  • Pass one string to single-item APIs such as file readers.
  • Use tf.constant(..., dtype=tf.string) or tf.data.Dataset for batched string workflows.
  • Check for nested lists and unexpected Python container types during preprocessing.
  • Debug the contract of the specific TensorFlow function instead of applying a generic conversion.

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